设置:spring-boot 1.4.3.RELEASE Java 1.8
我已经针对BluecostRestController.java(以下代码)编写了三个测试用例。以下网址的测试工作正常: GET / bluecost / sscdata GET / bluecost / sscdata / sergio
现在,我正在尝试为POST / bluecost / sscdata /编写一个测试用例,并且遇到以下断言异常:
java.lang.AssertionError: Status expected:<201> but was:<400>
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:665)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
at com.ibm.cio.cloud.cost.spreadsheet.controller.UTBlueCostRestControllerTests.testAddingSpreadsheetUpload(UTBlueCostRestControllerTests.java:198)
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.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:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
由于某种原因,它找不到此路径:/ bluecost / sscdata与该对象。这是我的测试课:
@RunWith(SpringRunner.class) @WebMvcTest(com.ibm.cio.cloud.cost.spreadsheet.rest.controller.BlueCostRestController.class)
public class UTBlueCostRestControllerTests extends UTBlueCostRestControllerParent {
private static final long CREATED_SSCDATA_ID = 400l;
@Test
public void testAddingSpreadsheetUpload() throws Exception {
BluecostSSCData mockSSCData = new BluecostSSCData(
CREATED_SSCDATA_ID,
200l,
"chargeType-D7",
"controlGroup-293D",
"TEXACO",
"TestUser"
);
String sscData = "{\r\n" +
" \"sscdataid\": \"400\",\r\n" +
" \"processGroupId\": \"200\",\r\n" +
" \"chargeTypeCd\": \"chargeType-D7\",\r\n" +
" \"controlGroupCd\": \"controlGroup-293D\",\r\n" +
" \"accountId\": \"TEXACO\",\r\n" +
" \"userId\": \"TestUser\",\r\n" +
"}";
when(blueCostSSCDataService.saveBluecostSSCData(mockSSCData))
.thenReturn(mockSSCData);
mvc.perform(MockMvcRequestBuilders
.post("/bluecost/sscdata/").content(sscData)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(header().string("location",
containsString("/bluecost/sscdata/"
+ CREATED_SSCDATA_ID)));
}
This is the Controller Class:
@RequestMapping("bluecost")
@RestController
public class BlueCostRestController {
@Autowired
BluecostSSCDataService blueCostSSCDataService;
@Autowired
CostSpreadsheetService costSpreadsheetService;
@Autowired
BlueCostService blueCostService;
@RequestMapping(value = "/sscdata/", method = RequestMethod.GET)
public ResponseEntity<List<BluecostSSCData>> getAllBluecostSSCData() {
List<BluecostSSCData> bluecostSSCDataList = blueCostSSCDataService.getAllBlueCostSSCData();
if (bluecostSSCDataList.isEmpty()) {
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<BluecostSSCData>>(bluecostSSCDataList, HttpStatus.OK);
}
@RequestMapping(value = "/sscdata/{localField2}", method = RequestMethod.GET)
public ResponseEntity<?> findSSCDataByUploader(@PathVariable("localField2") String localField2) {
List<BluecostSSCData> bluecostSSCDataList = blueCostSSCDataService.findBluecostSSCDataByUploader(localField2);
if (bluecostSSCDataList.size() == 0) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<List<BluecostSSCData>>(bluecostSSCDataList, HttpStatus.OK);
}
@RequestMapping(value = "/sscdata/", method = RequestMethod.POST)
public ResponseEntity<?> createBluecostSSCData(@RequestBody BluecostSSCData sscdata) {
BluecostSSCData createdSSCData = blueCostSSCDataService.saveBluecostSSCData(sscdata);
if (createdSSCData == null)
return ResponseEntity.noContent().build();
URI location = ServletUriComponentsBuilder
.fromCurrentRequest().path("/{id}")
.buildAndExpand(createdSSCData.getSscdataid())
.toUri();
return ResponseEntity.created(location).build();
}
答案 0 :(得分:0)
如@dashboard所建议,这可能是“ userId”:“ TestUser” 中的逗号结尾的问题。但是,这种错误很可能是由于复制粘贴而发生的。为了编写这样的junit,我更喜欢使用 JSONObject 构建json字符串,而不是手动编写。
因此,您的String sscData = "{\r\n" + ....
将表示为
import org.json.JSONObject;
JSONObject json = new JSONObject();
json.put("sscdataid", 400);
json.put("chargeTypeCd", controlGroup-293D);
...
您的求职电话变成
mvc.perform(MockMvcRequestBuilders
.post("/bluecost/sscdata/").content(**json.toString()**)
.contentType(MediaType.APPLICATION_JSON))
希望这会有所帮助。