Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6940541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:45:57+00:00 2026-05-27T12:45:57+00:00

I am using junit with eclipse to write function tests. When running an individual

  • 0

I am using junit with eclipse to write function tests.

When running an individual test it runs in the order that I have them set within the class.

Eg.

testCreateUser
testJoinUserToRoom
testVerify
testDeleteUser

However when I run this test as part of a suite, (so in a package) the order is random.

It will for example do the verify, then delete user then joinuserToRoom then Createuser.

My tests within the suite are not dependent on each other. However each individual test within a test is dependent on them being run in the correct order.

Is there any way I can achieve this?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:45:58+00:00Added an answer on May 27, 2026 at 12:45 pm

    You can’t guarantee the order of execution of test methods in JUnit.

    The order of execution of test classes within a suite is guaranteed (if you’re using Suite), but the order of execution if the test classes are found by reflection isn’t (for instance, if you’re running a package in Eclipse, or a set of tests from maven or ant). This may be definable by ant or maven, but it isn’t defined by JUnit.

    In general, JUnit executes the test methods in the order in which they are defined in the source file, but not all JVMs guarantee this (particulary with JVM 7). If some of the methods are inherited from an abstract base test class, then this may not hold either. (This sounds like your case, but I can’t tell from your description).

    For more information on this, see my answer to Has JUnit4 begun supporting ordering of test? Is it intentional?.

    So what can you do to fix your problem? There are two solutions.

    In your original example, you’ve actually only got one test (verify), but you’ve got 4 methods, two setup (createUser, joinUserToRoom) and one teardown (deleteUser). So your first option is to better define your test cases, using a TestRule, in particular ExternalResource. ExternalResource allows you to define before/after behaviour for a test, similar to @Before/@After. However, the advantage of ExternalResource is that you can factor this out of your test.

    So, you would create/delete the user in your external resource:

    public class UsesExternalResource {
         @Rule
         public ExternalResource resource= new ExternalResource() {
             @Override
             protected void before() throws Throwable {
                // create user
             };
    
             @Override
             protected void after() {
                // destroy user
             };
         };
    
         @Test
         public void testJoinUserToRoom() {
            // join user to room
            // verify all ok
         }
    }
    

    For me, this is simpler and easier to understand, and you get independent tests, which is a good thing. This is what I would do, but you will need to refactor your tests a bit. You can also stack these Rules using RuleChain.

    Your second option, if you really want to introduce dependencies between your test methods, is to look at TestNG, in which you can define dependencies from one test to another.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have setup an android test project that runs junit tests. It's using two
I have the following test case in eclipse, using JUnit 4 which is refusing
We have a lot of integration tests written using JUnit 3 , though we're
I have two tests to check the expected exception throw. I am using Junit
I have recently been using junit in eclipse and I am still learning. I
I have an extremely odd problem in my JUnit tests that I just can't
I have organised my junit tests using one inner class per method as described:
I'm using Eclipse Indigo on win XP and trying to write a GWT test
Is there a way of getting JUnit tests in Eclipse (specifically I'm using SpringJUnit4ClassRunner
I have written few test scripts using JUnit 4 and Selenium. I have added

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.