问题描述
我正在尝试测试REST控制器的GET / users端点。我正在使用Spring Boot和Mongodb。
我的控制器:
class MessageController(
private val userRepository: UserRepository
) {
@GetMapping("/users")
fun getUsers(@RequestParam(value = "name",required = false) name: String?): ResponseEntity<List<User>> {
val users: List<User> =
name?.takeIf { it.isNotEmpty() }?.let { userRepository.findUsersByName(it) } ?: userRepository.findAll()
return ResponseEntity.ok(users)
}
我的测试:
@Test
fun shouldReturnUsers() {
val users = listof(User("1","James",18))
val response = restTemplate.getForEntity<List<User>>("/users")
given(userRepository.findAll()).willReturn(users)
val response = restTemplate.getForEntity<List<User>>("/users")
assertthat(response.statusCode).isEqualTo(HttpStatus.OK)
assertthat(response.body).containsExactlyInAnyOrderElementsOf(users)
}
java.lang.AssertionError:
Expecting:
<[{"age"=18,"id"="1","name"="James"}]>
to contain exactly in any order:
<[User(id=1,name=James,age=18)]>
elements not found:
<[User(id=1,age=18)]>
and elements not expected:
<[{"age"=18,"name"="James"}]>
为什么我的response.body按字母顺序排序? 我怎么能通过考试?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)