setter에 관한 이야기

 var 프로퍼티가 public으로 열려있어 setter를 쓸 수 있지만 setter 대신 좋은 이름의 함수(=updateName)을 사용하는 것이 훨씬 clean하다. 하지만 name에 대한 setter는 public이기 때문에 유저 이름 업데이트 기능에서 sette r를 사용할'수도' 있다. (=updateName=setName) 따라서 setter만 private하게 만드는 것이 좋다.

첫 번째 방법(backing property 사용)

내부에서는 _name(언더바name)을 사용해서 이름 값을 바꿀 수 있고 외부에서는 불변이 (val) name에 접근해서 Get할 수 있다.

두 번째 방법(custom setter 이용하기)

위 두 방법 모두 property가 많아질수록 번거롭다. 따라서 setter를 열어는 두지만 사용하지 않는 방법을 선호 -> 팀 컨벤션을 잘 맞추면 된다.

 

생성자 안의 프로퍼티. 클래스 body 안의 프로퍼티

꼭 primary constructor 안에 모든 프로퍼티를 넣어야 할까?

body에 만들어도 잘 동작한다.

추천

  • 모든 프로퍼티를 생성자에 넣거나
  • 프로퍼티를 생성자 혹은 클래스 body 안에 구분해서 넣을 때 명확한 기준이 있거나

JPA와 data class

Entity는 data class를 피하는 것이 좋다.

  • equals, hashCode, toString 모두 JPA Entity와는 100% 어울리지 않는 메서드

위의 경우에서

  • User의 quals가 UserLoanHistory의 equals를 부른다.
  • UserLoanHisoty의 equals가 User의 quals를 부른다.

TIP

Entity가 생성되는 로직을 찾고 싶으면 constructor 지시어를 명시적(임의로)으로 작성하고 추적하자!

단순 Book으로 눌러봤을 때는 모든 class가 나오고 constructor를 임의로 작성하고 내부를 눌러보면 딱 '생성'한 부분만 추적가능하다.

'Back-end > Spring+Kotlin' 카테고리의 다른 글

[Spring -> Kotlin] 리팩토링  (0) 2022.12.11
[Java -> Kotlin] Junit5 Test로 코드 짜기  (0) 2022.11.18

Domain 코드 변경하기

특징 : POJO, JPA Entity 객체

Repository  코드 변경하기

특징 : Spring Bean, 의존성 X

Service 코드 변경하기

특징 : Spring Bean, 의존성 O, 비즈니스 로직

Controller, DTO 코드 변경하기

특징 : Spring Bean, 의존성 O, DTO의 경우 그 숫자가 많음

 

Book 코틀린 변경 코드

https://github.com/WantAirpod/Kotlin/commit/b8b353f373aadc79e978897a7cb428cfc77e49b9#diff-56052074ae54fdd8138685dde12fe0d723909f3890c2de412f408e50e83d42a7

User 코틀린 변경 코드

https://github.com/WantAirpod/Kotlin/pull/5/commits/be811187dfb3deebee2704c859a4f8db54a9e6e3#diff-57400e043967f14691c6a655d55c4fb1c76fca87f7d93601827c3a3b7cadf25a

 

Issue : 코틀린 리플렉션 발생 수정

단순히 java->kotlin으로 코드 변경 시 reflect 이슈가 발생한다.

해결 방법

코틀린 reflect 관련 의존성 주입으로 해결한다.

그 밖에 코틀린을 사용하기 위한 의존성도 있다.

 

코틀린은 null에 대해서 무자비하다. 

https://tourspace.tistory.com/114

 

[Kotlin] 코틀린 null 처리 - ? ?. ?: !!, let, lateinit, 제너릭, 플랫폼 타입

이 글은 Kotlin In Action을 참고 하였습니다.더욱 자세한 설명이나 예제는 직접 책을 구매하여 확인 하시기 바랍니다 코틀린에서는 자바보다 null 처리를 좀더 명확하게 합니다. 따라서 NPE(NullPointerEx

tourspace.tistory.com

 

목차

    도서관리 어플리케이션 이해하기

    • 우선 자바로 되어있는 코드를 코틀린으로 바꾼다고 생각하면 된다. 하단은 대략적인 프로젝트의 (흔한)플로우이다.

    • localhost:8080/h2-console 접속 및 로그인 정보

    테스트 코드란 무엇인가? 그리고 왜 필요한가?

    • 개발 과정에서 문제를 미리 발견할 수 있다.
    • 기능 추가와 리팩토링을 안심하고 할 수 있다.
    • 빠른 시간 내 코드의 동작 방식과 결과를 확인할 수 있다.
    • 좋은 테스트 코드를 작성하려 하다보면, 자연스럽게 좋은 코드가 만들어 진다.
    • 잘 작성한 테스트는 문서 역할을 한다.(코드리뷰를 돕는다)
    A라는 API는 25개의 다른 로직에 영향을 미친다. 어느날 A라는 API를 수정할 일이 생겼다. 그렇다면...?? 눈물이난다...

    코틀린 코드 작성 준비하기

    • 코틀린 build gradle 생성
    더보기
    plugins {
        id 'org.springframework.boot' version '2.6.8'
        id 'io.spring.dependency-management' version '1.0.11.RELEASE'
        id 'java'
        id 'org.jetbrains.kotlin.jvm' version '1.6.21'
        id 'org.jetbrains.kotlin.plugin.jpa' version '1.6.21'
        id 'org.jetbrains.kotlin.plugin.spring' version '1.6.21'
        id 'org.jetbrains.kotlin.kapt' version '1.6.21'
    }
    
    group = 'com.group'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '11'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-web'
    
        implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' // 코틀린을 사용하기 위한 의존성 추가
        implementation 'org.jetbrains.kotlin:kotlin-reflect:1.6.21' // 코틀린을 사용하기 위한 의존성 추가
        implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3' // 코틀린을 사용하기 위한 의존성 추가
        implementation 'org.junit.jupiter:junit-jupiter:5.8.1' // 코틀린을 사용하기 위한 의존성 추가
        implementation 'com.querydsl:querydsl-jpa:5.0.0' //querydsl 의존성
        kapt("com.querydsl:querydsl-apt:5.0.0:jpa")
        kapt("org.springframework.boot:spring-boot-configuration-processor")
    
        runtimeOnly 'com.h2database:h2'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
    
    tasks.named('test') {
        useJUnitPlatform()
    }
    
    /**
     * 코틀린에 필요한 compile 옵션 추가
     */
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
    
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }

    사칙연산 계산기에 대해 테스트 코드 작성하기

    1. 계산기는 정수만 취급한다.

    2. 계산기가 생성될 때 숫자를 1개 받는다.

    3. 최초 숫자가 기록된 이후에는 연산자 함수를 통해 숫자를 받아 지속적으로 계산한다.

    데이터 클래스로 테스트 코드 검증하기

    더보기

    검증하고자 하는 클래스에 data를 붙여준다.

    package com.group.libraryapp.calculator
    
    import java.lang.IllegalArgumentException
    
    data class Calculator (
        private var number: Int
    ){
    
        fun add(operand: Int){
            this.number += operand
        }
    
        fun minus(operand: Int){
            this.number -= operand
        }
    
        fun multiply(operand: Int){
            this.number *= operand
        }
    
        fun divide(operand: Int){
            if(operand == 0){
                throw IllegalArgumentException("0으로 못나눔")
            }
            this.number /= operand
        }
    }

    메인을 만들어 테스트를 진행한다.

    package com.group.libraryapp.calculator
    
    import java.util.Calendar
    
    /**
     * main 생성
     */
    
    fun main(){
        val calculatorTest = CalculatorTest()
        calculatorTest.addTest();
    }
    class CalculatorTest {
        fun addTest(){
            val calculator = Calculator(5)
            calculator.add(3)
    
            val expectedCalculator = Calculator(8)
            if(calculator != expectedCalculator){
                throw IllegalStateException()
            }
        }
    }

    data를 지우고 number를 public으로 하거나 get을 열어주어 test에서도 접근가능하도록한다.

    코드 컨벤션 _number

    package com.group.libraryapp.calculator
    
    import java.lang.IllegalArgumentException
    
    class Calculator (
        //private var number: Int
        //var number: Int // setter를 연 상태
        private var _number: Int
    
    ){
    
        /**
         * getter를 연상태
         */
        val number: Int
            get() = this._number
    
        fun add(operand: Int){
            this._number += operand
        }
    
        fun minus(operand: Int){
            this._number -= operand
        }
    
        fun multiply(operand: Int){
            this._number *= operand
        }
    
        fun divide(operand: Int){
            if(operand == 0){
                throw IllegalArgumentException("0으로 못나눔")
            }
            this._number /= operand
        }
    }

    test쪽에서 바로 get 가능하다.

    package com.group.libraryapp.calculator
    
    import java.util.Calendar
    
    /**
     * main 생성
     */
    
    fun main(){
        val calculatorTest = CalculatorTest()
        calculatorTest.addTest();
    }
    class CalculatorTest {
        fun addTest(){
            // given
            val calculator = Calculator(5)
    
            //when
            calculator.add(3)
            /*
            val expectedCalculator = Calculator(8)
            if(calculator != expectedCalculator){
                throw IllegalStateException()
            }  */
            //then
            if(calculator.number != 8 ){
                throw  IllegalStateException()
            }
        }
    }

    Junit5 사용법과 테스트 코드 리팩토링

    Junit5에서 사용되는 5가지 어노테이션

    6

    • @Test : 테스트 
    • @BeforeEach : 각 테스트 메소드가 수행되기 전에 실행되는 메소드를 지정한다.
    • @AfterEach : 각 테스트가 수행된 후에 실행되는 메소드를 지정한다.
    • @BeforeAll : 모든 테스트를 수행하기 전에 최초 1회 수행되는 메소드를 지정한다.
    • @AfterAll : 모든 테스트를 수행한 후 최후 1회 수행되는 메소드를 지정한다.

    Test Code 작성

    더보기
    package com.group.libraryapp.calculator
    
    import org.junit.jupiter.api.*
    
    class JunitTest {
        companion object {
            @BeforeAll
            @JvmStatic
            fun beforeAll() {
                println("모든 테스트 시작 전")
            }
    
            @AfterAll
            @JvmStatic
            fun afterAll() {
                println("모든 테스트 종료 후")
            }
        }
    
        @BeforeEach
        fun beforeEach() {
            println("각 테스트 시작 전")
        }
    
        @AfterEach
        fun afterEach() {
            println("각 테스트 종료 후")
        }
    
        @Test
        fun test1() {
            println("테스트 1")
        }
    
        @Test
        fun test2() {
            println("테스트 2")
        }
    }

    결과

    계산기에 적용하기

    assertThat Imort 하기

    • AssertProvider 선택하기

    테스트 코드 작성하기

    @Test
    fun addTest(){
        // given
        val calculator = Calculator(5)
    
        // when
        calculator.add(3)
    
        // then
        assertThat(calculator.number).isEqualTo(7);
    }

    테스트 코드 결과

    추가로 사용하는 단언문

    • isTrue/isFalse : true/false 검증
    // then
    val isNew = true
    assertThat(isNew).isTrue();
    assertThat(isNew).isFalse();
    • hasSize(n) : size 검증 (주로 list의 갯수를 확인)

    • extracting/containsExactlyInAnyOrder : 주어진 컬렉션 안의 Item 들에서 name 이라는 프로퍼티를 추출한 후, 그 값이 A와 B인지를 검증한다.(AnyOrder : 이 때 순서는 중요하지 않다)

    • assertThrows : funtion1 함수를 실행했을 때 liigalArgumentException이 나오는지 검증

    만약 나온다면 message로 던져주는 메서드

    Junit5으로 Spring Boot 테스트 하기

    • Controller - bean 관리 대상이므로 @SpringBootTest로 진행
    • Service - bean 관리 대상이므로 @SpringBootTest로 진행
    • Repository - bean 관리 대상이므로 @SpringBootTest로 진행
    • Domain - bean 관리 대상이 아니므로 @Test 진행

    어떤 계층을 테스트 해야 할까?

    보통은 Service 계층을 테스트한다. 보통 A를 보냈을 때 B가 잘 나오는지, 원하는 로직을 잘 수행하는지 검증할 수 있기 때문이다.

     

    @Autowired 해주기

    class UserServiceTest @Autowired constructor(
        private val userRepository: UserRepository
        ,private val userService: UserService
    ) {

    위처럼 contrructor를 사용해서 한 번에 Autowired 해줄 수 있다.

     

    테스트 코드 작성하기

    1. 사칙연산 테스트 코드 작성

    2. UserTest 작성

    2.1 UserTest 작성(get)

    2.2 UserTest 작성(Delete)

    3. Book관련 Loan 관련 테스트 작성

     

    모든 테스트 Terminal로 실행하는 방법

    ./gradlew test

    결과

    다음과 같이 커버리지도 알 수 있다.

    2번 째 방법

    다음의 test를 눌러 본다. 더 권장 되는 방법으로 어디서 틀렸는지 쉽게 확인 가능하다.

    + Recent posts