inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

인프런 워밍업 클럽 3기 BE 스터디 4주차

징니
0

💻 강의

입문자를 위한 Spring Boot with Kotlin - 나만의 포트폴리오 사이트 만들기

 

📚 학습

@Transactional(readOnly = true)

읽기 전용 트랜잭션으로 설정하면 데이터 변경이 일어나지 않기 때문에 스냅샷을 저장하는 동작을 생략해 좀 더 성능을 개선할 수 있다


Service 테스트

 

Repository 테스트를 했을 때와는 다르게 Service 테스트를 할 때는 Mockito를 사용한다

@ExtendWith(MockitoExtension::class)
class PresentationServiceTest {

    @InjectMocks
    lateinit var presentationService: PresentationService

    @Mock
    lateinit var presentationRepository: PresentationRepository
}

Service 테스트 코드 해석

일단 홀수이면 isActive = false, 짝수이면 true로 설정한다

필터링을 해 isActive = true인 것만 남긴다

presentationRepository.getActiveIntroductions()를 실행하면 필터링 한 데이터를 반환하도록 한다

["2", "4", "6"]

테스트 대상 메서드를 실행한다

DATA_SIZE = 7이므로 DATA_SIZE / 2 = 3.5이지만 정수 연산에서는 3이 반환 된다

필터링 한 데이터 수가 일치하면 첫 번째 검증은 통과이다

content 값을 정수로 변환하고, isEven()을 사용해 짝수인지 검증한다

짝수이면 두 번째 검증도 통과이다

@Repository
class PresentationRepository(
    ...) {
...
    fun getActiveIntroductions(): List<Introduction> {
        return introductionRepository.findAllByIsActive(true)
    }
...
    @Test
    fun testGetIntroductions() {
        // given
        val introductions = mutableListOf<Introduction>()
        for (i in 1..DATA_SIZE) { // 1, 3, 5, 7 -> false / 2, 4, 6 -> true
            val introduction = Introduction(content = "${i}", isActive = i % 2 == 0)
            introductions.add(introduction)
        }

        val activeIntroductions = introductions.filter { introduction ->
            introduction.isActive
        }

        Mockito.`when`(presentationRepository.getActiveIntroductions())
            .thenReturn(activeIntroductions)

        // when
        val introductionDTOs = presentationService.getIntroductions()

        // then
        assertThat(introductionDTOs).hasSize(DATA_SIZE / 2)
        for (introductionDTO in introductionDTOs) {
            assertThat(introductionDTO.content.toInt()).isEven()
        }
    } 

Controller 테스트

@SpringBootTest
@AutoConfigureMockMvc
@DisplayName("[API 컨트롤러 테스트]")
class PresentationApiControllerTest(@Autowired private val mockMvc: MockMvc) {
}

Thymeleaf 문법

 

fragment 이름을 navigation으로 지정하고, 해당 경로에 있는 파일의 navigation fragment를 찾아서 대체한다

// /templates/presentation/fragments/fragment-navigation.html

<nav class="navbar navbar-expand-lg navbar-light bg-white py-3" th:fragment="navigation">
// /templates/presentation/index.html

<div th:replace="~{presentation/fragments/fragment-navigation :: navigation}"></div>

 

 

🎯 미션 6과 미션 7



문제

이미지 태그를 지정하고 Dockerfile을 Build 하는 과정에서 test 실패 오류가 발생하였다

테스트 코드가 문제인 건지 확인하기 위해 TestApplication을 실행해 전체 테스트 코드를 실행시키니 오류는 없었다

원인을 찾을 수 없어서 계속 구글링을 해보니 아래 코드가 원인이 될 수도 있다고 한다

tasks.withType<Test> {
	useJUnitPlatform()
}

해결

아래 코드를 주석 처리하니 정상적으로 Build가 됐다

tasks.withType<Test> {
	useJUnitPlatform()
}

백엔드

답변 0