Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

api - Spring version impacting assertEquals evaluation

hows going?

I'm studying Kotlin+Spring+MongoDB and I got a problem.

My person class:

import org.bson.types.ObjectId
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document

@Document
data class Person(
    @Id
    val id: ObjectId = ObjectId.get(),
    val name: String,
    val sobrenome: String

)

My controller class:

@RestController
@RequestMapping("/person")
class PersonController (private val repository: PersonRepository){

    @GetMapping("/{id}")
    fun getOnePatient(@PathVariable("id") id: String): ResponseEntity<Person> = ResponseEntity.ok(repository.findOneById(
        ObjectId(id)
    ))
}

Finally, my test class:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension::class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class PersonControllerTest @Autowired constructor (
    private val repository: PersonRepository,
    private val restTemplate: TestRestTemplate
        ) {
    private val defaultPersonId = ObjectId.get()

    @LocalServerPort
    protected var port: Int = 0

    @BeforeEach
    fun setUp() {
        repository.deleteAll()
    }

    private fun getBaseUrl(): String? = "http://localhost:$port/person"
    private fun saveOnePerson() = repository.save(Person(defaultPersonId, "Gabriel", "Andrade"))

    @Test
    fun `should return a single patient by id`() {
        saveOnePerson()
        val response = restTemplate.getForEntity(getBaseUrl() + "/$defaultPersonId", Person::class.java)
        assertEquals(200, response.statusCode.value())
        assertEquals(defaultPersonId, response.body?.id)
    }
}

THE PROBLEM:

assertEquals(defaultPersonId, response.body?.id)

LOG

expected: <5ffdbc186c34a85d396eb847> but was: <5ffdbc186c34a85d396eb848>
Expected :5ffdbc186c34a85d396eb847
Actual   :5ffdbc186c34a85d396eb848

The id that is in the bank will always be increased in relation to the defaultPersonId. In version 2.2.6.RELEASE of Spring it compares correctly, with no increase in value. But in 2.4.1 this error happens.

My doubt is:

I would like to know if there is a way to make it work in the most current version I use.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...