Clean up Mockito usage

This commit migrates to the MockitoJUnitRunner where sensible, which
will later allow for an easier migration to Mockito's extension for
JUnit Jupiter.

In addition, this commit deletes unnecessary stubbing for various mocks
and polishes test fixture setup in various test classes.
This commit is contained in:
Sam Brannen
2019-05-28 13:56:10 +02:00
parent d495902a9c
commit 141ef9082f
27 changed files with 170 additions and 290 deletions

View File

@@ -20,12 +20,12 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.converter.HttpMessageConverter;
@@ -55,10 +55,9 @@ import static org.mockito.Mockito.verify;
*
* @author Rossen Stoyanchev
*/
@RunWith(MockitoJUnitRunner.class)
public class DelegatingWebMvcConfigurationTests {
private DelegatingWebMvcConfiguration delegatingConfig;
@Mock
private WebMvcConfigurer webMvcConfigurer;
@@ -83,12 +82,7 @@ public class DelegatingWebMvcConfigurationTests {
@Captor
private ArgumentCaptor<List<HandlerExceptionResolver>> exceptionResolvers;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
delegatingConfig = new DelegatingWebMvcConfiguration();
}
private final DelegatingWebMvcConfiguration delegatingConfig = new DelegatingWebMvcConfiguration();
@Test
@@ -134,7 +128,6 @@ public class DelegatingWebMvcConfigurationTests {
converters.add(0, customConverter);
}
});
delegatingConfig = new DelegatingWebMvcConfiguration();
delegatingConfig.setConfigurers(configurers);
RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter(

View File

@@ -21,9 +21,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.test.MockHttpServletRequest;
@@ -34,34 +32,27 @@ import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.support.WebContentGenerator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Unit tests for
* {@link org.springframework.web.servlet.handler.HandlerMappingTests}.
* Unit tests for {@link org.springframework.web.servlet.HandlerMapping}.
* @author Brian Clozel
*/
public class HandlerMappingTests {
private MockHttpServletRequest request;
private AbstractHandlerMapping handlerMapping;
private StaticWebApplicationContext context;
private AbstractHandlerMapping handlerMapping = new TestHandlerMapping();
private StaticWebApplicationContext context = new StaticWebApplicationContext();
private MockHttpServletRequest request = new MockHttpServletRequest();
@Before
public void setup() {
this.context = new StaticWebApplicationContext();
this.handlerMapping = new TestHandlerMapping();
this.request = new MockHttpServletRequest();
}
@Test
public void orderedInterceptors() throws Exception {
HandlerInterceptor i1 = Mockito.mock(HandlerInterceptor.class);
HandlerInterceptor i1 = mock(HandlerInterceptor.class);
MappedInterceptor mappedInterceptor1 = new MappedInterceptor(new String[]{"/**"}, i1);
HandlerInterceptor i2 = Mockito.mock(HandlerInterceptor.class);
HandlerInterceptor i3 = Mockito.mock(HandlerInterceptor.class);
HandlerInterceptor i2 = mock(HandlerInterceptor.class);
HandlerInterceptor i3 = mock(HandlerInterceptor.class);
MappedInterceptor mappedInterceptor3 = new MappedInterceptor(new String[]{"/**"}, i3);
HandlerInterceptor i4 = Mockito.mock(HandlerInterceptor.class);
HandlerInterceptor i4 = mock(HandlerInterceptor.class);
this.handlerMapping.setInterceptors(mappedInterceptor1, i2, mappedInterceptor3, i4);
this.handlerMapping.setApplicationContext(this.context);

View File

@@ -18,11 +18,11 @@ package org.springframework.web.servlet.mvc.method.annotation;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.MediaType;
@@ -42,19 +42,13 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Rossen Stoyanchev
* @author Tomasz Nurkiewicz
*/
@RunWith(MockitoJUnitRunner.class)
public class ResponseBodyEmitterTests {
private ResponseBodyEmitter emitter;
@Mock
private ResponseBodyEmitter.Handler handler;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.emitter = new ResponseBodyEmitter();
}
private final ResponseBodyEmitter emitter = new ResponseBodyEmitter();
@Test

View File

@@ -22,9 +22,7 @@ import java.util.EnumSet;
import java.util.List;
import javax.servlet.ServletException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
@@ -62,6 +60,7 @@ import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Test fixture for {@link ResponseEntityExceptionHandler}.
@@ -70,26 +69,15 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class ResponseEntityExceptionHandlerTests {
private ResponseEntityExceptionHandler exceptionHandlerSupport;
private ResponseEntityExceptionHandler exceptionHandlerSupport = new ApplicationExceptionHandler();
private DefaultHandlerExceptionResolver defaultExceptionResolver;
private DefaultHandlerExceptionResolver defaultExceptionResolver = new DefaultHandlerExceptionResolver();
private WebRequest request;
private MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/");
private MockHttpServletRequest servletRequest;
private MockHttpServletResponse servletResponse = new MockHttpServletResponse();
private MockHttpServletResponse servletResponse;
@Before
public void setup() {
this.servletRequest = new MockHttpServletRequest("GET", "/");
this.servletResponse = new MockHttpServletResponse();
this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);
this.exceptionHandlerSupport = new ApplicationExceptionHandler();
this.defaultExceptionResolver = new DefaultHandlerExceptionResolver();
}
private WebRequest request = new ServletWebRequest(this.servletRequest, this.servletResponse);
@Test
@@ -179,7 +167,7 @@ public class ResponseEntityExceptionHandlerTests {
@Test
public void methodArgumentNotValid() {
Exception ex = Mockito.mock(MethodArgumentNotValidException.class);
Exception ex = mock(MethodArgumentNotValidException.class);
testException(ex);
}