Skip to content

Instantly share code, notes, and snippets.

@dnno
Created June 3, 2019 18:41
Show Gist options
  • Select an option

  • Save dnno/ce994ebd91d0f8adaf076a16a05888a9 to your computer and use it in GitHub Desktop.

Select an option

Save dnno/ce994ebd91d0f8adaf076a16a05888a9 to your computer and use it in GitHub Desktop.
@RunWith(SpringRunner::class)
@ContextConfiguration(classes = arrayOf(CityConfig::class))
@DataJpaTest
internal class JpaCityServiceTest {
@Autowired
lateinit var service: CityService
@Autowired
lateinit var repository: CityRepository
@get:Rule
var softly = JUnitSoftAssertions()
@Test
fun `'retrieveCities' should retrieve empty list if repository doesn't contain entities`() {
assertThat(service.retrieveCities()).isEmpty()
}
@Test
fun `'retrieveCity' should return null if city for cityId doesnt exist`() {
assertThat(service.retrieveCity(-99)).isNull()
}
@Test
fun `'retrieveCity' should map existing entity from repository`() {
repository.save(CityEntity("cityname", "description"))
val result = service.retrieveCity("city")
softly.assertThat(result).isNotNull
softly.assertThat(result?.id).isNotNull
softly.assertThat(result?.name).isEqualTo("cityname")
softly.assertThat(result?.description).isEqualTo("description")
}
@Test
fun `'addCity' should return created entity`() {
val result = service.addCity(CreateCityDto("name", "description"))
softly.assertThat(result.id).isNotNull()
softly.assertThat(result.name).isEqualTo("name")
softly.assertThat(result.description).isEqualTo("description")
}
@Test
fun `'updateCity' should update existing values`() {
val existingCity = repository.save(CityEntity("cityname", "description")).toDto()
val result = service.updateCity(existingCity.id, UpdateCityDto("new name", "new description"))
softly.assertThat(result).isNotNull
softly.assertThat(result?.id).isEqualTo(existingCity.id)
softly.assertThat(result?.name).isEqualTo("new name")
softly.assertThat(result?.description).isEqualTo("new description")
}
@Test
fun `'updateCity' shouldn't update null values`() {
val existingCity = repository.save(CityEntity("cityname", "description")).toDto()
val result = service.updateCity(existingCity.id, UpdateCityDto(null, null))
softly.assertThat(result).isNotNull
softly.assertThat(result?.id).isEqualTo(existingCity.id)
softly.assertThat(result?.name).isEqualTo("cityname")
softly.assertThat(result?.description).isEqualTo("description")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment