Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot java.lang.AssertionError:预期状态:200实际:400_Spring Boot_Kotlin_Spring Boot Test - Fatal编程技术网

Spring boot java.lang.AssertionError:预期状态:200实际:400

Spring boot java.lang.AssertionError:预期状态:200实际:400,spring-boot,kotlin,spring-boot-test,Spring Boot,Kotlin,Spring Boot Test,我有一个具有以下端点的身份验证控制器 @PostMapping("/register") fun register(@RequestBody body: RegisterDto): ResponseEntity<User> { val user = User() user.name = body.name user.email = body.email user.password = body.password

我有一个具有以下端点的身份验证控制器

   @PostMapping("/register")
   fun register(@RequestBody body: RegisterDto): ResponseEntity<User> {
     val user = User()
     user.name = body.name
     user.email = body.email
     user.password = body.password
     return ResponseEntity.status(201).body(userService.saveUser(user))
  }

跑步时,我得到400分,我需要得到200分(成功)。我对测试弹簧靴还很陌生。我可能做错了什么或错过了什么?感谢您在andvance中设置参数。实际上,您需要传递一个JSON正文,如下所示:

fun`使用有效输入注册用户,返回ok`(){
val user=user()
user.id=1
user.name=“荷马”
user.email=”homer@simpsons.com"
user.password=“123456”
var response=new ObjectMapper().writeValueAsString(用户)
val result=mockMvc.perform(MockMvcRequestBuilders.post(“/api/auth/register”)
.contentType(MediaType.APPLICATION_JSON)
.内容(回应)
.accept(MediaType.APPLICATION_JSON))
result.andExpect(MockMvcResultMatchers.status().isCreated)
}
  fun `register user with valid input, returns ok`() {
    val result = mockMvc.perform(MockMvcRequestBuilders.post("/api/auth/register")
        .param("id","1")
        .param("name","Homer")
        .param("email","homer@simpsons.com")
        .param("password","123456")
        .contentType(MediaType.APPLICATION_JSON)
        .accept(MediaType.APPLICATION_JSON))
    result.andExpect(MockMvcResultMatchers.status().isOk)
}