183
社区成员
发帖
与我相关
我的任务
分享| The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ |
|---|---|
|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/603748107 |
| The Aim of This Assignment | Unittest for JAVA |
| MUID&FZUID | 19103557_831902230 |
import static org.junit.Assert.*;
import org.junit.Test;
public class AssertionsTest {
String obj1 = "junit";
String obj2 = "junit";
String obj3 = "test";
String obj4 = "test";
String obj5 = null;
int var1 = 1;
int var2 = 2;
int[] arithmetic1 = { 1, 2, 3 };
int[] arithmetic2 = { 1, 2, 3 };
@Test
public void test() {
assertEquals(obj1, obj2);
assertEquals(obj3, obj4);
assertNull(obj5);
assertTrue(var1 < var2);
assertArrayEquals(arithmetic1, arithmetic2);
}
}
2.Junit time test
import org.junit.Test;
public class TestTimeOut {
// Fix timeout assert in Test function below. Test fail if running 1000ms longer
/***********************Begin**************************/
@Test(timeout=1000)
public void test() {
while(true){}
}
/************************End***************************/
}
3.Junit parameterized test
import static org.junit.Assert.assertEquals; // static import
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import step1.Calculator;
/**
* JUnit4 parameterized test
*/
@RunWith(Parameterized.class)
public class ParameterTest {
private int input11;
private int input22;
private int expected;
public ParameterTest(int input11, int input22, int expected){
this.input11 = input11;
this.input22 = input22;
this.expected = expected;
}
@Parameters
public static Collection prepareData(){
/**
*the type of the two-dimension array must be Object.
*data in the two-dimension array is ready of test sub() in Calculator
* every element in the two-dimension array should corresponds to position of parameters in construct method ParameterTest
*let the third element equals the first subtract the second element according to parameters’ postion
*fix missing codes under ‘Begin’ and above ‘End’,pass 4 groups of parameters to test sub method in Calculator is right or not
*tip:only two lines of codes
*/
/*********************************Begin********************************************/
Object zx [][]= {{1,2,3},{5,6,-1},{9,5,4},{4,3,0}};
return Arrays.asList(zx);
/**********************************End********************************************/
}
@Test
public void testSub(){
Calculator cal = new Calculator();
assertEquals(cal.sub(input11, input22), expected);
}
}
4.Junit Exception Test
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import step2.Person;
public class JunitException {
/**
*add a line of annotation in Begin/End,check the age of Person Object is legal or not. *throw IllegalArgumentException exception
*/
/***********************************Begin***********************************/
@Test(expected = IllegalArgumentException.class)
/************************************End************************************/
public void checkage() {
Person person = new Person();
person.setAge(-1);
}
}
5.Junit Suite Test
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import step3.Calculate;
import step3.CalculateTest;
import step3.Car;
import step3.CarTest;
/*
add two lines of annotations. Implement Suite Test of CalculateTest and CarTest
Suite Test codes must next to Class SuiteTest, no shift allowed!
*/
//**************************************************************
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculateTest.class,CarTest.class})
public class SuiteTest {
}