vendredi 27 décembre 2019

@WebMvcTest keeps attempting to start ApplicationContext

Isn't the whole point of using @WebMvcTest as opposed to @SpringBootTest to avoid the running of the ApplicationContext? I'm not sure why my test keeps attempting to start it.

MY TEST

class BoardControllerIntegrationTest {

    @MockBean
    BoardServiceImpl boardService;

    @Autowired
    MockMvc mockMvc;

    Board validBoard;

    @BeforeEach
    void setUp() {
        validBoard = Board.builder()
                .setBoardName("Test Board 1")
                .setBoardId(UUID.randomUUID())
                .setRetroType("madSadGlad")
                .setDateCreated(LocalDate.now())
                .build();
    }

    @AfterEach
    void tearDown() {
        reset(boardService);
    }

    @Test
    void testGetBoardByUUID() throws Exception {
        Optional<Board> boardOptional = Optional.of(validBoard);
        given(boardService.findById(any())).willReturn(boardOptional);

        mockMvc.perform(get("/board/" + validBoard.getBoardUUID()))
                .andExpect(status().isOk());
    }


}

THE EXCEPTION

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.postProcessFields(MockitoTestExecutionListener.java:95)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.injectFields(MockitoTestExecutionListener.java:79)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:54)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)

Can anyone shed any light on this?

Aucun commentaire:

Enregistrer un commentaire