Calculator.java
package org.zbq.test;
public class Calculator {
public int add(int a, int b){
return a + b;
}
public int subtract(int a, int b){
return a - b;
}
public int multiply(int a, int b){
return a * b;
}
public int division(int a, int b) throws Exception {
if(0 == b){
throw new Exception("Disior by zero");
}
return a / b;
}
}
package org.zbq.test;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class CalculatorTest {
private Calculator cal;
@BeforeClass
public static void init(){
System.out.println("Init");
}
@AfterClass
public static void destroy(){
System.out.println("Destroy");
}
@Before
public void setUp(){
System.out.println("Before");
cal = new Calculator();
}
@After
public void tearDown(){
System.out.println("After");
}
@Test(timeout = 500)
public void testAdd(){
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
int result = cal.add(2, 3);
assertEquals(5, result);
}
@Test
public void testSubtract(){
int result = cal.subtract(5, 3);
assertEquals(2, result);
}
@Test
public void testMultiply(){
int result = cal.multiply(4, 5);
assertEquals(20, result);
}
@Test
public void testDivision() throws Exception {
int result = cal.division(10, 3);
assertEquals(3, result);
}
@Test(expected = Exception.class)
public void testDivisionByZero() throws Exception{
cal.division(5, 0);
}
@Test
public void testDivisionByZero2() throws Exception{
Throwable ex = null;
try{
cal.division(5, 0);
Assert.fail("Divisor by zero");
}catch(Exception e){
ex = e;
}
assertEquals(Exception.class, ex.getClass());
assertEquals("Disior by zero", ex.getMessage());
}
@Test
@Ignore("not yet")
public void ignore(){
System.out.println("Ignore");
}
}
ParametersTest.java
package org.zbq.test;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class ParametersTest {
private int result;
private int a;
private int b;
private Calculator cal;
public ParametersTest(int result, int a, int b){
this.result = result;
this.a = a;
this.b = b;
}
@SuppressWarnings("unchecked")
@Parameters
public static Collection parameterData(){
Object[][] obj = {{5, 2, 3},{-2, 4, -6},{0, 3, -3}};
return Arrays.asList(obj);
}
@Before
public void setUp(){
this.cal = new Calculator();
}
@Test
public void testAdd(){
assertEquals(this.result, cal.add(this.a, this.b));
}
}
Largest.java
package org.zbq.test;
public class Largest {
public static int getLargest(int[] array) throws Exception{
if(null == array || array.length ==0){
new Exception("数组为空");
}
int t = array[0];
for(int i : array){
if(t < i){
t = i;
}
}
return t;
}
}
LargestTest.java
package org.zbq.test;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class LargestTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testGetLargest() throws Exception {
int[] array = new int[]{1,2,5,3,4};
int max = Largest.getLargest(array);
assertEquals(5, max);
}
@Test(expected = Exception.class)
public void testGetLargest2() throws Exception{
int[] array = new int[]{};
Largest.getLargest(array);
}
}
TestAll.java
package org.zbq.test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorTest.class, LargestTest.class})
public class TestAll {
}