Spring 5.0.x and Spring Boot 2.0.x have been released, but this time I will write what I was addicted to when refactoring the code implemented using the old Spring version.
Since @SpringApplicationConfiguration has been deprecated since Spring 1.4, I would like to replace it with @SpringBootTest, which is provided as an alternative annotation. The environment is Spring Boot (1.4.8), PowerMock (1.6.5)
Looking at the Official blog, it seems that you can just rewrite the annotation, but the test When I try to run it, I get a ClassNotFoundException.
TestSample.java
@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
When using PowerMock v1.6.5 or earlier together, it seems that a ClassNotFoundException occurs due to a bug in PowerMock. Reference issue 1, Reference issue 2
Since the bug has been fixed in 1.6.6, let's not only rewrite the Spring annotation but also specify the PowerMock version to 1.6.6 or later.
pom.xml
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
</dependencies>
Recommended Posts