Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/jacoco-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ on:
branches: [ "main" ]

jobs:
build:
jacoco-report:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write

steps:
- uses: actions/checkout@v4
Expand All @@ -28,7 +29,8 @@ jobs:
uses: Madrapps/[email protected]
with:
title: Test Coverage Report
paths: ${{ github.workspace }}/build/jacocoReport/test/jacocoTestReport.xml
id: jacocoReport
paths: ${{ github.workspace }}/build/reports/jacoco/test/jacocoTestReport.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 50
min-coverage-changed-files: 50
17 changes: 12 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ java {

jacoco {
toolVersion = "0.8.11"
reportsDirectory = layout.buildDirectory.dir('jacocoReport')
}

configurations {
Expand All @@ -30,11 +29,18 @@ repositories {
}

dependencies {
// spring boot
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

// h2
runtimeOnly 'com.h2database:h2'
}

tasks.named('test') {
Expand All @@ -53,8 +59,9 @@ jacocoTestReport {
classDirectories.setFrom(
files(classDirectories.files.collect {
fileTree(dir: it, excludes: [
'**/domain/**',
'**/global/**',
'**/*dto*',
'**/*entity*',
'**/*Jpa*',
'**/*Application*'
])
})
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/demo/jacoco/api/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package demo.jacoco.api.controller;

import demo.jacoco.api.dto.PostCreateRequest;
import demo.jacoco.api.dto.PostInfoResponse;
import demo.jacoco.api.service.PostService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/posts")
public class PostController {

private final PostService postService;

public PostController(PostService postService) {
this.postService = postService;
}

@PostMapping
public void createPost(@RequestBody PostCreateRequest request) {
postService.createPost(request);
}

@GetMapping("/{id}")
public PostInfoResponse getPost(@PathVariable Long id){
return postService.getPost(id);
}
}
8 changes: 8 additions & 0 deletions src/main/java/demo/jacoco/api/dto/ListWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package demo.jacoco.api.dto;

import java.util.List;

public record ListWrapper<T>(
List<T> result
) {
}
17 changes: 17 additions & 0 deletions src/main/java/demo/jacoco/api/dto/PostCreateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package demo.jacoco.api.dto;

import demo.jacoco.persistence.entity.post.PostJpaEntity;
import lombok.Builder;

@Builder
public record PostCreateRequest(
String title,
String content
) {
public PostJpaEntity toEntity() {
return PostJpaEntity.builder()
.title(title)
.content(content)
.build();
}
}
21 changes: 21 additions & 0 deletions src/main/java/demo/jacoco/api/dto/PostInfoResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package demo.jacoco.api.dto;

import demo.jacoco.persistence.entity.post.PostJpaEntity;
import lombok.Builder;

@Builder
public record PostInfoResponse(
Long id,
String title,
String content,
Integer likeCount
) {
public static PostInfoResponse fromEntity(PostJpaEntity postJpaEntity) {
return PostInfoResponse.builder()
.id(postJpaEntity.getId())
.title(postJpaEntity.getTitle())
.content(postJpaEntity.getContent())
.likeCount(postJpaEntity.getLikeCount())
.build();
}
}
26 changes: 26 additions & 0 deletions src/main/java/demo/jacoco/api/service/PostService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package demo.jacoco.api.service;

import demo.jacoco.api.dto.PostCreateRequest;
import demo.jacoco.api.dto.PostInfoResponse;
import demo.jacoco.persistence.entity.post.PostJpaEntity;
import demo.jacoco.persistence.repository.PostRepository;
import org.springframework.stereotype.Service;

@Service
public class PostService {

private final PostRepository postRepository;

public PostService(final PostRepository postRepository) {
this.postRepository = postRepository;
}

public void createPost(PostCreateRequest request) {
postRepository.save(request.toEntity());
}

public PostInfoResponse getPost(Long id){
PostJpaEntity postJpaEntity = postRepository.getById(id);
return PostInfoResponse.fromEntity(postJpaEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package demo.jacoco.persistence.entity.post;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Builder;
import lombok.Getter;

@Getter
@Table(name = "post")
@Entity
public class PostJpaEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "post_id")
private Long id;

@Column(name = "post_title", nullable = false)
private String title;

@Column(name = "post_content")
private String content;

@Column(name = "post_like_count")
private Integer likeCount;

protected PostJpaEntity() {}

@Builder
private PostJpaEntity(String title, String content) {
this.title = title;
this.content = content;
this.likeCount = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package demo.jacoco.persistence.repository;

import demo.jacoco.persistence.entity.post.PostJpaEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostJpaRepository extends JpaRepository<PostJpaEntity, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package demo.jacoco.persistence.repository;

import demo.jacoco.persistence.entity.post.PostJpaEntity;

public interface PostRepository {
void save(PostJpaEntity postJpaEntity);

PostJpaEntity getById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package demo.jacoco.persistence.repository;

import demo.jacoco.persistence.entity.post.PostJpaEntity;
import org.springframework.stereotype.Repository;

@Repository
public class PostRepositoryImpl implements PostRepository{

private final PostJpaRepository postJpaRepository;

public PostRepositoryImpl(PostJpaRepository postJpaRepository) {
this.postJpaRepository = postJpaRepository;
}

@Override
public void save(PostJpaEntity postJpaEntity) {
postJpaRepository.save(postJpaEntity);
}

@Override
public PostJpaEntity getById(Long id) {
return postJpaRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Post not found"));
}
}
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spring:
application:
name: jacoco-test

datasource:
url: jdbc:h2:mem:testdb
username: sa
password: password
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
13 changes: 13 additions & 0 deletions src/test/java/demo/jacoco/api/controller/TestControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@ void test() throws Exception {
.andExpect(status().isOk())
.andExpect(content().string("Test"));
}

@DisplayName("테스트 컨트롤러에서 Example 문자열을 반환한다.")
@Test
void example() throws Exception {
// Given
given(testService.getExample())
.willReturn("Example");

// When & Then
mockMvc.perform(get("/example"))
.andExpect(status().isOk())
.andExpect(content().string("Example"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package demo.jacoco.persistence.repository;

import static org.assertj.core.api.Assertions.assertThat;

import demo.jacoco.api.dto.PostCreateRequest;
import demo.jacoco.persistence.entity.post.PostJpaEntity;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

@DataJpaTest
@Import(PostRepositoryImpl.class)
class PostRepositoryTest {

@Autowired
private PostRepository postRepository;

@DisplayName("포스트를 저장할 수 있다.")
@Test
void save() {
// Given
PostCreateRequest postCreateRequest = createPostRequest("테스트 제목", "테스트 내용");

// When
postRepository.save(postCreateRequest.toEntity());

// Then
PostJpaEntity postJpaEntity = postRepository.getById(1L);
assertThat(postJpaEntity).isNotNull()
.extracting("title", "content")
.containsExactly("테스트 제목", "테스트 내용");
}

private static PostCreateRequest createPostRequest(String title, String content) {
return PostCreateRequest.builder()
.title(title)
.content(content)
.build();
}

}
Loading