Merge pull request #5602 from shreyasm/BAEL-2201
BAEL-2201-shreyas-Replacing customer written MockitoExtension class with Mockito's own class
This commit is contained in:
commit
4df7e74fef
|
@ -72,6 +72,13 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>${mockito.junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
|
@ -118,13 +125,13 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<junit.jupiter.version>5.2.0</junit.jupiter.version>
|
||||
<junit.jupiter.version>5.3.1</junit.jupiter.version>
|
||||
<mockito.junit.jupiter.version>2.23.0</mockito.junit.jupiter.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<log4j2.version>2.8.2</log4j2.version>
|
||||
<h2.version>1.4.196</h2.version>
|
||||
<mockito.version>2.8.9</mockito.version>
|
||||
<powermock.version>1.7.4</powermock.version>
|
||||
<powermock.version>2.0.0-RC.1</powermock.version>
|
||||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||
<spring.version>5.0.1.RELEASE</spring.version>
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials are
|
||||
* made available under the terms of the Eclipse Public License v1.0 which
|
||||
* accompanies this distribution and is available at
|
||||
*
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
|
||||
package com.baeldung.junit5.mockito;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext.Store;
|
||||
import org.junit.jupiter.api.extension.ParameterContext;
|
||||
import org.junit.jupiter.api.extension.ParameterResolver;
|
||||
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Mock;
|
||||
|
||||
/**
|
||||
* {@code MockitoExtension} showcases the {@link TestInstancePostProcessor}
|
||||
* and {@link ParameterResolver} extension APIs of JUnit 5 by providing
|
||||
* dependency injection support at the field level and at the method parameter
|
||||
* level via Mockito 2.x's {@link Mock @Mock} annotation.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
public class MockitoExtension implements TestInstancePostProcessor, ParameterResolver {
|
||||
|
||||
@Override
|
||||
public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
|
||||
MockitoAnnotations.initMocks(testInstance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
|
||||
return parameterContext.getParameter().isAnnotationPresent(Mock.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
|
||||
return getMock(parameterContext.getParameter(), extensionContext);
|
||||
}
|
||||
|
||||
private Object getMock(Parameter parameter, ExtensionContext extensionContext) {
|
||||
Class<?> mockType = parameter.getType();
|
||||
Store mocks = extensionContext.getStore(Namespace.create(MockitoExtension.class, mockType));
|
||||
String mockName = getMockName(parameter);
|
||||
|
||||
if (mockName != null) {
|
||||
return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
|
||||
}
|
||||
else {
|
||||
return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
|
||||
}
|
||||
}
|
||||
|
||||
private String getMockName(Parameter parameter) {
|
||||
String explicitMockName = parameter.getAnnotation(Mock.class).name().trim();
|
||||
if (!explicitMockName.isEmpty()) {
|
||||
return explicitMockName;
|
||||
}
|
||||
else if (parameter.isNamePresent()) {
|
||||
return parameter.getName();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -3,9 +3,7 @@ package com.baeldung.junit5.mockito;
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
@ -16,6 +14,7 @@ import org.junit.runner.RunWith;
|
|||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import com.baeldung.junit5.mockito.repository.MailClient;
|
||||
|
@ -25,21 +24,24 @@ import com.baeldung.junit5.mockito.service.DefaultUserService;
|
|||
import com.baeldung.junit5.mockito.service.Errors;
|
||||
import com.baeldung.junit5.mockito.service.UserService;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class UserServiceUnitTest {
|
||||
|
||||
UserService userService;
|
||||
SettingRepository settingRepository;
|
||||
@Mock UserRepository userRepository;
|
||||
|
||||
@Mock MailClient mailClient;
|
||||
|
||||
User user;
|
||||
|
||||
@BeforeEach
|
||||
void init(@Mock SettingRepository settingRepository, @Mock MailClient mailClient) {
|
||||
void init(@Mock SettingRepository settingRepository) {
|
||||
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
||||
when(settingRepository.getUserMinAge()).thenReturn(10);
|
||||
lenient().when(settingRepository.getUserMinAge()).thenReturn(10);
|
||||
when(settingRepository.getUserNameMinLength()).thenReturn(4);
|
||||
when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false);
|
||||
lenient().when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false);
|
||||
this.settingRepository = settingRepository;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -56,7 +58,9 @@ public class UserServiceUnitTest {
|
|||
return user;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
||||
|
||||
// When
|
||||
User insertedUser = userService.register(user);
|
||||
|
||||
|
|
Loading…
Reference in New Issue