5

Currently I test the new version of springboot 2. I created a @controller and the associated test by following the doc provided. But when I run the test, I always get the following exception:

2018-04-30 21:02:49.536  WARN 13584 --- [           main] o.s.test.context.TestContextManager      : Caught exception while invoking 'afterTestMethod' callback on TestExecutionListener [org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@1e81f160] for test method [public void com.example.demo.DemoApplicationTests.contextLoads()] and test instance [com.example.demo.DemoApplicationTests@1672fe87]

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker
    at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:60) ~[mockito-core-2.15.0.jar:na]
    at com.sun.proxy.$Proxy72.getHandler(Unknown Source) ~[na:na]
    at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:81) ~[mockito-core-2.15.0.jar:na]
    at org.mockito.internal.util.DefaultMockingDetails.isMock(DefaultMockingDetails.java:35) ~[mockito-core-2.15.0.jar:na]
    at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:106) ~[spring-boot-test-2.0.1.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:84) ~[spring-boot-test-2.0.1.RELEASE.jar:2.0.1.RELEASE]
Caused by: java.lang.NoClassDefFoundError: net/bytebuddy/dynamic/loading/ClassLoadingStrategy
    at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.<init>(SubclassByteBuddyMockMaker.java:33) ~[mockito-core-2.15.0.jar:na]
    at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.<init>(ByteBuddyMockMaker.java:21) ~[mockito-core-2.15.0.jar:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488) ~[na:na]
    at java.base/java.lang.Class.newInstance(Class.java:558) ~[na:na]
    ... 19 common frames omitted
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.dynamic.loading.ClassLoadingStrategy
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496) ~[na:na]
    ... 40 common frames omitted

2018-04-30 21:02:49.544  INFO 13584 --- [       Thread-2] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@c5ee75e: startup date [Mon Apr 30 21:02:47 CEST 2018]; root of context hierarchy

Here is the unit test :

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class DemoApplicationTests {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void contextLoads() {
    final String body = restTemplate.getForObject("/hello", String.class);
    assertThat(body).isEqualTo("Hello World");
    }
}

And here is my pom.xml

ject>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>9</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

I followed the doc, but nothing to do the problem persists Who would have a solution?

Thank you

Régis Le Coz
  • 71
  • 1
  • 3
  • [This comment](https://github.com/mockito/mockito/issues/1017#issuecomment-290811442) from the mockito repo pointed out it could be a corrupt `.m2` repo. Clearing out mine fixed this problem. – Lucas Jun 16 '18 at 20:38
  • @Lucas do you mean deleting the .m2 directory??? – Ara Kokeba May 21 '19 at 18:24

0 Answers0