[英]Spring MVC Controllers Unit Test not calling @ControllerAdvice
我在应用程序中有一组控制器和一个注释为@ControllerAdvice
的类,它设置了在每个控制器中使用的某些数据元素。 我正在使用Spring MVC 3.2
并为这些控制器安装了 Junit。 当我运行 Junit 时,控件不会转到ControllerAdvice
类,如果我在Tomcat
部署应用程序并通过浏览器提交请求,它可以正常工作。
请问有什么想法吗?
使用答案来自@尤金到和其他类似的一个后,在这里我找到了限制,并在春季提出的一个问题: https://jira.spring.io/browse/SPR-12751
因此,Spring 测试在 4.2 中引入了在构建器中注册@ControllerAdvice
类的功能。 如果您使用的是Spring Boot,那么您将需要 1.3.0 或更高版本。
通过这一改进,如果您使用独立设置,那么您可以像这样设置一个或多个ControllerAdvice
实例:
mockMvc = MockMvcBuilders.standaloneSetup(yourController)
.setControllerAdvice(new YourControllerAdvice())
.build();
注意:名称setControllerAdvice()
可能不会立即显而易见,但您可以将许多实例传递给它,因为它具有 var-args 签名。
假设您有用@ControllerAdvice 注释的MyControllerAdvice 类,该类具有用@ExceptionHandler 注释的方法。 对于 MockMvc,您可以轻松地将此类添加为异常解析器。
@Before
public void beforeTest() {
MockMvc mockMvc = standaloneSetup(myControllers)
.setHandlerExceptionResolvers(createExceptionResolver())
.build();
}
private ExceptionHandlerExceptionResolver createExceptionResolver() {
ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
Method method = new ExceptionHandlerMethodResolver(MyControllerAdvice.class).resolveMethod(exception);
return new ServletInvocableHandlerMethod(new MyControllerAdvice(), method);
}
};
exceptionResolver.afterPropertiesSet();
return exceptionResolver;
}
我在尝试测试用@ControllerAdvice
注释的ExceptionHandler
时遇到了类似的问题。 在我的情况下,我必须将带有@EnableWebMvc
注释的@Configuration
文件添加到测试类的@ContextConfiguration
。
所以我的测试是这样的:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {
RestProcessingExceptionHandler.class,
TestConfiguration.class,
RestProcessingExceptionThrowingController.class })
public class TestRestProcessingExceptionHandler {
private MockMvc mockMvc;
@Autowired
WebApplicationContext wac;
@Before
public void setup() {
mockMvc = webAppContextSetup(wac).build();
}
@Configuration
// !!! this is very important - conf with this annotation
// must be included in @ContextConfiguration
@EnableWebMvc
public static class TestConfiguration { }
@Controller
@RequestMapping("/tests")
public static class RestProcessingExceptionThrowingController {
@RequestMapping(value = "/exception", method = GET)
public @ResponseBody String find() {
throw new RestProcessingException("global_error_test");
}
}
@Test
public void testHandleException() throws Exception {
mockMvc.perform(get("/tests/exception"))
.andExpect(new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
result.getResponse().getContentAsString().contains("global_error_test");
}
})
.andExpect(status().isBadRequest());
}
}
使用@EnableWebMvc
配置我的测试通过了。
这段代码对我有用:
public class MyGlobalExceptionHandlerTest {
private MockMvc mockMvc;
@Mock
HealthController healthController;
@BeforeTest
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(healthController)
.setControllerAdvice(new GlobalExceptionHandler())
.build();
}
@Test(groups = { "services" })
public void testGlobalExceptionHandlerError() throws Exception {
Mockito.when(healthController.health())]
.thenThrow(new RuntimeException("Unexpected Exception"));
mockMvc.perform(get("/health")).andExpect(status().is(500));
}
}
很长一段时间以来,我一直在为同样的事情苦苦挣扎。 经过大量挖掘,最好的参考是 Spring 文档:
简而言之,如果您只是测试控制器及其方法,那么您可以使用“standaloneSetup”方法来创建一个简单的 Spring MVC 配置。 这不会包括您使用 @ControllerAdvice 注释的错误处理程序。
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new AccountController()).build();
}
// ...
要创建一个更完整的Spring MVC的配置不会包含您的错误处理程序,你应该使用以下设置:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("test-servlet-context.xml")
public class AccountTests {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Autowired
private AccountService accountService;
// ...
}
@tunguski 示例代码有效,但了解事情的工作原理是值得的。 这只是一种设置方式。
@EnableWebMvc
相当于在spring配置文件中跟随
<mvc:annotation-driven />
本质上,为了工作,您需要初始化 Spring Mvc 并加载所有控制器和 bean 引用。 所以以下可能是一个有效的设置以及替代
以下是您将如何设置测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath: "classpath:test-context.xml" })
@WebAppConfiguration
public class BaseTest {
@Autowired
WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
}
以下可能是测试的弹簧配置
<mvc:annotation-driven />
<context:component-scan base-package="com.base.package.controllers" />
我在用spock (groovy) 编写控制器测试时遇到了这个问题。 我的测试类最初是这样写的:
@AutoConfigureMockMvc(secure = false)
@SpringBootTest
@Category(RestTest)
class FooControllerTest extends Specification {
def fooService = Mock(FooService)
def underTest = new FooController(FooService)
def mockMvc = MockMvcBuilders.standaloneSetup(underTest).build()
....
}
这导致 ControllerAdvice 被忽略。 将代码更改为 Autowire 模拟解决了问题。
@AutoConfigureMockMvc(secure = false)
@SpringBootTest
@Category(RestTest)
class FooControllerTest extends Specification {
@AutowiredMock
FooService FooService
@Autowired
MockMvc mockMvc
ControllerAdvice
应该由@WebMvcTest
,到目前为止,我还可以参阅Spring-Doc Works。
例子:
@RunWith(SpringRunner.class)
@WebMvcTest(ProductViewController.class)
您需要提供更多信息,也许还有一些实际的代码和/或配置文件,然后才能得到具体的答案。 也就是说,根据您提供的一点信息,听起来好像没有加载带注释的 bean。
尝试将以下内容添加到您的测试 applicationContext.xml(或等效的 spring 配置文件,如果您正在使用)。
<context:component-scan base-package="com.example.path.to.package" />
或者,您可能需要通过在测试类之前包含以下注释来“手动”加载测试中的上下文:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
祝你好运!
我怀疑您需要在测试中使用 asyncDispatch; 常规测试框架被异步控制器破坏了。
尝试以下方法: https : //github.com/spring-projects/spring-framework/blob/master/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests .java
最简单的方法是将您的@ControllerAdvice 注释类添加到@ContextConfiguration。
我不得不改变这个
@AutoConfigureMockMvc
@ContextConfiguration(classes = OrderController.class)
@WebMvcTest
class OrdersIntegrationTest
对此:
@AutoConfigureMockMvc
@ContextConfiguration(classes = {OrderController.class, OrdersExceptionHandler.class})
@WebMvcTest
class OrdersIntegrationTest
我正在使用 Spring Boot 2.x,但似乎不再需要 MockMvcBuilders,或者当我们将 ControllerAdvice 定义为配置的一部分时,它会被加载。
@WebMvcTest
@ContextConfiguration(classes = {
UserEndpoint.class, //the controller class for test
WebConfiguration.class, //security configurations, if any
StandardRestExceptionInterpreter.class. //<-- this is the ControllerAdvice class
})
@WithMockUser(username = "test@asdf.com", authorities = {"DEFAULT"})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class UserEndpointTests {
@Test
@Order(3)
public void shouldThrowExceptionWhenRegisteringDuplicateUser() throws Exception {
//do setup...
Mockito.doThrow(EntityExistsException.class).when(this.userService).register(user);
this.mockMvc
.perform(MockMvcRequestBuilders
.post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content(this.objectMapper.writeValueAsString(user)))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isConflict());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.