1. org.junit.runners.AllTests
Runner for use with JUnit 3.8.x-style AllTests classes (those that only implement a static suite() method). For example:
@RunWith(AllTests.class)
public class ProductTests {
public static junit.framework.Test suite() {
...
}
}
2. org.junit.runners.Enclosed
Enclosed runner (which runs all static inner classes)
import org.junit.runners.*;
import org.junit.runner.*;
import org.junit.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
@RunWith(value=Enclosed.class) //Iam using Enclosed runner
public class J41Innerclass{
public static class publicstaticInnerclass //Inner class
{
@Test
public void tstasserttht() //Test method in inner class
{
int i=3;
System.out.println("Inside test assertThat method");
assertThat(i,is(4));
}
}
@Test
public void outermethod()
{
assertEquals(1,1);
}
}
3. org.junit.runners.Parameterized
The custom runner Parameterized implements parameterized tests. When running a parameterized test class, instances are created for the cross-product of the test methods and the test data elements.
For example, to test a Fibonacci function, write:
@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters
public static Collection data() {
return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
{ 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
}
private int fInput;
private int fExpected;
public FibonacciTest(int input, int expected) {
fInput= input;
fExpected= expected;
}
@Test public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
Each instance of FibonacciTest will be constructed using the two-argument constructor and the data values in the @Parameters method.
4. org.junit.runners.Suite
Using Suite as a runner allows you to manually build a suite containing tests from many classes. It is the JUnit 4 equivalent of the JUnit 3.8.x static Test suite() method. To use it, annotate a class with @RunWith(Suite.class) and @SuiteClasses(TestClass1.class, ...). When you run this class, it will run all the tests in all the suite classes.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment