简体   繁体   English

java.lang.AssertionError:预期状态:<200>但原为:<201>

[英]java.lang.AssertionError: Status expected:<200> but was:<201>

Hi I'm trying to implement junit in my controller. 嗨,我正在尝试在控制器中实现junit。 But what I get is 201 instead of 200. 但是我得到的是201而不是200。

Below is my controller 下面是我的控制器

@RestController
@RequestMapping(value = "/treat")
public class TreatController {

  private final TreatService treatService;

@Autowired
  public TreatController(TreatService treatService){
    this.treatService = treatService;
  }

@PostMapping
  public ResponseEntity<CommonResponse> addNew(
      @RequestBody Treat treat) throws RecordNotFoundException{
    CommonResponse response = new CommonResponse();
    response.setStatus(CommonConstants.OK);
    response.setData(treatService.save(treat));
    return new ResponseEntity<>(response, HttpStatus.CREATED);
  }
}

next is my Junit testing: 接下来是我的Junit测试:

@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(TreatController.class)
public class TreatControllerTest {

  private RecordNotFoundException recordException = new RecordNotFoundException("");

  private final String title = "{\"title\" : \"title\"}";

  @Autowired
  private MockMvc mockMvc;

  @MockBean
  private TreatService treatService;

@Test
  public void addNew() throws Exception{
    Treatment treatment = new Treatment();

    given(treatmentService.save(
        Mockito.any(Treat.class))).willReturn(treat);
    mockMvc.perform(post("/treats")
    .content(title)
    .accept(MediaType.APPLICATION_JSON_VALUE)
    .contentType(MediaType.APPLICATION_JSON_VALUE))

    .andDo(print())
    .andExpect(status().isOk());

    Mockito.verify(treatService).save(Mockito.any(Treat.class));
  }
}

is there anything that I missed? 我有什么想念的吗? By the way, I dont use Json. 顺便说一句,我不使用杰森。 I just inserted it because it works. 我刚刚插入它是因为它有效。

That's what you return. 那就是你的回报。

return new ResponseEntity<>(response, HttpStatus.CREATED);

HttpStatus.CREATED returns 201 and indicates that a resource has been created by the request HttpStatus.CREATED返回201,并指示该请求已创建资源

How ever in your testcase you are expecting OK(200) .andExpect(status().isOk()); 在测试用例中,您期望的是OK(200) .andExpect(status().isOk());

According to HTTP1.1/ specs Post request should always result in the creation of a resource. 根据HTTP1.1 / specs,发布请求应始终导致资源的创建。 So it makes sense to return 201 from there. 因此从那里返回201很有意义。 All your need to do is change your testcase assertion expected value to HTTPStatus.CREATED. 您所需要做的就是将您的测试用例断言期望值更改为HTTPStatus.CREATED。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.AssertionError:预期状态:&lt;201&gt;,但之前是:&lt;400&gt; - java.lang.AssertionError: Status expected:<201> but was:<400> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;405&gt; - java.lang.AssertionError: Status expected:<200> but was:<405> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;302&gt; itShouldAllowAccessToSecuredPageForPermittedUser - java.lang.AssertionError: Status expected:<200> but was:<302> itShouldAllowAccessToSecuredPageForPermittedUser @RequestMapping java.lang.AssertionError:预期状态:200 实际:404 - @RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404 java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;404&gt; in Junit test - java.lang.AssertionError: Status expected:<200> but was:<404> in Junit test java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> java.lang.AssertionError:预期状态:&lt;200&gt;,但在静态服务中为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> in restful services java.lang.AssertionError: 预期状态:&lt;200&gt; 但为:&lt;400&gt; - java.lang.AssertionError: Status expected:<200> but was:<400> MockMVC测试java.lang.AssertionError:预期状态:201实际:404 - MockMVC test java.lang.AssertionError: Status Expected :201 Actual :404 java.lang.AssertionError:预期状态:&lt;400&gt; 但原为:&lt;200&gt; 预期:400 实际:200 - java.lang.AssertionError: Status expected:<400> but was:<200> Expected :400 Actual :200
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM