PoweMock集成Spring-test 测试静态方法 禁用字节码验证 -noverify -XX:-UseSplitVerifier

来源:互联网 发布:波士顿矩阵分析案例 编辑:程序博客网 时间:2024/06/03 15:57
  1. 问题 我们使用Spring-test的时候使用如下的代码
@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 {}
  • 1
  • 2
  • 3
  • 4
  • 5

然后测试类,直接继承这个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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在官方文档的例子中我们可以看到解决这个问题的办法啦! 
在测试的时候,在测试类上面加上如下的注解,是可以解决问题的

@RunWith(PowerMockRunner.class)@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)//这个和之前的RunWith具有相同的效果啦~感觉是可以使用了吧!@ContextConfiguration("classpath:/example-context.xml")@PrepareForTest(IdGenerator.class)

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"> 禁止我们的全局的初始 </beans>

 
阅读全文
0 0
原创粉丝点击