PoweMock集成Spring-test 测试静态方法 禁用字节码验证 -noverify -XX:-UseSplitVerifier
2016-05-07 13:01
796 查看
问题 我们使用Spring-test的时候使用如下的代码
然后测试类,直接继承这个class就可以直接的跑起来我们的spring框架啦,这个是没得错的,但是有一点小的问题,我们在模拟PowerMock也是需要一个@RunWith(PowerMockRunner.class)这两个是不可以同时的存在的。@RunWith(SpringJUnit4ClassRunner.class) 这个在高版本的Powemok中,做了一些改进也是希望解决这个问题。我们先看看我们的PowerMock的配置吧!
在官方文档的例子中我们可以看到解决这个问题的办法啦!
在测试的时候,在测试类上面加上如下的注解,是可以解决问题的
/content/7873624.html
这里上面的链接是我们的基本的使用说明,关于PoweMock的。
看起来我们的问题似乎是解决啦,但是还是没有解决,新的问题又来啦。我们编译的时候,可能是我们引入的库的原因,产生字节码不符合java7的规范,报错!如下
http://stackoverflow.com/questions/15122890/java-lang-verifyerror-expecting-a-stackmap-frame-at-branch-target-jdk-1-7
这个是解决的方案
Java 7 introduced a stricter verification and changed the class format a bit – to contain a stack map, used to verify that code is correct. The exception you see, means that some method doesn’t have a valid stack map.
Java version or bytecode instrumentation could both be to blame. Usually this means that a library that the application uses, generates invalid bytecode that doesn’t pass the stricter verification. So nothing else than reporting it as a bug to the library can be done by the developer.
As a workaround you can add -noverify to the JVM arguments in order to disable verification. In Java 7 it was also possible to use -XX:-UseSplitVerifier to use the less strict verification method, but that option was removed in Java 8.
作为一种解决方法,你可以添加noverify的JVM参数来禁用验证。在java 7还可以使用XX:- usesplitverifier使用更严格的验证方法,但选择是在java 8中移除。
看起来还是顺利的解决啦问题,新的问题来啦,spring初始化的时候,引入的单例模式的初始化,加快加载的速度,报错啦,我们测试,没必要开启这么多的初始化的引入的工作。可以设置全局的禁止初始化。
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:springmvc-servlet-test.xml", "classpath:application-context-datasource-test.xml", "classpath:springbeans/mq-beans-test.xml","classpath:springbeans/ws-beans-test.xml","classpath:springbeans/task-beans-test.xml", "classpath:springbeans/threadpool-beans-test.xml"}) public class JUnitServiceBase extends AbstractJUnit4SpringContextTests { }
然后测试类,直接继承这个class就可以直接的跑起来我们的spring框架啦,这个是没得错的,但是有一点小的问题,我们在模拟PowerMock也是需要一个@RunWith(PowerMockRunner.class)这两个是不可以同时的存在的。@RunWith(SpringJUnit4ClassRunner.class) 这个在高版本的Powemok中,做了一些改进也是希望解决这个问题。我们先看看我们的PowerMock的配置吧!
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.6.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.6.5</version> <scope>test</scope> </dependency>
在官方文档的例子中我们可以看到解决这个问题的办法啦!
在测试的时候,在测试类上面加上如下的注解,是可以解决问题的
@RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) //这个和之前的RunWith具有相同的效果啦~感觉是可以使用了吧! @ContextConfiguration("classpath:/example-context.xml") @PrepareForTest(IdGenerator.class)
/content/7873624.html
这里上面的链接是我们的基本的使用说明,关于PoweMock的。
看起来我们的问题似乎是解决啦,但是还是没有解决,新的问题又来啦。我们编译的时候,可能是我们引入的库的原因,产生字节码不符合java7的规范,报错!如下
java.lang.VerifyError: Inconsistent stackmap frames at branch target 38 Exception Details: Locatio
http://stackoverflow.com/questions/15122890/java-lang-verifyerror-expecting-a-stackmap-frame-at-branch-target-jdk-1-7
这个是解决的方案
Java 7 introduced a stricter verification and changed the class format a bit – to contain a stack map, used to verify that code is correct. The exception you see, means that some method doesn’t have a valid stack map.
Java version or bytecode instrumentation could both be to blame. Usually this means that a library that the application uses, generates invalid bytecode that doesn’t pass the stricter verification. So nothing else than reporting it as a bug to the library can be done by the developer.
As a workaround you can add -noverify to the JVM arguments in order to disable verification. In Java 7 it was also possible to use -XX:-UseSplitVerifier to use the less strict verification method, but that option was removed in Java 8.
作为一种解决方法,你可以添加noverify的JVM参数来禁用验证。在java 7还可以使用XX:- usesplitverifier使用更严格的验证方法,但选择是在java 8中移除。
看起来还是顺利的解决啦问题,新的问题来啦,spring初始化的时候,引入的单例模式的初始化,加快加载的速度,报错啦,我们测试,没必要开启这么多的初始化的引入的工作。可以设置全局的禁止初始化。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" default-lazy-init="true"> 禁止我们的全局的初始 <!-- 注释扫描 --> <context:component-scan base-package="com.pms.**.action" /> <context:component-scan base-package="com.pms.**.service" /> <context:component-scan base-package="com.pms.**.dao" /> <context:component-scan base-package="com.pms.**.api" /> <!-- Tree接口 --> <context:component-scan base-package="com.pms.common.tree" /> <!-- 扫描 ref接口类 --> <context:component-scan base-package="com.pms.ref" /> <!-- 定义系统日志切面,必须定义在此配置文件中,否则spring mvc切面拦截功能会失效 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- <bean id="sysLogAspect" class="com.module.baseapp.common.service.SysLogAspect"/> --> <!-- 开启springmvc对象到json数据的转换功能 --> <mvc:annotation-driven> <mvc:argument-resolvers> <bean class="com.common.web.bind.ExtServletModelAttributeMethodProcessor"/> <bean class="com.cms.common.web.bind.ExtRequestBodyArgumentResolver"/> </mvc:argument-resolvers> </mvc:annotation-driven> <!-- 拦截器 配置多个将会顺序执行 --> <mvc:interceptors> <!-- <bean class="com.common.interceptor.DBLogInterceptor" /> --> <bean class="com.common.web.interceptor.HttpServletResponseInterceptor" /> <bean class="com..common.web.interceptor.ModelAndViewAttributesAutoSetInterceptor" /> </mvc:interceptors> <!-- 视图加载 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"/> <property name="suffix" value=".jsp" /> </bean> <!-- 统一异常处理 --> <bean id="exceptionResolver" class="com.common.web.resolver.CustomSimpleMappingExceptionResolver" /> <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8" /> <!-- 这里不限制最大文件尺寸,所以不需要捕捉超过文件大小的异常 --> <!-- <property name="maxUploadSize" value="102400000" /> --> </bean> </beans>
相关文章推荐
- Java课程“新”得(六)
- JAVA之旅(一)——基本常识,JAVA概念,开发工具,关键字/标识符,变量/常量,进制/进制转换,运算符,三元运算
- Java基础知识练习之数字操作
- JAVA之旅(一)——基本常识,JAVA概念,开发工具,关键字/标识符,变量/常量,进制/进制转换,运算符,三元运算
- java web路径和spring读取配置文件
- SpringMVC源代码学习外传(一)xml命名空间的解析NamespaceHandler
- java学习路线
- R.java文件介绍
- Ehcache 整合Spring 使用页面、对象缓存
- 设计模式--单例应用 java
- NDK开发中Eclipse报错“Unresolved inclusion jni.h”解决办法
- Eclipse中10个最有用的快捷键组合
- java内部类之成员内部类、静态内部类
- 在实践中理解Spring框架的依赖注入(DI)
- 一个奇妙的java坑:Long 类型的比较
- 一个奇妙的java坑:Long 类型的比较
- java反射详解
- (一)问候Struts2
- SpringMvc学习与使用
- 关于Java中Scanner对象的hasNext()方法对实现Readable接口的对象中的read()方法调用的探讨