package com.bill99.junit;
public class ACase {
private String echoRequest(String request) {
return "Hello!"+request;
}
private String echoRequest() {
return "Hello!";
}
}
package com.bill99.junit;
import java.lang.reflect.Method;
import junit.framework.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 testNoParamEchoRequest() throws Exception {
//测试没有参数的echoRequest()方法
Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest", null);
//Method对象继承自java.lang.reflect.AccessibleObject,父类方法setAccessible可调
//将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查。
//要访问私有方法必须将accessible设置为true,否则抛java.lang.IllegalAccessException
testNoParamMethod.setAccessible(true);
//调用
Object result = testNoParamMethod.invoke(a, null);
System.out.println(result);
Assert.assertNotNull(result);
}
@Test
public void testParamEchoRequest() throws Exception {
//测试带有参数的echoRequest(String request)方法
Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest",String.class);
testNoParamMethod.setAccessible(true);
//调用
Object result = testNoParamMethod.invoke(a, "this is a test information");
System.out.println(result);
Assert.assertNotNull(result);
}
}
分享到:
相关推荐
利用java的反射,实现在junit中测试private函数
首先新建一个项目叫JUnit_Test,我们编写一个Calculator类,这是一个能够简单实现加减乘除、平方、开方的计算器类,然后对这些功能进行单元测试。这个类并不是很完美,我们故意保留了一些Bug用于演示,这些Bug在注释...
Acai 是 JUnit4 和 Guice 的测试库,可以更容易的编写应用功能测试。主要特性:注入测试需要的助手类启动测试需要的任意的服务 运行测试之间的服务清理按照正确顺序启动多个服务 创建测试作用域绑定Acai 主要针对的...
demo for Jax-rs with the Junit test #开发需求 1、实现RS服务 2、生成SWAGGER接口文档 3、实现接口单元测试示例 4、对接口访问过程进行日志记录 #实现方案 REST API实现方式 Spring Boot框架实现方法 1、创建资源...
Junit用的测试jar包可以测试private等方法
如果应该tests目录下的Android.mk文件中有LOCAL_SDK_VERSION := current这个配置,需要将该配置修改为LOCAL_PRIVATE_PLATFORM_APIS := true 3、 将应用tests目录下的AndroidManifest.xml文件中的 instrumentation ...
@RunWith(SpringJUnit4ClassRunner.class)//Spring的Junit测试,会在测试开始时,创建Spring的应用上下文 @ContextConfiguration(classes=CDPlayerSpringConfig.class)//表明配置类 public class SpringTest1 { //...
from within an initializer or constructor.GwtMockito 就很好的解决了这个问题,允许从 JUnit 测试中调用 GWT.create。返回 Mockito mocks。使用:public class MyWidget extends Composite { ...
该项目旨在简化在 JUnit 测试中集成 。 这是使用此模块的 Governator Junit 测试的外观: @RunWith ( GovernatorJunit4Runner . class) @LifecycleInjectorParams ( modules = SampleModule . class, ...
初级java笔试题示例 1 的评论 概要 ...private String name; public String sayHello (){ return ( " Hello " ); } public String sayGoodbye (){ return ( " Goodbye " ); } public String sa
* junit:开发人员测试用的 * 搭建hibernate层 * 定义需求:部门信息的页面数据要插入到数据库中 * 定义表 CREATE TABLE `sys_user_group` ( `id` INTEGER(11) NOT NULL AUTO_INCREMENT, #编号 ...
学校软件工程测试运动给出了一个类 Car(如下所列),每次扩展时都将其扩展为一个单独的类。 public class Car {private ... 这一切都应该用 JUnit 测试进行测试。目标练习的目标是学习一些在学校学到的程序模式的
1. 使用JUnit对代码进行单元测试 2. 部署代码到Tomcat服务器上 ## 总结 本文介绍了大学生毕业季实习工作第三方平台java代码开发的过程,通过环境配置、代码开发、测试和部署等步骤,最终实现了用户注册、登录、...
您可以像在Eclipse IDE上进行Junit测试一样在测试包中运行Private和Public测试文件(添加JUnit 4以构建路径之后,可以这样做:右键单击项目-> Build Path-> Configure Build Path-> select库选项卡->添加库...-> ...
使用在springboot项目中使用powermock-mockito进行单元测试,演示mock static、private、whenNew、exception的测试。 注意 示例使用spring-boot 2.0.3.RELEASE 它间接引用 junit-4.12 mockito-core-2.15.0 但是官网...
7.5.1 重写private方法 7.5.2 重写其他访问权限的方法 7.6 非静态内部类的陷阱 7.6.1 非静态内部类的构造器 7.6.2 非静态内部类不能拥有静态成员 7.6.3 非静态内部类的子类 7.7 static关键字 7.7.1 静态方法...
模拟实现__Spring的Ioc 1、Spring主要两个作用:实例化Bean,动态装配Bean。并将所有的bean放到spring容器中,调用时从容器中取。...使用第三方jar包有:jdom.jar(用于解析xml文件),junit-4.4.jar(用于测试)。
private static void getTest(String strURL, String saveFilePath) { // 构造HttpClient的实例 HttpClient httpClient = new HttpClient(); // 创建GET方法的实例 GetMethod getMethod = new GetMethod...
写一个单元测试类,命名方式:XxxTest(测试类没有main方法) 2. 导入包,Junit4包 选中项目,右键 => Build Path => Add Library => 选中Junit 选中Junit4 => finish 3. 在测试类中,设计测试方法,...