Junit 单元测试
来源:互联网 发布:朴素贝叶斯算法 matlab 编辑:程序博客网 时间:2024/05/16 17:36
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;@Beforepublic void setUp() throws Exception {a = new ACase();}@Testpublic void testEchoRequestStringArray() {String[] request = "this is a test information".split(" ");String s = a.echoRequest(request);System.out.println(s); assertNotNull(s);}@Testpublic 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;@Beforepublic 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;@Beforepublic void setUp() throws Exception {c = new CCase();}@Testpublic 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();}}
推荐:点击打开链接
0 0
- JUnit单元测试
- JUnit 单元测试
- 单元测试JUnit
- Junit单元测试
- Junit单元测试
- junit单元测试
- JUnit单元测试
- Junit单元测试
- JUnit单元测试
- junit单元测试
- Junit单元测试
- Junit 单元测试
- Junit单元测试
- JUnit单元测试
- JUnit单元测试
- jUnit 单元测试
- Junit单元测试
- junit 单元测试
- 奇葩问题之非法字符---<200b>
- 修复数据库表方法
- C#获取多维数组的行数与列数
- 数据库连接模板
- C++ primer plus 练习10
- Junit 单元测试
- acm hdu p2185 确定比赛名次
- 【前端学习笔记】深入学习Javascript:EVENT总结
- 2015-多校5-MZL's chemistry
- [LeetCode]Container With Most Water
- 2015年工作中遇到的问题:11-20
- SSH 案例学习总结(一)
- 获取IPhone相册中图片的方法(包括获取所有图片)
- mybatis 输入映射和输出映射