私はSpring Boot 1.4.0.M2で作業しており、Springセキュリティを備えたコントローラーが正しく機能することを保証するために次のテストケースを書きました。
@RunWith(SpringRunner.class)
@WebMvcTest(HelloController.class)
public class HelloControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.alwaysDo(print())
.apply(springSecurity())
.build();
}
@Test
public void getHelloShouldReturnWithCacheControlSet() throws Exception {
this.mockMvc.perform(get("/hello")
.accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk())
.andExpect(content().string("Hello World!"))
.andExpect(header().stringValues("Cache-Control", "max-age=5"));
}
}
テストを実行すると、次の例外がスローされます。
java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
他のすべてのコードはここにあります。 https://github.com/renewinkler84/http-cache-demo
なぜこの例外が投げられるのですか?他に何を設定する必要がありますか?
回答:
回答№1は6私はあなたが恋しいと思います @SpringBootTest
これを変更する
@WebMvcTest(HelloController.class)
これとともに
@SpringBootTest