JUnit 4 Features
One of the main differences between JUnit 3 and JUnit 4 is the use of annotation instead of naming convention.
Using @Test to define a test method
using the @Test annotation we can define any method as a unit test no matter what the name of the method is. In JUnit 3 we needed to start a method name with the word 'test' . In JUnit 4 we can write the word 'test' in the beginning of the method name but it will have no effect. We will need to add the @Test annotation before the method
@Test
public void testMethod() {
...
}
or we can just write:@Test
public void method() {
...
}
Expecting Exceptions
If out test case need to check that exception is thrown we can use the @Test(expected=Exception.class) annotation. The test will fail if no Exception will be thrown.
@Test(expected=IllegalArgumentException.class)
public void checkForException() {
throw new IllegalArgumentException();
}
Test Timeout
If we want to make sure that our code is execute within a time limit, we can define a timeout for the test. The following test will fail after 100 milliseconds.
@Test(timeout=100)
public void method() {
Thread.sleep(1000);
}
Disable Test
In case we want to disable a test but we still want to see it on the tests list we can use the @Ignore annotation. This way we can write why did we disabled it. The comment will be visible in the final report
@Ignore("This is an example on how to ignore JUnit tests")
@Test()
public void method() {
...
}
@Before and @After instead of setUp() and tearDown()
@Before
public void doSomthing1() {
...
}
@After
public void doSomthing2() {
...
}
We can choose whatever method name we want.
We can define as many method as we want with @Before and @After. Each one of them will be execure before or after any unit test method in the class.
In order to define a method that will be execute before or after the whole class we can use the annotations: @BeforeClass and @AfterClass
Test Suites
This test suite will run the classes Test1, Test2, Test3 and Test4 one after the over.
package com.shimi.example;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses( {
Test1.class,
Test2.class,
Test3.class,
Test4.class
})
public class TestSuite {
}
Parameterized Unit Tests
Parameterized unit tests is a feature which came to help us with cases when we need to pass different parameters to our unit test. To define a parameterized unit test we need to use the @RunWith(value=Parameterized.class) annotation for the class. We need to define a Collection with the parameters values and the @Parameters annotation. We will need to write a Constructor which will get the parameters values and will set them in the class variables. In the following example the unit test will run 6 times. Each time JUnit will call the class Constructor with a different pair of Strings from the paramsValues Collection. The Constructor will initialize the class members with the given values. Then, it will call the unit test method which will check if the two Strings are equals.
@RunWith(value=Parameterized.class)
public class ParameterizedTest {
private String param1
private String param2
@Parameters
public static Collection paramsValues {
return Arrays.asList( new Object[][] {
{ "abc", "abc" },
{ "ab", "ba" },
{ "a", "b" },
{ "ab", "a" },
{ "abcd", "abcd" },
});
}
public ParameterizedTest(String param1, String param2) {
this.param1 = param1;
this.param2 = param2;
}
@Test
public void test {
assertEquals(param1, param2);
}
}
Assert Results
using the JUnit assert method we can assert different results values and assign an error message.
assertEquals("Object 1 and Object 2 are different", object1, object2);
The assertEquals method assert Objects. If we will use it for primitiveassertEquals("Object 1 and Object 2 are different", 1, 1);
The result will be false. Java will use autoboxing in order to convert the primitives to Object. We can compare primitives by using the method assertTrue()int param1 = 1;
int param2 = 1;
assertTrue("Param 1 and Param 2 are different", param1 == oparam2);
Subscribe to:
Post Comments (Atom)
Post a Comment