Reputation: 18123
I am trying to mock the final class
which is available in our company's internal library using Mockito 2.18.3 framework, unfortunately we don't have access to change the code in the library. But whenever I run I get below error:
java.lang.NoClassDefFoundError: Could not initialize class org.mockito.Mockito
at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:107)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:69)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:55)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.afterTestMethod(ResetMocksTestExecutionListener.java:50)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:319)
This is my dependency:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.18.3</version>
<scope>test</scope>
</dependency>
This is the test class:
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:application-test.properties")
@SpringBootTest
public class JwtTokenTest {
@Autowired
private class JwtValidatorService jwtValidatorService;
@Mock
private JwtTokenDetails jwtTokenDetails;
@Test
public void jwtGenerateTest() {
//Code to test JWT generation
}
}
Also as per this link: https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#unmockable I have created org.mockito.plugins.MockMaker
file with contents: mock-maker-inline
.
I tried searching in other Stackoverflow posts and Google, but still not solution. Can anyone kindly help me in this? Looks like I am missing something, but failed to identify it. Since I don't have much expertise in Mockito, tried to use powermock but it is posing different challenges in downloading dependencies in company's network.
Please let me know if I need to add more code or more details.
Upvotes: 1
Views: 8518
Reputation: 396
Spring Boot uses Mockito 1.x by default. However, it's possible to override it with Mockito 2.x, as Spring Boot can also use it without any problems (see this commit).
To do so, just add this modification to your POM file properties:
<properties>
<mockito.version>2.18.3</mockito.version>
</properties>
Upvotes: 0
Reputation: 116031
Spring Boot 1.5.11 is compatible with Mockito 1.x. Specifically, it uses 1.10.19. Rather than overriding the version of Mockito to a new major version, you should let Spring Boot's dependency management specify the version. Doing so will ensure that you use a compatible version. If Mockito 1.10 doesn't meet your needs, you'll need to find an alternative solution.
Upvotes: 1