为了激活完整的Spring MVC配置,您需要使用 代替 .MockMvcBuilders.webAppContextSetup
MockMvcBuilders.standaloneSetup
查看Spring文档的这一部分以获取更多详细信息。
您的代码将如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("test-config.xml")
public class ClientQueriesControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private AuthenticationService authenticationService;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void findAllAccountRelatedClientsUnauthorized() throws Exception {
when(authenticationService.validateAuthorization(anyString())).thenThrow(AuthenticationException.class);
mockMvc.perform(get("/rest/clients").header("Authorization", UUID.randomUUID().toString()))
.andExpect(status().isUnauthorized());
}
}
然后在里面你会添加一个春天的豆子,因为这是一个模拟。test-config.xml
AuthenticationService
<bean id="authenticationService" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="your.package.structure.AuthenticationService"/>
</bean>
当然,如果您想重用常规的Spring配置文件而不是创建,则可以使用配置文件在测试中注入模拟。AuthenticationService
test-config.xml
更新
经过一番挖掘,我发现返回的()是完全可定制的。这意味着您可以插入您喜欢的任何异常解析器。StandaloneMockMvcBuilder
MockMvcBuilders.standaloneSetup
但是,由于您正在使用 ,下面的代码将不起作用。但是,如果您的方法位于同一控制器中,则只需更改以下代码:@ControllerAdvice
@ExceptionHandler
mockMvc = MockMvcBuilders.standaloneSetup(controller).setHandlerExceptionResolvers(new ExceptionHandlerExceptionResolver()).build();
更新 2
更多的挖掘给出了如何在使用 时注册正确的异常处理程序的答案。@ControllerAdvice
您需要将测试中的设置代码更新为以下内容:
@Before
public void setUp() throws Exception {
final ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver();
//here we need to setup a dummy application context that only registers the GlobalControllerExceptionHandler
final StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerBeanDefinition("advice", new RootBeanDefinition(GlobalControllerExceptionHandler.class, null, null));
//set the application context of the resolver to the dummy application context we just created
exceptionHandlerExceptionResolver.setApplicationContext(applicationContext);
//needed in order to force the exception resolver to update it's internal caches
exceptionHandlerExceptionResolver.afterPropertiesSet();
mockMvc = MockMvcBuilders.standaloneSetup(controller).setHandlerExceptionResolvers(exceptionHandlerExceptionResolver).build();
}