기본 환경 세팅
Dependency
plugins {
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'jacoco'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
jacoco {
toolVersion = '0.8.5'
}
jacocoTestReport {
reports {
html.enabled true
xml.enabled false
csv.enabled false
}
}
jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS'
limit {
counter = 'BRANCH'
value = 'COVEREDRATIO'
// minimum = 0.90
}
}
}
}
test {
useJUnitPlatform()
jacoco {
enabled = true
destinationFile = file("$buildDir/jacoco/${name}.exec")
includes = []
excludes = []
excludeClassLoaders = []
includeNoLocationClasses = false
sessionId = "<auto-generated value>"
dumpOnExit = true
classDumpDir = null
output = JacocoTaskExtension.Output.FILE
address = "localhost"
port = 6300
jmx = false
}
}
테스트 코드 작성
@SpringBootTest
class TestCoverageApplicationTests {
@Autowired
private MemberController controller;
@Autowired
private MemberRepository repository;
@PostConstruct
void initData() {
repository.save("first", "firstData");
repository.save("second", "secondData");
repository.save("third", "thirdData");
repository.save("forth", "forthData");
repository.save("fifth", "fifthData");
}
@Test
void findAll() {
Map<String, String> result = controller.findAll();
assertThat(result.size()).isEqualTo(5);
}
}
결과 확인
test -> jacocoTestReport -> jacocoTestCoverageVerification 순으로 실행
그리고 build.gradle에 작성한
package com.example.testconverage;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.PostConstruct;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
class TestCoverageApplicationTests {
@Autowired
private MemberController controller;
@Autowired
private MemberRepository repository;
@PostConstruct
void initData() {
repository.save("first", "firstData");
repository.save("second", "secondData");
repository.save("third", "thirdData");
repository.save("forth", "forthData");
repository.save("fifth", "fifthData");
}
@Test
void findAll() {
Map<String, String> result = controller.findAll();
assertThat(result.size()).isEqualTo(5);
}
@Test
void findOne() {
Map<String, String> result = controller.findByKey("first");
assertThat(result.size()).isEqualTo(1);
assertThat(result.get("result")).isEqualTo("firstData");
}
@Test
void save() {
Map<String, String> saveResult = controller.save("sixth", "sixthData");
Map<String, String> result = controller.findAll();
assertThat(saveResult.get("result")).isEqualTo("success");
assertThat(result.size()).isEqualTo(6);
}
@Test
void update() {
Map<String, String> updateResult = controller.update("first", "firstDataUpdated");
Map<String, String> result = controller.findAll();
assertThat(updateResult.get("result")).isEqualTo("success");
assertThat(result.size()).isEqualTo(5);
}
@Test
void delete() {
Map<String, String> deleteResult = controller.delete("first");
Map<String, String> result = controller.findAll();
assertThat(deleteResult.get("result")).isEqualTo("success");
assertThat(result.size()).isEqualTo(4);
}
}
테스트가 이뤄진 코드는 초록색으로 표기되고 실행되지 않은 코드는 빨간색, 노란줄은 일부만 실행된 코드
추가 환경 설정
build.gradle에 아래 코드 추가
task testCoverage(type: Test) {
group 'verification'
description 'Runs the unit tests with coverage'
dependsOn(':test',
':jacocoTestReport',
':jacocoTestCoverageVerification')
tasks['jacocoTestReport'].mustRunAfter(tasks['test'])
tasks['jacocoTestCoverageVerification'].mustRunAfter(tasks['jacocoTestReport'])
}
testCoverage라는 task 추가
testCoverage를 실행하여 test, jacocoTestReport, jacocoTestCoverageVerification을 한번에 순서대로 실행할 수 있다.
참고자료
https://woowabros.github.io/experience/2020/02/02/jacoco-config-on-gradle-project.html
'spring' 카테고리의 다른 글
Spring - Kafka 연동하기 (0) | 2021.12.04 |
---|---|
Spring에서 Http Request 보내기 - Feign (0) | 2021.09.19 |
Spring Data Event Handler (0) | 2021.06.19 |
Custom HandlerMethodArgumentResolver (0) | 2021.06.08 |
JWT를 이용한 인증 처리 (0) | 2021.05.29 |
댓글