Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
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)
    }
}
Comments