BAEL-2201-shreyas-Replacing customer written MockitoExtension class with mockito's own

This commit is contained in:
Shreyas Mahajan 2018-11-02 11:54:23 +05:30
parent 18d7252bdf
commit adacecbd75
3 changed files with 18 additions and 89 deletions

View File

@ -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>

View File

@ -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;
}
}

View File

@ -3,19 +3,16 @@ 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;
import org.junit.jupiter.api.Test;
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.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,25 +22,25 @@ 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)
public class UserServiceUnitTest {
UserService userService;
@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);
}
@Test
void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) {
void givenValidUser_whenSaveUser_thenSucceed() {
// Given
user = new User("Jerry", 12);
when(userRepository.insert(any(User.class))).then(new Answer<User>() {