1. 测试public方法
待测代码:
package myPro;
public class ACase {
public String echoRequest(String[] request) {
StringBuffer sb = new StringBuffer();
for(String str: request) sb.append(" " + str);
return "Hello!" + sb.toString();
}
public String echoRequest() {
return "Hello!";
}
}
测试代码:
package myPro;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class ACaseTest {
ACase a = null;
@Before
public void setUp() throws Exception {
a = new ACase();
}
@Test
public void testEchoRequestStringArray() {
String[] request = "this is a test information".split(" ");
String s = a.echoRequest(request);
System.out.println(s);
assertNotNull(s);
}
@Test
public void testEchoRequest() {
String s = a.echoRequest();
System.out.println(s);
assertNotNull(s);
}
}
2. 测试staic内部类的private方法
待测代码:
package myPro;
public class BCase {
static class InnerBCase {
private String echoRequest(String[] request) {
StringBuffer sb = new StringBuffer();
for(String str: request) sb.append(" " + str);
return "Hello!" + sb.toString();
}
private String echoRequest() {
return "Hello!";
}
}
}
测试代码:
package myPro;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
public class BCaseTest {
BCase.InnerBCase a = null;
@Before
public void setUp() throws Exception {
a = new BCase.InnerBCase();
}
/**
* @function: test method with parameters
*/
@Test
public void testParamEchoRequest() throws Exception {
Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest",String[].class);
testNoParamMethod.setAccessible(true);
String[] strs = "this is a test information".split(" ");
Object result = testNoParamMethod.invoke(a, (Object)strs); //注意此处String[]类型的参数,必须加上(Object),否则会认为为可变参数,报错
System.out.println(result);
assertNotNull(result);
}
/**
* @function: test methods with no parameters
*/
@Test
public void testNoParamEchoRequest() throws Exception {
Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest", null);
testNoParamMethod.setAccessible(true);
Object result = testNoParamMethod.invoke(a, null);
System.out.println(result);
assertNotNull(result);
}
}
3. 测试void方法
待测代码:
package myPro;
public class CCase {
String nickname = null;
String[] logFormat = null;
private void generateLogFormat(String str) {
if (str == null)
return;
str = str.trim();
int k = str.length() - 1;
while (k>=0 && str.charAt(k) != '"')
k--;
if(k <= 1) return;
nickname = str.substring(k + 1).trim();
str = str.substring(1, k);
logFormat = str.split(" ");
}
}
测试代码:
package myPro;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
public class CCaseTest {
CCase c = null;
@Before
public void setUp() throws Exception {
c = new CCase();
}
@Test
public void testGenerateLogFormat() throws Exception {
String str = "\"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"\" combined";
Method testNoParamMethod = c.getClass().getDeclaredMethod("generateLogFormat",String.class);
testNoParamMethod.setAccessible(true);
testNoParamMethod.invoke(c, (Object)str);
assertNotNull(c.nickname);
System.out.println(c.nickname);
assertNotNull(c.logFormat);
for(String s: c.logFormat) System.out.print(s + " ");
System.out.println();
}
}
推荐:点击打开链接