Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- kotlin
- Greedy
- 김영한
- Android
- SpringBoot
- Thymeleaf
- QueryDSL
- spring
- jpa
- 스프링 핵심 기능
- transaction
- db
- springdatajpa
- 자바
- 스프링 핵심 원리
- 그리디
- 알고리즘
- java
- JPQL
- AOP
- Exception
- 인프런
- 백준
- Proxy
- Servlet
- 스프링
- Spring Boot
- http
- JDBC
- pointcut
Archives
- Today
- Total
개발자되기 프로젝트
JUnit2 본문
1. Todo
data class Todo (
var index:Int?= null,
var title:String?= null,
var description: String?= null,
var schedule: LocalDateTime?= null,
var createdAt: LocalDateTime?= null,
var updatedAt: LocalDateTime?= null
)
2. DataBase
data class TodoDataBase(
var index: Int = 0,
var todoList: MutableList<Todo> = mutableListOf()
) {
fun init(){
this.todoList = mutableListOf()
this.index = 0
print("[DEBUG] todo database init")
}
}
3.Configuration
@Configuration
class AppConfig {
@Bean(initMethod = "init")
fun todoDataBase(): TodoDataBase {
return TodoDataBase()
}
}
4.Repository
interface TodoRepository {
fun save(todo:Todo): Todo?
fun saveAll(todoList: MutableList<Todo>): Boolean
fun delete(index: Int): Boolean
fun findOne(index: Int): Todo
fun findAll(): MutableList<Todo>
}
@Service
class TodoRepositoryImpl: TodoRepository {
@Autowired
lateinit var todoDataBase: TodoDataBase
override fun save(todo: Todo): Todo? {
return todo.index?.let {index ->
//update
return findOne(index)?.apply {
this.title = todo.title
this.description = todo.description
this.schedule = todo.schedule
this.updatedAt = LocalDateTime.now()
}
}?: kotlin.run {
//insert
todo.apply {
this.index = ++todoDataBase.index
this.createdAt = LocalDateTime.now()
this.updatedAt = LocalDateTime.now()
}.run {
todoDataBase.todoList.add(todo)
this
}
}
}
override fun saveAll(todoList: MutableList<Todo>): Boolean {
return try {
todoList.forEach {
save(it)
}
true
}catch (e: Exception){
false
}
}
override fun delete(index: Int): Boolean {
val todo = findOne(index)
return todo?.let {
todoDataBase.todoList.removeAt(index)
true
}?: kotlin.run {
false
}
}
override fun findOne(index: Int): Todo {
return todoDataBase.todoList.first { it.index == index }
}
override fun findAll(): MutableList<Todo> {
return todoDataBase.todoList
}
}
5.Test
//test시 로드 할 클래스 지정.
@SpringBootTest(classes = [TodoRepositoryImpl::class, AppConfig::class])
internal class TodoRepositoryImplTest{
@Autowired
lateinit var todoRepositoryImpl: TodoRepositoryImpl
@BeforeEach
fun before(){
todoRepositoryImpl.todoDataBase.init()
}
@Test
fun saveTest(){
val todo = Todo().apply {
this.title = "테스트 일정"
this.description = "test"
this.schedule = LocalDateTime.now()
}
val result = todoRepositoryImpl.save(todo)
Assertions.assertEquals(1, result?.index)
Assertions.assertNotNull(result?.createdAt)
Assertions.assertNotNull(result?.updatedAt)
}
@Test
fun saveAllTest(){
val todoList= mutableListOf(
Todo().apply {
this.title = "테스트 일정1"
this.description = "test1"
this.schedule = LocalDateTime.now()
},
Todo().apply {
this.title = "테스트 일정2"
this.description = "test2"
this.schedule = LocalDateTime.now()
},
Todo().apply {
this.title = "테스트 일정3"
this.description = "test3"
this.schedule = LocalDateTime.now()
}
)
val result = todoRepositoryImpl.saveAll(todoList)
Assertions.assertEquals(true, result)
}
@Test
fun findOneTest(){
val todoList= mutableListOf(
Todo().apply {
this.title = "테스트 일정1"
this.description = "test1"
this.schedule = LocalDateTime.now()
},
Todo().apply {
this.title = "테스트 일정2"
this.description = "test2"
this.schedule = LocalDateTime.now()
},
Todo().apply {
this.title = "테스트 일정3"
this.description = "test3"
this.schedule = LocalDateTime.now()
}
)
todoRepositoryImpl.saveAll(todoList)
val result = todoRepositoryImpl.findOne(2)
Assertions.assertNotNull(result)
Assertions.assertEquals("테스트 일정2", result?.title)
}
@Test
fun updateTest(){
val todo = Todo().apply {
this.title = "테스트 일정"
this.description = "test"
this.schedule = LocalDateTime.now()
}
val insertTodo = todoRepositoryImpl.save(todo)
val newTodo = Todo().apply {
this.index = 1
this.title = "업데이트 일정"
this.description = "업데이트 테스트"
this.schedule = LocalDateTime.now()
}
val result = todoRepositoryImpl.save(newTodo)
Assertions.assertNotNull(result)
Assertions.assertEquals(insertTodo?.index, result?.index)
assertEquals("업데이트 일정", result?.title)
}
}
'인프런 > [인프런] Kotlin으로 개발하는 Spring Boot Web MVC' 카테고리의 다른 글
JUnit Test (0) | 2022.04.26 |
---|---|
예외처리: Exception Handler (0) | 2022.04.25 |
예외처리: Controleller Advice (0) | 2022.04.25 |
JSR-380 Bean Validation : custom annotaion (0) | 2022.04.25 |
JSR-380 Bean Validation (0) | 2022.04.24 |
Comments