JUnit 4.0 ExampleThe following classes are the application code for which we want to write unit tests using JUnit. package info.compute.example;
public class Class1 {
public String meth() {
return "Class1";
}
}
package info.compute.example;
public class Class2 {
public boolean meth() {
return true;
}
}
We create one unit test class per application class.
package info.compute.example.test;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestClass1 {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testMeth() {
Class1 c1 = new Class1();
String result = c1.meth();
assertEquals("Result", "Class1", result);
}
}
package info.compute.example.test;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestClass2 {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testMeth() {
Class2 c2 = new Class2();
boolean result = c2.meth();
assertTrue(result);
}
}
Then we create a test suite that would execute all unit tests for the application. package info.compute.example.test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({Class1.class, Class2.class})
public class AllTest {
//nothing
}
|