Just JUnit
Calculator.java
package com.dev.junit.test;
public class Calculator {
public int add(int value1, int value2){
return value1+value2;
}
public int substract(int value1, int value2){
return value1-value2;
}
public int multiply(int value1, int value2){
return value1*value2;
}
public int devide(int value1, int value2){
return value1/value2;
}
}
Without Annotations
CalculatorTest.java
CalculatorTest.java
package com.dev.junit.test;
With Annotations
CalculatorTest.java
package com.dev.junit.test;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest {
import junit.framework.TestCase;
public class CalculatorTest extends TestCase{
Calculator calc = null;
public void setUp(){
// to set up data
// runs before execution of every test case
calc = new Calculator();
}
public void testAdd() {
assertNotNull(calc);
int result = calc.add(4, 2);
assertEquals(6,result);
}
public void testSubstract() {
assertNotNull(calc);
int result = calc.substract(4, 2);
assertEquals(2,result);
}
public void testMultiply() {
assertNotNull(calc);
int result = calc.multiply(4, 2);
assertEquals(8,result);
}
public void testDevide() {
assertNotNull(calc);
int result = calc.devide(4, 2);
assertEquals(2,result);
}
public void tearDown(){
// to remove data
// runs after execution of every test case
}
}
With Annotations
CalculatorTest.java
package com.dev.junit.test;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest {
Calculator calc = null;
@Before
public void setUp() throws Exception {
// to setup data
// runs before execution of every test case
calc = new Calculator();
}
@After
public void tearDown() throws Exception {
// to remove data
// runs after execution of every test case
}
@Test
public void testAdd() {
assertNotNull(calc);
int result = calc.add(4, 2);
assertEquals(6,result);
}
@Test
public void testSubstract() {
assertNotNull(calc);
int result = calc.substract(4, 2);
assertEquals(2,result);
}
@Test
public void testMultiply() {
assertNotNull(calc);
int result = calc.multiply(4, 2);
assertEquals(8,result);
}
@Test
public void testDevide() {
assertNotNull(calc);
int result = calc.devide(4, 2);
assertEquals(2,result);
}
}