[JAVA]在Junit中测试私有函数的方法(junit, private, method)
2005-11-20 18:33
465 查看
eclipse中如何写一个测试私有方法的junit?
假设类Summer定义如下:
public class Summer{
private int methodone(String argsone){
//method code
.......
return 4;
}
}
测试如下:
public class SummerTest extends TestCase {
public void testMethodone(){
Object rightResult = xxxxx;
Summer example = new Summer.newInstance();
Method m = example .getClass().getDeclaredMethod("methodone",new Class[]{String.class});
m.setAccessible(true);
Object result = m.invoke(example ,new Object[] {new String(xxxx)});
m.setAccessible(false);
if (result.equals(rightResult)){
//your code
......
}
}
}
假设类Summer定义如下:
public class Summer{
private int methodone(String argsone){
//method code
.......
return 4;
}
}
测试如下:
public class SummerTest extends TestCase {
public void testMethodone(){
Object rightResult = xxxxx;
Summer example = new Summer.newInstance();
Method m = example .getClass().getDeclaredMethod("methodone",new Class[]{String.class});
m.setAccessible(true);
Object result = m.invoke(example ,new Object[] {new String(xxxx)});
m.setAccessible(false);
if (result.equals(rightResult)){
//your code
......
}
}
}
相关文章推荐
- java反射运用举例-junit测试私有方法
- Junit中测试私有函数的方法
- [java]junit测试private方法
- Junit测试private方法
- JUnit 3.8 通过反射测试私有方法
- Junit3.8 私有方法测试
- JAVA学习7_Eclipse操作_生成方法、接口、Junit测试类_定制视图、New选项_Tomcat服务器_Jar包
- 测试 Java 类的private和protected变量和方法
- 执行Android JUnit测试出现java.net.SocketException: Permission denied的解决方法
- [php] Interface abstract里面的私有方法 -- private method of interface and abstract class
- Junit3.8 私有方法测试
- java利用反射访问类的私有(private)属性及方法
- 如何测试 java 中的私有方法
- java测试private方法
- JUnit 3.8 通过反射测试私有方法
- 根据反射机制测试private方法(junit测试)
- Junit中对私有方法测试
- 如何测试私有 Private/Internal 方法
- Java中反射调用私有方法出现NoSuchMethodException
- 为什么使用Junit Test而不用普通java main方法来完成测试?