Я хочу провести сквозной тест приложения весенней загрузки rest-api. Для этого я использую пружинный макет mvc. Но я не могу получить ответ 200, потому что остальные API использует специальный перехватчик безопасности для проверки токена в запросе. Вместо этого я продолжаю получать 401 в качестве ответа. Как включить проверку токена в моем тесте?
Я пробовал несколько настроек, включив @ContextConfiguration (classes = {WebMvcConfig.class}) в мой тестовый класс. WebMvcConfig - это класс конфигурации для регистрации перехватчика.
Это мой тестовый файл
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@SpringBootTest(classes = VeripalServiceApplication.class)
@TestPropertySource(locations="classpath:test.properties")
@Transactional
public class VeripalTextConfigurationTest {
@Autowired
private MockMvc mockMvc;
@Test
public void happpyPath_thenReturns200() throws Exception {
String jsonBody = "some json body";
String endPoint = "/end_point_to_my_api";
HttpHeaders headers = new HttpHeaders();
headers.add("token", "this_is_my_token");
headers.setContentType(aplication/json);
/** Hit the API */
mockMvc.perform(post(endPoint)
.headers(httpHeaders)
.content(jsonBody)
)
.andExpect(status().isOk()).andDo(print());
}
}
А это @ Конфигурация
@Configuration
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
private ConsumerService consumerService;
@Autowired
private EndpointService endpointService;
@Autowired
private ConsumerConfigurationService consumerConfigurationService;
@Autowired
private AccessLimitService accessLimitService;
@Autowired
private ConfigurationHistoryService configurationHistoryService;
@Autowired
private LimitCarryOverService limitCarryOverService;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new Interceptor(consumerService, endpointService, consumerConfigurationService, accessLimitService, configurationHistoryService, limitCarryOverService));
}
}
А это мой класс перехватчиков
public class Interceptor implements HandlerInterceptor {
// some code here ...
}