我有一个基本的SpringBoot应用程序。使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行的JAR文件。
我有一个控制器:
@Controller
@RequestMapping("/deviceevent")
public class DeviceEventController {
@RequestMapping(value={ "/list"}, method = { RequestMethod.GET})
public String deviceeventList() {
return "tdk/deviceEvent/DeviceEventList";
}
}
和另一个测试类。使用Spring的MockMVC框架进行测试。这在测试中驱动MVC应用程序,就好像它在容器中运行一样,
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class MockMvcTests {
// Pull in the application context created by @ContextConfiguration
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
// Setup MockMVC to use our Spring Configuration
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void getDeviceEventsTest() throws Exception {
this.mockMvc
.perform(get("/deviceevent/list") //
.accept(MediaType.parseMediaType("text/html;charset=UTF-8")))
.andExpect(status().isOk()) //
.andExpect(model().size(1)) //
}
但我犯了一个错误:
java.lang.AssertionError: Status expected:<200> but was:<404>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:664)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
at com.tdk.iot.web.controllers.MockMvcTests.getDeviceEventsTest(MockMvcTests.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
请像这样尝试:
@RunWith(SpringRunner.class)
@WebMvcTest(WarehouseController.class)
public class WarehosueControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
WarehouseService warehouseService;//the business service
@Test
public void testGetWarehouses() throws Exception{
List<Long> list = new ArrayList<>();
list.add(1L);
WarehouseDTO warehouseDTO = new WarehouseDTO();
List<WarehouseDTO> warehouseDTOs = new ArrayList<>();
warehouseDTO.setId(1L);
warehouseDTO.setCode("test_code");
warehouseDTO.setName("test_name");
warehouseDTO.setAddress("adress value");
warehouseDTOs.add(warehouseDTO);
given(this.warehouseService.getWarehouses(0, list))
.willReturn(warehouseDTOs);
this.mvc.perform(get("/api/warehouses?filterType=0&ids=1")
.header("Accept-Language", "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
//check if the jason node exsits
.andExpect(jsonPath("$.errorCode").exists())
.andExpect(jsonPath("$.payload[0].name").exists())
.andExpect(jsonPath("$.payload[0].code").exists())
.andExpect(jsonPath("$.payload[0].id").exists())
//check the type of json node
.andExpect(jsonPath("$.errorCode").isString())
.andExpect(jsonPath("$.payload[0].name").isString())
.andExpect(jsonPath("$.payload[0].code").isString())
.andExpect(jsonPath("$.payload[0].id").isNumber())
//check the return value
.andExpect(jsonPath("$.errorCode").value(""))
.andExpect(jsonPath("$.payload[0].name").value("test_name"))
.andExpect(jsonPath("$.payload[0].code").value("test_code"))
.andExpect(jsonPath("$.payload[0].address").value("adress value"))
.andExpect(jsonPath("$.payload[0].id").value(1));
}
}
问题内容: 我编写了一个测试UsersController的单元测试。UsersControllerTest.findUser工作正常,但不能正常运行UsersControllerTest.insertGetModifyDelete。 在测试日志中,我可以看到POST请求与UsersController的任何方法都不匹配,但是我不明白为什么。您能帮我这个吗? 这是我其余的Java类: 我有2种方法
我需要jUnit测试的帮助,我的结果是json文件,我需要测试该文件的长度,问题是在jsonPath函数中没有这样的方法。 这里是我的测试方法,预期长度是7。如有任何帮助,我将不胜感激,谢谢
共享Api实现类,添加preauthorize-admin,查看所有用户 这是我的JUnit测试,我发送get请求并返回403错误。
我有一个简单控制器的设置: 对于这个简单的控制器,我试图使用MockMVC在Spock中编写一个测试: 我有的罐子是: Spring测试:4.0.5 Javax servlet api:3.0.1 spock Spring:0.7-groovy-2.0 运行测试后出现的错误如下:
有没有人有任何提示,或者有人知道如何测试HTTP响应对象返回的“错误消息”? 响应:
我在应用程序中有我的配置。类路径根中的yml文件(src/main/resources/)。当我正常启动应用程序时,配置加载良好。然而,在我的测试中,应用程序。yml文件根本没有加载。 我的测试的标题如下所示: 配置类: 当我调试应用程序时,我看到yml文件被加载到ConfigFileApplication ationListener中,但在测试中没有调用ConfigFileApplication