![](/img/trans.png)
[英]java.lang.IllegalStateException: Failed to load 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.