LoginSignup
4
1

More than 5 years have passed since last update.

@SpringApplicationConfiguration -> @SpringBootTestに書き換えたときにハマったところ

Last updated at Posted at 2018-04-08

Spring5.0.xやSpring Boot2.0.xがリリースされていますが、今回は古いSpringのバージョンを用いて実装されているコードをリファクタリングした際にハマったところを書いていこうと思います。

やりたいこと

Spring1.4から@SpringApplicationConfigurationがdeprecatedになっているので、代替のアノテーションとして用意されている@SpringBootTestに置き換えたい。環境は、Spring Boot(1.4.8), PowerMock(1.6.5)

ハマったところ

公式ブログをみると、アノテーションを書き換えるだけでよさそうですが、テストを走らせようとすると、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

原因

PowerMockのv1.6.5以前を併用していると、PowerMockのバグでClassNotFoundExceptionが起きてしまうようです。参考issue1, 参考issue2

対応

1.6.6でバグ修正がされているので、Springのアノテーションの書き換えだけでなく、PowerMockのversionを1.6.6以降に指定するようにしましょう。

pom.xml
<dependencies>
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.6.6</version>
    <scope>test</scope>
  </dependency>
</dependencies>

参考

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1