当将@SpringApplicationConfiguration改为@SpringBootTest时我遇到了问题
Spring5.0.x和Spring Boot2.0.x已经发布,但在这次重构使用旧版Spring的代码时,我想写下一些遇到的困难。
想做的事情
由于Spring1.4中的@SpringApplicationConfiguration被弃用,我想将其替换为备用注解@SpringBootTest。环境:Spring Boot (1.4.8)、PowerMock (1.6.5)。
迷上了圈套
看了公式博客后,似乎只需要更改注释,但是试图运行测试时,却报ClassNotFoundException错误。
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@PrepareForTest(Hoge.class)
public class TestSample
{
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(Hoge.class);
}
...
}
java.lang.ClassNotFoundException: com.example.PowermockClassLoadTest$Config$$EnhancerBySpringCGLIB$$e0ecd163
The reason why
据提供的参考问题,如果同时使用PowerMock的v1.6.5之前版本,可能会导致PowerMock的一个bug引发ClassNotFoundException异常。
應對
由于在1.6.6版本中进行了错误修复,因此我们需要对Spring的注解进行修改,并且还需指定PowerMock的版本为1.6.6或更高。
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
</dependencies>
请你提供一个中文的句子或短语,以便我能够为您做出改写。
-
- https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
-
- https://github.com/spring-projects/spring-boot/issues/6808
- https://github.com/powermock/powermock/issues/695