Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit: new instance before invoking each @Test method. What are the benefits?

Tags:

Currently, I am reading "JUnit in action" book. In this book I found text below:

JUnit creates a new instance of the test class before invoking each @Test method. This helps provide independence between test methods and avoids unintentional side effects in the test code. Because each test method runs on a new test class instance, we can’t reuse instance variable values across test methods.

Now I do not see much point in this approach:

For example:

public class CalculatorTest {     @Test     public void testAdd_1() {         Calculator calculator = new Calculator();         double result = calculator.add(1, 1);         assertEquals(2, result, 0);     }      @Test     public void testAdd_2() {         Calculator calculator = new Calculator();         double result = calculator.add(2, 2);         assertEquals(4, result, 0);     } } 

For test class CalculatorTest there are no any benefits.

Ok, lets go pay attention on another example:

public class OneTest {      static byte count;      public OneTest() {         count++;     }      @Test     public void test1() {         System.out.println(count);     }      @Test     public void test2() {         System.out.println(count);     } } 

For test class OneTest I found a way to use the same variable count for the many test methods...

so, How to see the real benefits of the approach described in the book?

like image 950
user471011 Avatar asked Dec 23 '12 10:12

user471011


People also ask

What are the reasons and benefits for using JUnit for testing?

JUnit is elegantly simple. It is less complex and takes less time. JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.

What is the use of @test annotation in JUnit?

The @Test annotation is used to identify the actual test case. This is required as JUnit allows multiple tests to be grouped under a single test class. The test method is where assertions are done and the result is determined.

What is the purpose of @test annotation in Java?

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.

What happens when a JUnit method has the annotation before?

The @Before annotation is used when different test cases share the same logic. The method with the @Before annotation always runs before the execution of each test case. This annotation is commonly used to develop necessary preconditions for each @Test method.


1 Answers

How to see the real benefits of the approach described in the book?

The purpose of separate instance is not for any benefit but to maintain the contract that each test should be independently executed without any effect of the execution of a previous test. There is just no other way to ensure this contract other than using a different instance for each test.

For example, the Spring transaction management makes sure to rollback all changes made to the database by a test, by default, to maintain the same contract.

So, using static variables in a test is generally discouraged as it will defeat the whole purpose of one-instance-per-test to have a clean slate for each test.

like image 153
Vikdor Avatar answered Sep 18 '22 02:09

Vikdor