-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
121 additions
and
7 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/example/kotlinonspringboot/domain/usecase/EmployeeDeleteUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.kotlinonspringboot.domain.usecase | ||
|
||
import com.example.kotlinonspringboot.domain.model.condition.DeleteCondition | ||
|
||
interface EmployeeDeleteUseCase { | ||
|
||
/** | ||
* 削除ユースケース | ||
* @param deleteCondition 削除条件 | ||
* @return true,削除した場合。false,削除対象がなかった場合。 | ||
*/ | ||
fun delete(deleteCondition: DeleteCondition): Boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/example/kotlinonspringboot/usecase/EmployeeDeleteUseCaseImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.kotlinonspringboot.usecase | ||
|
||
import com.example.kotlinonspringboot.domain.model.condition.DeleteCondition | ||
import com.example.kotlinonspringboot.domain.usecase.EmployeeDeleteUseCase | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class EmployeeDeleteUseCaseImpl : EmployeeDeleteUseCase { | ||
|
||
override fun delete(deleteCondition: DeleteCondition): Boolean { | ||
return true | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...xample/kotlinonspringboot/presentation/controller/DeleteInEmployeeUpdateControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.example.kotlinonspringboot.presentation.controller | ||
|
||
import com.example.kotlinonspringboot.domain.model.EmployeeServiceException | ||
import com.example.kotlinonspringboot.domain.usecase.EmployeeDeleteUseCase | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.CsvSource | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.doReturn | ||
import org.mockito.kotlin.doThrow | ||
import org.mockito.kotlin.whenever | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.mock.mockito.MockBean | ||
import org.springframework.test.web.reactive.server.WebTestClient | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@DisplayName("社員削除コントローラのテスト") | ||
class DeleteInEmployeeUpdateControllerTest { | ||
|
||
@MockBean | ||
private lateinit var employeeDeleteUseCase: EmployeeDeleteUseCase | ||
|
||
/** | ||
* メソッドが多いのでテストクラスを分割している | ||
*/ | ||
@Nested | ||
@DisplayName("PK社員削除") | ||
inner class Delete { | ||
|
||
@ParameterizedTest(name = "削除対象:{1}の場合、例外が発生しない場合は204を返却する") | ||
@CsvSource( | ||
delimiter = '|', | ||
textBlock = | ||
// 削除対象の有無 | ||
""" | ||
true |削除あり | ||
false |削除対象なし""" | ||
) | ||
@DisplayName("削除時に例外が出ない場合、204を返却すること") | ||
fun test1(isDelete: Boolean, description: String, @Autowired webClient: WebTestClient) { | ||
// 準備 | ||
// 社員削除ユースケースからの返却を定義 | ||
whenever(employeeDeleteUseCase.delete(any())).doReturn(isDelete) | ||
|
||
// 実行 & 検証 | ||
webClient | ||
.delete().uri("/api/v1/employee/1") | ||
.exchange() | ||
.expectStatus().isNoContent | ||
} | ||
|
||
@Test | ||
@DisplayName("社員データソース例外が発生した場合、ステータス500をが返却すること") | ||
fun test2(@Autowired webClient: WebTestClient) { | ||
// 準備 | ||
// 社員削除ユースケースからの返却を定義 | ||
val runtimeException = RuntimeException("something error message") | ||
val exception = EmployeeServiceException.EmployeeDataSourceException(runtimeException) | ||
whenever(employeeDeleteUseCase.delete(any())).doThrow(exception) | ||
|
||
// 実行 & 検証 | ||
webClient | ||
.delete().uri("/api/v1/employee/1") | ||
.exchange() | ||
.expectStatus().is5xxServerError | ||
.expectStatus().isEqualTo(500) | ||
.expectBody().json( | ||
""" | ||
{ | ||
"message": "databaseで予期しない例外が発生しました" | ||
}""" | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters