这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
36 changes: 36 additions & 0 deletions gradle-modules/gradle/junit-report-multi-module/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
plugins {
id("java")
id("jvm-test-suite")
id("test-report-aggregation")
}

group = "com.baeldung.gradle"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}

testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
}
}
}

reporting {
reports {
val testAggregateTestReport by existing(AggregateTestReport::class)
}
}

dependencies {
subprojects.forEach { sub ->
testReportAggregation(project(sub.path))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id("java-library")
id("jvm-test-suite")
}

repositories {
mavenCentral()
}

testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.gradle.firstmodule;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ModuleATest {

@Test
void givenNumbers_whenAdd_thenCorrect() {
int sum = 2 + 3;
assertEquals(5, sum);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id("java-library")
id("jvm-test-suite")
}

repositories {
mavenCentral()
}

testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.gradle.secondmodule;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ModuleBTest {

@Test
void givenString_whenCheckLength_thenCorrect() {
String word = "Hello World";
assertTrue(word.length() > 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = "junit-report-multi-module"
include("modulea", "moduleb")
27 changes: 27 additions & 0 deletions gradle-modules/gradle/junit-report-single-module/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id("java")
id("jacoco")
}

group = "com.baeldung.gradle"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
useJUnitPlatform()

reports {
html.required = true
junitXml.required = true
}

finalizedBy(tasks.jacocoTestReport)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "junit-report-single-module"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.gradle.example;

public class Calculator {

public int add(int a, int b) {
return a + b;
}

public int subtract(int a, int b) {
return a - b;
}

public int multiply(int a, int b) {
return a * b;
}

public int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed");
}
return a / b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.baeldung.gradle.example;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class CalculatorUnitTest {

private Calculator calculator;

@BeforeEach
void setUp() {
calculator = new Calculator();
}

@Test
void shouldAddTwoNumbers() {
int result = calculator.add(5, 3);
assertEquals(8, result);
}

@Test
void shouldSubtractTwoNumbers() {
int result = calculator.subtract(10, 4);
assertEquals(6, result);
}

@Test
void shouldThrowExceptionForDivisionByZero() {
assertThrows(ArithmeticException.class, () -> calculator.divide(10, 0));
}

@Test
void shouldMultiplyTwoNumbers() {
int result = calculator.multiply(4, 7);
assertEquals(28, result);
}
}
2 changes: 2 additions & 0 deletions gradle-modules/gradle/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ include 'greeter'
include 'gradletaskdemo'
include 'unused-dependencies'
include 'gradle-wsdl-stubs'
include 'junit-report-single-module'
include ' junit-report-multi-module'
println 'This will be executed during the initialization phase.'