我已从Spring 4.2升级到Spring 5.0.5。升级后,我的junits失败,出现以下错误。
java.lang.AssertionError: Testing return value. expected:<405> but was:<200>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:645)
at
以下是我的代码:
`package com.hp.ci.mgmt.controllers;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
public class AbstractBaseControllerTest extends AbstractBaseController
{
ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
ObjectMapper mapper = new ObjectMapper();
private MockHttpServletRequest request;
private MockHttpServletResponse response;
@Mock
CiException cie;
@SuppressWarnings("rawtypes")
@Before
public void setup()
{
final List<HttpMessageConverter<?>> messageConvertersList = new ArrayList<HttpMessageConverter<?>>();
messageConvertersList.add(new MappingJackson2HttpMessageConverter());
resolver.setMessageConverters(messageConvertersList);
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
MockitoAnnotations.initMocks(this);
Mockito.when(cie.getErrorSource()).thenReturn("test");
}
@Test
public void testUnsupportedOperationException()
throws Exception
{
validateException(HttpStatus.METHOD_NOT_ALLOWED, new RequestNotAllowedException());
}
private void validateException(final HttpStatus expectedStatus, final
Exception e)
throws Exception
{
validateExceptionStatus(expectedStatus, e);
}
private void validateExceptionStatus(final HttpStatus expectedStatus, final Exception e)
throws Exception
{
resolver.resolveException(request, response, this, e);
if (expectedStatus == null)
{
assertEquals("Testing return value.", HttpStatus.BAD_REQUEST.value(), response.getStatus());
}
else
{
assertEquals("Testing return value.", expectedStatus.value(), response.getStatus());
}
}
问题是以下代码总是返回200状态代码,因此我的所有测试都失败了。
resolver.resolveException(request,response,this,e);
有谁知道如何解决这个问题?
答案 0 :(得分:0)
我找到了造成这个问题的原因。看起来下面一行中的“this”不会扩展HandlerMethod,因此resolveException返回null而不做任何事情。
resolver.resolveException(request,response,this,e);
@Override
protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object
handler) {
if (handler == null) {
return super.shouldApplyTo(request, null);
}
else if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
handler = handlerMethod.getBean();
return super.shouldApplyTo(request, handler);
}
else {
return false;
}
}
有人可以帮我配置处理程序。我该如何实现HandlerMethod?