Skip to content

Commit

Permalink
Merge pull request #24 from co-co-gong/feat#21
Browse files Browse the repository at this point in the history
test: JaCoCo & Codecov
  • Loading branch information
Zerohertz authored Jan 7, 2025
2 parents ae11a6b + bbcc612 commit dbe8b18
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: "CI/CD"

on:
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: test
run: |
echo "test"
# TODO: ghcr.io & Argo CD (related: #22)
41 changes: 41 additions & 0 deletions .github/workflows/codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: "Spring Test"

on:
push:
branches:
- "*"
pull_request:
branches:
- main

permissions:
contents: write
actions: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- uses: actions/setup-java@v4
with:
java-version: "17"
distribution: "adopt"

- name: Spring test
run: |
./gradlew clean test
env:
JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY }}

- name: Upload test results to Codecov
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Upload results to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
- [Package 구조](docs/package-structure.md)
- [Service layer 개발 방법](docs/service.md)
- [Controller 개발 방법](docs/controller.md)
- [Test 코드 개발 방법](docs/test.md)
- [Docker Compose 사용 방법](docs/docker-compose.md)
- [Kubernetes 사용 방법](docs/kubernetes.md)
33 changes: 33 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.3.2'
id 'io.spring.dependency-management' version '1.1.6'
id 'jacoco'
}

group = 'co-co-gong'
Expand Down Expand Up @@ -33,6 +34,8 @@ dependencies {
// Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'

// MapStruct
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
Expand All @@ -56,4 +59,34 @@ dependencies {

tasks.named('test') {
useJUnitPlatform()
finalizedBy 'jacocoTestReport'
}

jacocoTestReport {
dependsOn test
reports {
xml.required = true
csv.required = false
html.outputLocation = layout.buildDirectory.dir("jacocoHtml")
}
}

jacocoTestCoverageVerification {
violationRules {
rule {
limit {
// minimum = 0.80
minimum = 0.0
}
}
}
}

test {
finalizedBy jacocoTestCoverageVerification
testLogging {
events "passed", "failed", "skipped"
exceptionFormat "full"
}
}

32 changes: 32 additions & 0 deletions codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
coverage:
status:
project:
default:
target: 0% # FIXME: Test 완성 후 변경
patch:
default:
target: 0% # FIXME: Test 완성 후 변경

comment:
layout: "header, diff, flags, components, files, footer"
behavior: default
require_changes: false
require_base: false
require_head: false
hide_project_coverage: false

component_management:
default_rules:
statuses:
- type: project
target: 0% # FIXME: Test 완성 후 변경
- type: patch
target: 0% # FIXME: Test 완성 후 변경
individual_components:
- component_id: user
name: user
paths:
- src/test/java/com/server/domain/user/service/**
# TODO: Coverage 측정 제외 대상에 대해 정의
# ignore:
# -
Binary file added docs/images/jacoco.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions docs/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Spring test 규약

- `JUnit`: Java 단위 test framework
- 목적: Code 검증
- 사용법: `@Test`, `@BeforeEach` 등 annotation으로 test 구성
- `Mockito`: Mocking library
- 목적: 의존성 격리
- 사용법: 가짜 객체 생성으로 독립적 test 수행
- `JaCoCo`: Code coverage 측정 도구
- 목적: Test coverage 분석
- 사용법: Test 후 coverage report 제공

## Spring test 시 debugging

1. `@Slf4j` annotation을 추가한다.

```java
@Slf4j
class UserServiceTest {
```

2. 아래와 같이 `--info`를 통해 logger 수준을 설정한다.

```shell
$ ./gradlew clean test --info | grep ERROR
2025-01-07T21:21:19.197+09:00 ERROR 13537 --- [co-co-gong-server] [ Test worker] c.s.domain.user.service.UserServiceTest : -----------------
```

## JaCoCo

```shell
$ cd ./build/jacocoHtml && python -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET / HTTP/1.1" 200 -
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET /jacoco-resources/report.css HTTP/1.1" 200 -
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET /jacoco-resources/sort.js HTTP/1.1" 200 -
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET /jacoco-resources/redbar.gif HTTP/1.1" 200 -
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET /jacoco-resources/greenbar.gif HTTP/1.1" 200 -
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET /jacoco-resources/session.gif HTTP/1.1" 200 -
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET /jacoco-resources/report.gif HTTP/1.1" 200 -
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET /jacoco-resources/sort.gif HTTP/1.1" 200 -
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET /jacoco-resources/down.gif HTTP/1.1" 200 -
{IP}.{IP}.{IP}.{IP} - - [07/Jan/2025 21:44:45] "GET /jacoco-resources/package.gif HTTP/1.1" 200 -
```

![jacoco](./images/jacoco.png)

## [Codecov](https://app.codecov.io/gh/co-co-gong/co-co-gong-server)
5 changes: 5 additions & 0 deletions src/main/java/com/server/domain/oauth/dto/GithubDto.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.server.domain.oauth.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@Builder
@Getter
@NoArgsConstructor
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

@Getter
public class BaseException extends RuntimeException {
private final ErrorCode errorCode;
private final int status;
private final String message;

public BaseException(ErrorCode errorCode) {
this.errorCode = errorCode;
this.status = errorCode.getStatus();
this.message = errorCode.getMessage();
}
Expand Down
Loading

0 comments on commit dbe8b18

Please sign in to comment.