Here following is the class to be tested using JUnit
public class MyClass {
public int add(int a,int b){
return a+b;
}
public int sub(int a,int b){
return a-b;
}
public int mul(int a,int b){
return a*b;
}
public int div(int a,int b){
return a/b;
}
}
class that contains three testing method to test add method of class MyClass
import static org.junit.Assert.assertEquals;
import org.junit.*;
public class MyClassTest {
@Test
public void add() {
MyClass c=new MyClass();
assertEquals(30, c.add(10,20));
}
@Test
public void add2() {
MyClass c=new MyClass();
assertEquals(310, c.add(10,20));
}
@Test
public void add3() {
MyClass c=new MyClass();
assertEquals(30, c.add(10,20));
}
public static void main(String args[]) {
org.junit.runner.JUnitCore.main("MyClassTest");
}
}
Now set junit-4.5.jar into classpath , compile and run .
similarly you can write test cases for other methods.
public class MyClass {
public int add(int a,int b){
return a+b;
}
public int sub(int a,int b){
return a-b;
}
public int mul(int a,int b){
return a*b;
}
public int div(int a,int b){
return a/b;
}
}
class that contains three testing method to test add method of class MyClass
import static org.junit.Assert.assertEquals;
import org.junit.*;
public class MyClassTest {
@Test
public void add() {
MyClass c=new MyClass();
assertEquals(30, c.add(10,20));
}
@Test
public void add2() {
MyClass c=new MyClass();
assertEquals(310, c.add(10,20));
}
@Test
public void add3() {
MyClass c=new MyClass();
assertEquals(30, c.add(10,20));
}
public static void main(String args[]) {
org.junit.runner.JUnitCore.main("MyClassTest");
}
}
Now set junit-4.5.jar into classpath , compile and run .
similarly you can write test cases for other methods.
Comments