BAEL-2201-shreyas-Replacing customer written MockitoExtension class with mockito's own
This commit is contained in:
parent
18d7252bdf
commit
adacecbd75
|
@ -72,6 +72,13 @@
|
||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-junit-jupiter</artifactId>
|
||||||
|
<version>${mockito.junit.jupiter.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.powermock</groupId>
|
<groupId>org.powermock</groupId>
|
||||||
<artifactId>powermock-api-mockito2</artifactId>
|
<artifactId>powermock-api-mockito2</artifactId>
|
||||||
|
@ -118,13 +125,13 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<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.platform.version>1.2.0</junit.platform.version>
|
||||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||||
<log4j2.version>2.8.2</log4j2.version>
|
<log4j2.version>2.8.2</log4j2.version>
|
||||||
<h2.version>1.4.196</h2.version>
|
<h2.version>1.4.196</h2.version>
|
||||||
<mockito.version>2.8.9</mockito.version>
|
<powermock.version>2.0.0-RC.1</powermock.version>
|
||||||
<powermock.version>1.7.4</powermock.version>
|
|
||||||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||||
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||||
<spring.version>5.0.1.RELEASE</spring.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,19 +3,16 @@ package com.baeldung.junit5.mockito;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.junit.platform.runner.JUnitPlatform;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import org.mockito.stubbing.Answer;
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
import com.baeldung.junit5.mockito.repository.MailClient;
|
import com.baeldung.junit5.mockito.repository.MailClient;
|
||||||
|
@ -25,25 +22,25 @@ import com.baeldung.junit5.mockito.service.DefaultUserService;
|
||||||
import com.baeldung.junit5.mockito.service.Errors;
|
import com.baeldung.junit5.mockito.service.Errors;
|
||||||
import com.baeldung.junit5.mockito.service.UserService;
|
import com.baeldung.junit5.mockito.service.UserService;
|
||||||
|
|
||||||
@RunWith(JUnitPlatform.class)
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
public class UserServiceUnitTest {
|
public class UserServiceUnitTest {
|
||||||
|
|
||||||
UserService userService;
|
UserService userService;
|
||||||
@Mock UserRepository userRepository;
|
@Mock UserRepository userRepository;
|
||||||
|
@Mock MailClient mailClient;
|
||||||
|
|
||||||
User user;
|
User user;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void init(@Mock SettingRepository settingRepository, @Mock MailClient mailClient) {
|
void init(@Mock SettingRepository settingRepository) {
|
||||||
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
||||||
when(settingRepository.getUserMinAge()).thenReturn(10);
|
lenient().when(settingRepository.getUserMinAge()).thenReturn(10);
|
||||||
when(settingRepository.getUserNameMinLength()).thenReturn(4);
|
when(settingRepository.getUserNameMinLength()).thenReturn(4);
|
||||||
when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false);
|
lenient().when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) {
|
void givenValidUser_whenSaveUser_thenSucceed() {
|
||||||
// Given
|
// Given
|
||||||
user = new User("Jerry", 12);
|
user = new User("Jerry", 12);
|
||||||
when(userRepository.insert(any(User.class))).then(new Answer<User>() {
|
when(userRepository.insert(any(User.class))).then(new Answer<User>() {
|
||||||
|
|
Loading…
Reference in New Issue