Aşağıdaki gibi bir sınıfınız olsun :
public class SimpleTest {
public int topla(int a, int b){
return a+b;
}
public int carp(int a, int b){
return a*b;
}
}
Bu sınıf içindeki topla ve carp method'larını test eden sınıf aşağıdaki gibi yazılabilir:
import org.junit.*;
import static org.junit.Assert.*;
public class Tester {
SimpleTest simple=null;
@Before
public void setUp(){
simple=new SimpleTest();
}
@After
public void tearDown(){
simple=null;
}
@Test
public void testTopla(){
int c=simple.topla(1,2);
assertEquals(3, c);
}
@Test
public void testCarp(){
int c=simple.carp(1,2);
assertEquals(2, c);
}
}
Yukarıdaki örnekte, SimpleTest nesnesi setUp içinde yapılmaktadır. Bu şekilde her test öncesi nesne yaratılır ve test sonrası nesne null haline getirilir