diff --git a/maven-modules/jacoco-coverage-aggregation/aggregate-report/pom.xml b/maven-modules/jacoco-coverage-aggregation/aggregate-report/pom.xml new file mode 100644 index 0000000000..1a5d186490 --- /dev/null +++ b/maven-modules/jacoco-coverage-aggregation/aggregate-report/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + com.baeldung.jacoco-coverage-aggregation + aggregate-report + aggregate-report + pom + + + com.baeldung + jacoco-coverage-aggregation + 1.0 + + + + + com.baeldung.jacoco-coverage-aggregation + services + ${project.parent.version} + + + com.baeldung.jacoco-coverage-aggregation + controllers + ${project.parent.version} + + + + + + + org.jacoco + jacoco-maven-plugin + 0.8.8 + + + verify + + report-aggregate + + + + **/jacoco.exec + + + ${project.reporting.outputDirectory}/jacoco-aggregate + + + + + + + + + \ No newline at end of file diff --git a/maven-modules/jacoco-coverage-aggregation/controllers/pom.xml b/maven-modules/jacoco-coverage-aggregation/controllers/pom.xml new file mode 100644 index 0000000000..a7c213f5af --- /dev/null +++ b/maven-modules/jacoco-coverage-aggregation/controllers/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + com.baeldung.jacoco-coverage-aggregation + controllers + controllers + jar + + + com.baeldung + jacoco-coverage-aggregation + 1.0 + + + + + com.baeldung.jacoco-coverage-aggregation + services + ${project.parent.version} + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot.version} + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot.version} + test + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.1.2 + + + + integration-test + verify + + + + + + **/*IntegrationTest.java + + + + + + + + 3.0.9 + + + \ No newline at end of file diff --git a/maven-modules/jacoco-coverage-aggregation/controllers/src/main/java/com/baeldung/coverageaggregation/MyApplication.java b/maven-modules/jacoco-coverage-aggregation/controllers/src/main/java/com/baeldung/coverageaggregation/MyApplication.java new file mode 100644 index 0000000000..03b8cb6b87 --- /dev/null +++ b/maven-modules/jacoco-coverage-aggregation/controllers/src/main/java/com/baeldung/coverageaggregation/MyApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.coverageaggregation; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MyApplication { + public static void main(String[] args) { + SpringApplication.run(MyApplication.class, args); + } +} \ No newline at end of file diff --git a/maven-modules/jacoco-coverage-aggregation/controllers/src/main/java/com/baeldung/coverageaggregation/MyController.java b/maven-modules/jacoco-coverage-aggregation/controllers/src/main/java/com/baeldung/coverageaggregation/MyController.java new file mode 100644 index 0000000000..59217a5b94 --- /dev/null +++ b/maven-modules/jacoco-coverage-aggregation/controllers/src/main/java/com/baeldung/coverageaggregation/MyController.java @@ -0,0 +1,30 @@ +package com.baeldung.coverageaggregation; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +class MyController { + + private final MyService myService; + + public MyController(MyService myService) { + this.myService = myService; + } + + @GetMapping("/tested") + String fullyTested() { + return myService.coveredByUnitAndIntegrationTests(); + } + + @GetMapping("/indirecttest") + String indirectlyTestingServiceMethod() { + return myService.coveredByIntegrationTest(); + } + + @GetMapping("/nottested") + String notTested() { + return myService.notTested(); + } + +} \ No newline at end of file diff --git a/maven-modules/jacoco-coverage-aggregation/controllers/src/test/java/com/baeldung/coverageaggregation/MyControllerIntegrationTest.java b/maven-modules/jacoco-coverage-aggregation/controllers/src/test/java/com/baeldung/coverageaggregation/MyControllerIntegrationTest.java new file mode 100644 index 0000000000..3811f7e150 --- /dev/null +++ b/maven-modules/jacoco-coverage-aggregation/controllers/src/test/java/com/baeldung/coverageaggregation/MyControllerIntegrationTest.java @@ -0,0 +1,40 @@ +package com.baeldung.coverageaggregation; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +@SpringBootTest(classes = MyApplication.class) +@AutoConfigureMockMvc +class MyControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + void whenFullyTested_ThenCorrectText() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/tested")) + .andExpect(MockMvcResultMatchers.status() + .isOk()) + .andExpect(MockMvcResultMatchers.content() + .string("covered by unit and integration tests")); + } + + @Test + void whenIndirectlyTestingServiceMethod_ThenCorrectText() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/indirecttest")) + .andExpect(MockMvcResultMatchers.status() + .isOk()) + .andExpect(MockMvcResultMatchers.content() + .string("covered by integration test")); + } + +} diff --git a/maven-modules/jacoco-coverage-aggregation/pom.xml b/maven-modules/jacoco-coverage-aggregation/pom.xml new file mode 100644 index 0000000000..8670ff328a --- /dev/null +++ b/maven-modules/jacoco-coverage-aggregation/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + com.baeldung + jacoco-coverage-aggregation + 1.0 + jacoco-coverage-aggregation + pom + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + services + controllers + aggregate-report + + + + + + org.jacoco + jacoco-maven-plugin + 0.8.8 + + + + prepare-agent + + + + + + + + \ No newline at end of file diff --git a/maven-modules/jacoco-coverage-aggregation/services/pom.xml b/maven-modules/jacoco-coverage-aggregation/services/pom.xml new file mode 100644 index 0000000000..50c8c87f54 --- /dev/null +++ b/maven-modules/jacoco-coverage-aggregation/services/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + com.baeldung.jacoco-coverage-aggregation + services + services + jar + + + com.baeldung + jacoco-coverage-aggregation + 1.0 + + + + + + org.springframework + spring-context + ${spring-context.version} + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter-engine.version} + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + + + **/*Test.java + + + + + + + + 5.9.2 + 6.0.11 + + + \ No newline at end of file diff --git a/maven-modules/jacoco-coverage-aggregation/services/src/main/java/com/baeldung/coverageaggregation/MyService.java b/maven-modules/jacoco-coverage-aggregation/services/src/main/java/com/baeldung/coverageaggregation/MyService.java new file mode 100644 index 0000000000..1c19b8d3cd --- /dev/null +++ b/maven-modules/jacoco-coverage-aggregation/services/src/main/java/com/baeldung/coverageaggregation/MyService.java @@ -0,0 +1,25 @@ +package com.baeldung.coverageaggregation; + +import java.lang.String; +import org.springframework.stereotype.Service; + +@Service +class MyService { + + String unitTestedOnly() { + return "unit tested only"; + } + + String coveredByUnitAndIntegrationTests() { + return "covered by unit and integration tests"; + } + + String coveredByIntegrationTest() { + return "covered by integration test"; + } + + String notTested() { + return "not tested"; + } + +} \ No newline at end of file diff --git a/maven-modules/jacoco-coverage-aggregation/services/src/test/java/com/baeldung/coverageaggregation/MyServiceUnitTest.java b/maven-modules/jacoco-coverage-aggregation/services/src/test/java/com/baeldung/coverageaggregation/MyServiceUnitTest.java new file mode 100644 index 0000000000..017de771bf --- /dev/null +++ b/maven-modules/jacoco-coverage-aggregation/services/src/test/java/com/baeldung/coverageaggregation/MyServiceUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.coverageaggregation; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MyServiceUnitTest { + + MyService myService = new MyService(); + + @Test + void whenUnitTestedOnly_thenCorrectText() { + assertEquals("unit tested only", myService.unitTestedOnly()); + } + + @Test + void whenTestedMethod_thenCorrectText() { + assertEquals("covered by unit and integration tests", myService.coveredByUnitAndIntegrationTests()); + } + +} \ No newline at end of file diff --git a/maven-modules/pom.xml b/maven-modules/pom.xml index f7bba3a8ff..43f2904f04 100644 --- a/maven-modules/pom.xml +++ b/maven-modules/pom.xml @@ -19,6 +19,7 @@ compiler-plugin-java-9 dependency-exclusion host-maven-repo-example + jacoco-coverage-aggregation maven-archetype maven-builder-plugin maven-classifier