Asked  7 Months ago    Answers:  5   Viewed   49 times

I want to execute test methods which are annotated by @Test in specific order.

For example:

public class MyTest {
    @Test public void test1(){}
    @Test public void test2(){}
}

I want to ensure to run test1() before test2() each time I run MyTest, but I couldn't find annotation like @Test(order=xx).

I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature, why?

 Answers

88

I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature, why?

I'm not sure there is a clean way to do this with JUnit, to my knowledge JUnit assumes that all tests can be performed in an arbitrary order. From the FAQ:

How do I use a test fixture?

(...) The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). (...)

Why is it so? Well, I believe that making tests order dependent is a practice that the authors don't want to promote. Tests should be independent, they shouldn't be coupled and violating this will make things harder to maintain, will break the ability to run tests individually (obviously), etc.

That being said, if you really want to go in this direction, consider using TestNG since it supports running tests methods in any arbitrary order natively (and things like specifying that methods depends on groups of methods). Cedric Beust explains how to do this in order of execution of tests in testng.

Tuesday, June 1, 2021
 
khaverim
answered 7 Months ago
71

New Instance of MyTest for each test method

For each test method a new instance of MyTest will be created this is the behavior of Junit.

So in your case for both methods the variable count will have value 1, and thus the value of count++ will be 2 for both the test methods and hence the test cases pass.

public class MyTest{
   public MyTest(){
      // called n times
      System.out.println("Constructor called for MyTest");
   }

   @Before //called n times
   public void setUp(){
      System.out.println("Before called for MyTest");
   }

   //n test methods
}

If you execute the code above with 2 test methods:

Output will be:

Constructor called for MyTest
Before called for MyTest
//test execution
Constructor called for MyTest
Before called for MyTest
Saturday, July 3, 2021
 
diegoiglesias
answered 6 Months ago
75

There is 3 most common ways to test expected exception:

First one is the most common way, but you can test only the type of expected exception with it. This test will fail if ExceptionType won't be thrown:

@Test(expected = ExceptionType.class)
public void testSomething(){
    sut.doSomething();
}

Also you cannot specify the failure message using this approach

The better option is to use ExpectedException JUnit @Rule. Here you can assert much more for expected exception

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testSomething(){
    thrown.expect(ExceptionType.class);
    thrown.expectMessage("Error message");
    thrown.expectCause(is(new CauseOfExeption()));
    thrown.reportMissingExceptionWithMessage("Exception expected"); 
    //any other expectations
    sut.doSomething();
}

The third option will allow you to do the same as with using ExpectedException @Rule, but all the assertion should be written manually. However the advantage of this method is that you can use any custom assertion and any assertion library that you want:

@Test
public void testSomething(){
    try{
        sut.doSomething();
        fail("Expected exception");
    } catch(ExceptionType e) {
    //assert ExceptionType e
    } 
}
Monday, August 2, 2021
 
ramdemon
answered 5 Months ago
82

Yes, 'random' should really be 'non predictable'.

If you want true randomization, look up IMethodInterceptor, where TestNG will give you an opportunity to change the ordering to anything you like.

Wednesday, August 4, 2021
 
Noctis
answered 4 Months ago
17

Use thread.

t1 = Thread.new do
  first_method
end
second_method
t1.join
Tuesday, October 19, 2021
 
Mohd Abdul Mujib
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share