Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit test class with multiple @Test methods

Tags:

java

junit

junit4

I have a Junit test class with multiple @Test methods in it that I need to run in order. If there is an exception thrown in a method I would like to stop entire test case and error out, but all of the rest of the test methods running.

public class{

@Test{
 //Test1 method`enter code here`
}

@Test{
 //Test2 method
}

@Test{
 //Test3 method
}

}

If Test1 method fails then don't run other Tests

Note: All are independent tests

like image 233
Lingaraj R M Avatar asked Dec 13 '12 13:12

Lingaraj R M


People also ask

What is the use of @test in JUnit?

A JUnit test is a method contained in a class which is only used for testing. This is called a Test class. To define that a certain method is a test method, annotate it with the @Test annotation. This method executes the code under test.

Which methods Cannot be tested by JUnit test class?

Which methods cannot be tested by the JUnit test class? Explanation: When a method is declared as “private”, it can only be accessed within the same class. So there is no way to test a “private” method of a target class from any JUnit test class.

What is @test annotation in JUnit?

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure.


5 Answers

Unit tests should be designed to run independently of one another. The order of execution cannot be guaranteed. You should redesign your test class so that the order is not important.

Without further information it's hard to advise you specifically. But it may help to have an @before method, which checks for some precondition before running each test. If you included an Assume.assumeTrue(...) method call, then your test could be skipped if the condition fails?

like image 112
Duncan Jones Avatar answered Oct 17 '22 11:10

Duncan Jones


As discribed here, JUnit 4.11 supports the ordered execution with the Annotation @FixMethodOrder, but the others are right, all tests should be independent of each other.

At the end of a test you can set a global success flag. This flag will be tested at the begining of each test. If the flag is not set by the end of one test(because it fails before finishing) all other tests will fail too. Example:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ConsecutiveFail{
  private boolean success = true;

  @Test
  public void test1{
    //fist two statements in all tests
    assertTrue("other test failed first", success);
    success = false;
    //do your test
    //...

    //last statement
    success = true;
  }

  @Test
  public void test2{
    //fist two statements in all tests
    assertTrue("other test failed first", success);
    success = false;
    //do your test
    //...

    //last statement
    success = true;
  }
}
like image 34
Simulant Avatar answered Oct 17 '22 10:10

Simulant


I was able to achieve what you are looking with intellij idea IDE, I am using community edition.

Go to Edit Configuration in the class where test methods available.( Run --> Edit Configurations)

Select Test kind as "Class" as in the below Image.

enter image description here

When you run the Class Test it will execute all @Test Annotated methods inside the class as in below Picture.

enter image description here

like image 41
Chamith Chathuka Avatar answered Oct 17 '22 10:10

Chamith Chathuka


If you need the consequence to be kept and failing a test not to fail the whole set, put all such tests in one and test by assume.

like image 27
Gangnus Avatar answered Oct 17 '22 09:10

Gangnus


Here's example for TESTNG how to specify test running order:

@Test(priority = 1)
public void test1(){}

@Test(priority = 2)
public void test2(){}

@Test(priority = 3)
public void test3(){}
like image 28
Nikolai Tarasov Avatar answered Oct 17 '22 10:10

Nikolai Tarasov