我正在尝试在Spring-Boot应用程序中对RestController进行测试。
我要测试的方法是依靠依靠蒙古人的服务的crud方法(插入)。
我很喜欢下面做过,但是我无法弄清楚为什么在网络上进行了许多研究之后测试。
。@RunWith(SpringRunner.class)
@WebMvcTest(value = ConfigTagController.class, secure = false)
public class ConfigTagControllerTest {
@Autowired
MockMvc mvc;
@MockBean
ConfigTagService service;
//dati mockati
private ConfigTagEntity mockTag = new ConfigTagEntity("test",new ArrayList<>());
private ConfigTagEntity mockTag1 = new ConfigTagEntity("test1",new ArrayList<>());
private Collection<ConfigTagEntity> mockTagCollection = Arrays.asList(mockTag,mockTag1);
//baseurl del controller
private String baseUrl = "/v1/configtag";
@Test
public void insertTest() throws Exception {
//Jackson library
ObjectMapper mapper = new ObjectMapper();
//Object to JSON in String
String jsonInString = mapper.writeValueAsString(mockTag);
//stub del metodo
Mockito.when(service.insert(anyObject())).thenReturn(true);
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post(baseUrl);
request.param(jsonInString);
System.out.println(request);
this.mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
}
当我尝试运行测试时,我会得到此错误
java.lang.IllegalArgumentException: 'values' must not be empty
at org.springframework.util.Assert.notEmpty(Assert.java:228).......stacktrace
这是服务方法:
public boolean insert(ConfigTagEntity entity) {
if (configTagRepository.findByTagName(entity.getTagName()) == null) {
/* ... */
return true;
} else {
return false;
}
}
这是控制器方法
@Override
@RequestMapping(value = "/configtag",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> insert(@RequestBody ConfigTagEntity entity) {
return checkCall(configTagService.insert(entity));
}
编辑堆栈跟踪
java.lang.IllegalArgumentException: 'values' must not be empty
at org.springframework.util.Assert.notEmpty(Assert.java:228)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.addToMultiValueMap(MockHttpServletRequestBuilder.java:773)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.param(MockHttpServletRequestBuilder.java:329)
at com.pcsystem.controller.ConfigTagControllerTest.insertTest(ConfigTagControllerTest.java:114)
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.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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
您将JSON有效载荷作为查询参数传递。
所以而不是
request.param(jsonInString);
您应该尝试
request.content(jsonInString);
您还需要添加内容类型标头
request.contentType(MediaType.APPLICATION_JSON);