Skip to content

Commit

Permalink
Merge pull request #1 from muriloalvesdev/feature/test-controller
Browse files Browse the repository at this point in the history
test controller
  • Loading branch information
muriloalvesdev authored Jan 17, 2023
2 parents b800f34 + dc9d85d commit 0217fa5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# kotlin-hexagonal-architecture
# kotlin-hexagonal-architecture

[![Build Status](https://app.travis-ci.com/muriloalvesdev/kotlin-hexagonal-architecture.svg?branch=main)](https://app.travis-ci.com/muriloalvesdev/kotlin-hexagonal-architecture)
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import br.com.hexagonal.domain.ports.`interface`.UserServicePort
import br.com.hexagonal.provider.UserDtoProviderTests
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ArgumentsSource
import org.mockito.Mockito.doNothing
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.kotlin.given
import org.springframework.http.HttpStatus
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.web.context.request.RequestContextHolder
Expand All @@ -27,6 +29,7 @@ class UserControllerUnitUnitTest : BaseUnitTest() {
RequestContextHolder.setRequestAttributes(ServletRequestAttributes(request))
}

@DisplayName("Mocado - Deve salvar um usuario")
@ParameterizedTest
@ArgumentsSource(UserDtoProviderTests::class)
fun shouldSave(userDto: UserDto) {
Expand All @@ -39,9 +42,42 @@ class UserControllerUnitUnitTest : BaseUnitTest() {
//THEN
assertThat(HttpStatus.CREATED).isEqualTo(response.statusCode)
assertThat(response.headers.location).isNotNull
assertThat("http://localhost/users/muriloalvesdev")
assertThat("http://localhost/users/$USERNAME")
.isEqualTo(response.headers.location.toString())

verify(this.userServicePort, times(1))
verify(this.userServicePort, times(1)).save(userDto)
}

@DisplayName("Mocado - Deve buscar um usuario")
@ParameterizedTest
@ArgumentsSource(UserDtoProviderTests::class)
fun shouldGetUser(userDto: UserDto) {
//GIVEN
given(this.userServicePort.find(USERNAME))
.willReturn(userDto)

//WHEN
val response = this.controller.find(USERNAME)

//THEN
verify(this.userServicePort, times(1)).find(USERNAME)

assertThat(userDto)
.usingRecursiveComparison()
.isEqualTo(response)
}

@DisplayName("Mocado - Deve atualizar um usuario")
@ParameterizedTest
@ArgumentsSource(UserDtoProviderTests::class)
fun shouldUpdateUser(userDto: UserDto) {
//GIVEN
doNothing().`when`(this.userServicePort).update(userDto)

//WHEN
this.controller.update(userDto)

//THEN
verify(this.userServicePort, times(1)).update(userDto)
}
}

0 comments on commit 0217fa5

Please sign in to comment.