java.lang.AssertionError:期望的状态:< 200>但是:< 400> for Spring Boot Rest控制器补丁请求

时间:2018-05-21 09:24:05

标签: spring spring-boot spring-restcontroller

下面是我的控制器(SalesOrderController.Java)和JUnit Test(SalesOrderControllerTest.Java)

@RestController

公共类SalesOrderController扩展了BaseController {

/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory.getLogger(SalesOrderController.class);

/** The sales order service. */
@Autowired
private ISalesOrderService salesOrderService;

@CrossOrigin
@RequestMapping(value = "/issuers/{issuerId}/reassign-sales-orders", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public BaseResponse reassignSalesOrder(@PathVariable("issuerId") final Long pIssuerId,
        @RequestBody @Valid final ReassignSalesOrderRequest pReassignSalesOrderRequest) {
    LOGGER.info("Reassigning sales orders of issuer:" + pIssuerId);
    return salesOrderService.reassignSalesOrder(pIssuerId, pReassignSalesOrderRequest);
}

}

以下是我的Junit for above controller

@RunWith(MockitoJUnitRunner.class)

公共类SalesOrderControllerTest扩展了AbstractBaseControllerTest {

/** The sales order service. */
@Mock
private ISalesOrderService salesOrderService;

/** The sales order controller. */
@InjectMocks
private SalesOrderController salesOrderController;

@Before
public void setUp() throws Exception {
    mockMvc = MockMvcBuilders.standaloneSetup(salesOrderController).build();
}

@Test
public void testReassignSalesOrder() throws Exception {
    ReassignSalesOrderRequest lSalesOrderUpdateRequest = new ReassignSalesOrderRequest();
    lSalesOrderUpdateRequest.setRedirectOption("Satellite");
    lSalesOrderUpdateRequest.setSatelliteId(200L);
    lSalesOrderUpdateRequest.setSalesOrderIds(Collections.singletonList(1L));
    when(salesOrderService.reassignSalesOrder(100L, lSalesOrderUpdateRequest)).thenReturn(new BaseResponse());
    ObjectMapper mapper = new ObjectMapper();
    mockMvc.perform(patch("/api/issuers/100/reassign-sales-orders")
            .contentType(MediaType.APPLICATION_JSON_UTF8).accept(MediaType.APPLICATION_JSON_UTF8)
            .content(mapper.writeValueAsString(lSalesOrderUpdateRequest)))
            .andExpect(status().isOk());
    verify(salesOrderService).reassignSalesOrder(100L, lSalesOrderUpdateRequest);
}

}

java.lang.AssertionError: Status expected:<200> but was:<400>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:55)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:82)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:617)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:178)
at com.gide.csri.cui.controller.SalesOrderControllerTest.testReassignSalesOrder(SalesOrderControllerTest.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
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)

为什么我收到此错误我的代码中出了什么问题?

3 个答案:

答案 0 :(得分:0)

您的API会消耗并生成MediaType.APPLICATION_JSON_VALUE,但您会发送MediaType.APPLICATION_JSON_UTF8

另外,您确定使用了正确的URL吗? 我的意思是您的@RequestMapping/issuers/{issuerId}/reassign-sales-orders,并且您将请求发送给/api/issuers/100/reassign-sales-orders

答案 1 :(得分:0)

有两种简单的方法可以找到答案。

  • 调试测试代码并查看响应正文,而不仅仅是状态代码。或者甚至在身体上断言。
  • 将您的日志级别设置为debug,当然您的Spring应用程序也会显示错误客户端请求的错误。

答案 2 :(得分:0)

我有同样的问题。 确保您的类 ReassignSalesOrderRequest 具有所有的setter,getter和构造函数(主要是no args构造函数)。