10/11/2007

unit tests: multiple instances vs single instance

Often I shock participants of my testing seminars with the following question: "what is the result of the following tests running with JUnit?".

package com.huettermann;

import org.junit.*;
import static org.junit.Assert.*;

/**
* @author Michael Hüttermann
*/
public class JUnitTest {

private int count = 0;

@Test
public void test1() {
count++;
assertEquals(1, count);
}

@Test
public void test2() {
count++;
assertEquals(1, count);
}
}

The tests run through successfully! Why? For every test method a new instance of the test class will be created. count is 0 first place starting method test1 and test2.

Why is this shocking? Well, many people expect one test class runs a group of tests isolated. But with JUnit this isn't true. There one test (i.e. one test method in the test class) runs isolated. Interesting enough this approach is not unique for all test frameworks. Have a look on TestNG.

package com.huettermann;

import org.testng.annotations.Test;
import org.testng.Assert;

/**
* @author Michael Hüttermann
*/
public class TestNGTest {

private int count = 0;

@Test
public void test1() {
count++;
Assert.assertEquals(1, count);
}

@Test
public void test2() {
count++;
Assert.assertEquals(1, count);
}
}

TestNG creates one instance for the test class which all tests in this class share. So the test run outputs:

PASSED: test1
FAILED: test2
java.lang.AssertionError: expected:<2> but was:<1>
at com.huettermann.TestNGTest.test2(TestNGTest.java:22)
... Removed 27 stack frames


What do you think which approach is better? Why?

2 comments:

Elmar Brandt said...

Hello Michael,
I think the way of TestNG is consequential, because it is like in other java classes. The test-class has a class variable, in the first method call it was incremented and at the second time the value is one not zero and the test must fail.

Best regards,

Elmar

Srinath said...

Hello Mike(Michael),

Nice post. Something new to me. keep posting such interesting facts in future as well.

Kind Regards,

Srinath