2

Can someone point me to what could be wrong in below code. It is a boot spring 2.6.7 application. When test profile is running, it throws error for all tests like below.

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

@AutoConfigureMockMvc
@SpringBootTest(classes = some.class)
@ActiveProfiles("test")

public class someTest {

    @Autowired
    private MockMvc mvc;

    @Autowired
    private WebApplicationContext webAppContext;

    @MockBean
    private SomeBean someBean;

    @SpyBean
    private SomeSpyBean someSpyBean;

    @BeforeEach
    public void setup() {
        mvc = MockMvcBuilders
        .webAppContextSetup(webAppContext)
        .apply(springSecurity())
        .build();
    }

    @Test
    public void SomeTest1() throws Exception {
        String text = "text1";
        when(someBean.findStuff(text).thenReturn(Optional.of(new Thingie()));
        mvc.perform(multipart("/api/somepath/")
        .andExpect(status().isNotFound());

        verify(someSpyBean).doStuff();
    }

    @Test
    public void SomeTest2() throws Exception {
        String text = "text2";
        when(someBean.findStuff(text).thenReturn(Optional.of(new Thingie()));
        mvc.perform(multipart("/api/somepath/")
        .andExpect(status().isFound());

        verify(someSpyBean).doStuff();
    }
}
mantisghost
  • 113
  • 7
  • Can annotate the class with @RunWith(SpringRunner.class) and run it again? – Reg May 05 '22 at 15:23
  • @Reg I did try to add @RunWith(SpringRunner.class it did not fix the issue, also I think it is obsolete, correct me if I'm wrong. – mantisghost May 06 '22 at 06:38
  • see https://stackoverflow.com/questions/58901288/springrunner-vs-springboottest, depends on the version of Junit you are running. – Reg May 06 '22 at 07:05

0 Answers0