Skip to content
62 changes: 61 additions & 1 deletion practice-gradle/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.github.spotbugs.snom.SpotBugsTask

/*
* This file was generated by the Gradle 'init' task.
*
Expand All @@ -9,6 +11,8 @@ plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
id("com.diffplug.spotless") version "7.0.3"
jacoco
id("com.github.spotbugs") version "6.1.10"
}

repositories {
Expand All @@ -18,11 +22,28 @@ repositories {

spotless{
java {
googleJavaFormat("1.20.0").aosp()
googleJavaFormat("1.20.0")
.aosp()
target("src/**/*.java")

importOrder("java|javax", "org", "com.mycorp", "", "\\#")
removeUnusedImports()

trimTrailingWhitespace()
endWithNewline()
}
}

jacoco {
toolVersion = "0.8.13"
}

spotbugs {
toolVersion.set("4.9.3")
effort.set(com.github.spotbugs.snom.Effort.MAX)
reportLevel.set(com.github.spotbugs.snom.Confidence.MEDIUM)
}

dependencies {
// Use JUnit Jupiter for testing.
testImplementation(libs.junit.jupiter)
Expand All @@ -31,6 +52,7 @@ dependencies {

// This dependency is used by the application.
implementation(libs.guava)
testImplementation("org.assertj:assertj-core:3.27.3")
}

// Apply a specific Java toolchain to ease working on different environments.
Expand All @@ -48,4 +70,42 @@ application {
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
finalizedBy(tasks.jacocoTestReport)
}

tasks.jacocoTestReport {
dependsOn(tasks.test)
reports {
html.required.set(true)
xml.required.set(true)
}
}

tasks.named<SpotBugsTask>("spotbugsMain") {

// 필요 시 커스텀 클래스 경로 지정
// classes.setFrom(sourceSets.main.get().output)

reports.apply {
/* HTML */
create("html") {
required.set(true)
outputLocation.set(
layout.buildDirectory.file("reports/spotbugs/${name}.html")
)
setStylesheet("fancy-hist.xsl")
}

/* XML — SonarQube, CI 파싱용 */
create("xml") {
required.set(true)
}

/* SARIF — GitHub Code Scanning 용 (필요 없으면 true→false) */
create("sarif") {
required.set(true)
}
}
}

tasks.check { dependsOn("spotbugsMain") }
26 changes: 26 additions & 0 deletions practice-gradle/app/src/main/java/org/example/item/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.example.item;

public class Item {

private int cnt = 0;

public int getCnt() {
return cnt;
}

public void increaseCnt() {
cnt++;
}

public void decreaseCnt() {
cnt--;
}

public String toString() {
return cnt + "";
}

public void setCnt(int cnt) {
this.cnt = cnt;
}
}
4 changes: 3 additions & 1 deletion practice-gradle/app/src/test/java/org/example/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
package org.example;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class AppTest {
@Test void appHasAGreeting() {
@Test
void appHasAGreeting() {
App classUnderTest = new App();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
}
Expand Down
50 changes: 50 additions & 0 deletions practice-gradle/app/src/test/java/org/example/item/ItemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.example.item;

import org.junit.jupiter.api.Test;

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

public class ItemTest {

@Test
void getTest() {
// given
Item item = new Item();

// then
assertThat(item.getCnt()).isEqualTo(0);
}

@Test
void incrementTest() {
// given
Item item = new Item();

// when
item.increaseCnt();

assertThat(item.getCnt()).isEqualTo(1);
}

@Test
void decrementTest() {
Item item = new Item();

item.decreaseCnt();

assertThat(item.getCnt()).isEqualTo(-1);
}

@Test
void equalsTest() {
Item item = new Item();
assertThat(item).isEqualTo(item);
}

@Test
void setTest() {
Item item = new Item();
item.setCnt(3);
assertThat(item.getCnt()).isEqualTo(3);
}
}