簡單的Junit4 case
在junit4的架構下,直接用Annotation表示要執行的test method
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/*
* if unit test need run many times with different parameter and result
*/
@RunWith(Parameterized.class)
public class IPUtil_UT_HasIP {
private List<String> ipList;
private String ip;
private boolean result;
@SuppressWarnings("rawtypes")
@Parameters
public static Collection data() {
List<String> ips = new ArrayList<String>(300);
for(int i= 1;i<256;i++){
ips.add("192.168.1."+i);
}
ips.add("127.0.58.32");
return Arrays.asList(new Object[][]{
{ips, "192.168.1.10", true},
{ips, "192.168.1.1", true},
{ips, "10.2.78.10", false},
{ips, "192.168.1.255", true},
{ips, "192.168.1.123", true},
{ips, "127.0.58.32", true},
{ips, "abcd", false}
});
}
/*
* Will get parameter set for this constructor
* and run @Test method each time
*/
public IPUtil_UT_HasIP (List<String> ipList, String ip, boolean result) {
this.ipList = ipList;
this.ip = ip;
this.result = result;
}
/*
* test method
*/
@Test
public void IPUtil_HasIP_Test() {
Assert.assertEquals(result, IPUtil.hasIP(ipList, ip));
}
/*
* Run before test method
*/
@Before
public void setUp() throws Exception {
}
/*
* Run after test method
*/
@After
public void tearDown() throws Exception {
}
/*
* At the beginning
*/
@BeforeClass
public void beforeClass() {
}
/*
* At the end
*/
@AfterClass
public void afterClass() {
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/*
* if unit test need run many times with different parameter and result
*/
@RunWith(Parameterized.class)
public class IPUtil_UT_HasIP {
private List<String> ipList;
private String ip;
private boolean result;
@SuppressWarnings("rawtypes")
@Parameters
public static Collection data() {
List<String> ips = new ArrayList<String>(300);
for(int i= 1;i<256;i++){
ips.add("192.168.1."+i);
}
ips.add("127.0.58.32");
return Arrays.asList(new Object[][]{
{ips, "192.168.1.10", true},
{ips, "192.168.1.1", true},
{ips, "10.2.78.10", false},
{ips, "192.168.1.255", true},
{ips, "192.168.1.123", true},
{ips, "127.0.58.32", true},
{ips, "abcd", false}
});
}
/*
* Will get parameter set for this constructor
* and run @Test method each time
*/
public IPUtil_UT_HasIP (List<String> ipList, String ip, boolean result) {
this.ipList = ipList;
this.ip = ip;
this.result = result;
}
/*
* test method
*/
@Test
public void IPUtil_HasIP_Test() {
Assert.assertEquals(result, IPUtil.hasIP(ipList, ip));
}
/*
* Run before test method
*/
@Before
public void setUp() throws Exception {
}
/*
* Run after test method
*/
@After
public void tearDown() throws Exception {
}
/*
* At the beginning
*/
@BeforeClass
public void beforeClass() {
}
/*
* At the end
*/
@AfterClass
public void afterClass() {
}
}
留言
張貼留言