小编new*_*bie的帖子

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

您好,我正在尝试在我的控制器中实现 junit。但我得到的是 201 而不是 200。

下面是我的控制器

@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);
  }
}
Run Code Online (Sandbox Code Playgroud)

接下来是我的 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 …
Run Code Online (Sandbox Code Playgroud)

java junit spring

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

junit ×1

spring ×1