Damit die vollständige Spring MVC-Konfiguration aktiviert wird, müssen Sie MockMvcBuilders.webAppContextSetup
anstelle von MockMvcBuilders.standaloneSetup
verwenden.
Weitere Informationen finden Sie in der Dokumentation zum Frühling unter der Nummer this.
Ihr Code würde wie folgt aussehen:
@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());
}
}
Dann innen test-config.xml
Sie einen Spring-Bean für AuthenticationService
hinzufügen würden, dass ein Modell ist.
<bean id="authenticationService" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="your.package.structure.AuthenticationService"/>
</bean>
Sie könnten natürlich Nutzungsprofile, die Mock AuthenticationService
in den Tests injizieren, wenn Sie möchten, Ihre regelmäßige Spring-Konfigurationsdatei wieder zu verwenden anstatt test-config.xml
zu schaffen.
UPDATE
Nachdem sich ein wenig zu graben, fand ich, dass StandaloneMockMvcBuilder
von (MockMvcBuilders.standaloneSetup
) zurück total kundengerecht. Das bedeutet, dass Sie den von Ihnen bevorzugten Ausnahme-Resolver verwenden können.
Da Sie jedoch @ControllerAdvice
verwenden, wird der folgende Code nicht funktionieren. Wenn jedoch Ihre @ExceptionHandler
Methode in demselben Controller den gesamten Code war würden Sie ändern müssen, ist die folgende:
mockMvc = MockMvcBuilders.standaloneSetup(controller).setHandlerExceptionResolvers(new ExceptionHandlerExceptionResolver()).build();
UPDATE 2
mehr Einige, um die Antwort gab graben, wie Sie kann einen korrekten Ausnahmebehandler registrieren, wenn Sie auch @ControllerAdvice
verwenden.
benötigen Sie den Setup-Code in dem Test auf den folgende Aktualisierung:
@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();
}
Haben Sie die Option 'MockMvcBuilders.webAppContextSetup' versucht, wie unter http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/testing.html#spring-mvc-test- beschrieben. Framework anstelle von 'MockMvcBuilders.standaloneSetup'? – geoand