簡體   English   中英

java.lang.IllegalStateException:在運行 gradle 清理構建時無法加載 ApplicationContext

[英]java.lang.IllegalStateException: Failed to load ApplicationContext While running gradle clean build

我有 2 個測試文件,但每當我嘗試運行gradle clean build時,

我收到java.lang.IllegalStateException: Failed to load ApplicationContext ,當我刪除@AutoConfigureMockMvc時,我收到一個錯誤,無法自動裝配。 找不到“MockMvc”類型的 bean。

第一個文件作業測試

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class JobTest {
   @Autowired
    private WebApplicationContext applicationContext;

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private JobService jobService;

    private static final String URI = "/testJob/";

    @BeforeEach
    void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
    }

    private final UUID jobId = UUID.fromString("d35089c0-8ca8-4a9d-8932-8464e9a0736c");
    
    @Test
    public void testRequestJob() throws Exception {

        //create a request object
        RequestBuilder requestBuilder = MockMvcRequestBuilders.post(URI)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.ALL)
                .content("testRequestString");

        when(jobService.neededJob(anyString()).thenReturn(mockJob);

        ResultActions perform = mockMvc.perform(requestBuilder);
        assertEquals(HttpStatus.OK.value(), perform.andReturn().getResponse().getStatus());

        //perform the request and get the response
        perform.andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                .andExpect(jsonPath("$.data.jobId").exists());

    }
 
}

2nd File EmployerTest

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ShiftControllerTest {
   @Autowired
    private WebApplicationContext applicationContext;

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private EmployerService employerService;

    @BeforeEach
    void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
    }

    private final UUID jobId = UUID.fromString("d35089c0-8ca8-4a9d-8932-8464e9a0736c");

    private static final String URI = "/employer/";

    @Test
    public void testEmployer() throws Exception {

        RequestBuilder requestBuilder = MockMvcRequestBuilders.get(URI + jobId)
                .accept(MediaType.ALL);
        when(employerService.getEmployer(jobId)).thenReturn(mockEmployer);

        ResultActions perform = mockMvc.perform(requestBuilder);
        assertEquals(HttpStatus.OK.value(), perform.andReturn().getResponse().getStatus());
    }
}

如果我評論一個文件然后嘗試運行gradle clean build它可以正常工作,任何建議都將不勝感激。

在您發布的代碼中,除了創建一個MockMvc object 之外,您似乎沒有將WebApplicationContext用於其他任何事情,您已經通過@Autowired注釋創建了它。 如果您不需要它來做其他任何事情,請嘗試刪除WebApplicationContext 例如:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class JobTest {
    // @Autowired
    // private WebApplicationContext applicationContext;

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private JobService jobService;

    private static final String URI = "/testJob/";

    // @BeforeEach
    // void setup() {
    //     this.mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
    // }

    private final UUID jobId = UUID.fromString("d35089c0-8ca8-4a9d-8932-8464e9a0736c");
    
    @Test
    public void testRequestJob() throws Exception {

        //create a request object
        RequestBuilder requestBuilder = MockMvcRequestBuilders.post(URI)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.ALL)
                .content("testRequestString");

        when(jobService.neededJob(anyString()).thenReturn(mockJob);

        ResultActions perform = mockMvc.perform(requestBuilder);
        assertEquals(HttpStatus.OK.value(), perform.andReturn().getResponse().getStatus());

        //perform the request and get the response
        perform.andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                .andExpect(jsonPath("$.data.jobId").exists());

    }
 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM