Skip to content

Instantly share code, notes, and snippets.

@wesleyegberto
Last active August 20, 2022 22:06
Show Gist options
  • Save wesleyegberto/4cf1dd51f0e488c9112f1dfca7174673 to your computer and use it in GitHub Desktop.
Save wesleyegberto/4cf1dd51f0e488c9112f1dfca7174673 to your computer and use it in GitHub Desktop.
Dependencies to Java Testing: unit and mutation tests; controller tests; architecture tests; testcontainer for IT and Jacoco for coverage report.

Dependencies to Java Testing with:

  • JUnit 5
  • API tests
  • Architecture tests
  • TestContainer for IT
  • Jacoco for coverage report
  • Pitest for mutation test

To run:

  • mvn test to run UT
  • mvn integration-tests to run UT and IT
  • mvn verify to run everything

Flags:

  • skipTests
  • skipUnitTests
  • skipIntegrationTests
<project>
	<properties>
		<java.version>11</java.version>
		<skipTests>false</skipTests>
		<skipUnitTests>${skipTests}</skipUnitTests>
		<skipIntegrationTests>${skipTests}</skipIntegrationTests>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- API Doc -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-boot-starter</artifactId>
			<version>3.0.0</version>
		</dependency>

		<!-- JSON path assertions -->
		<dependency>
			<groupId>com.jayway.jsonpath</groupId>
			<artifactId>json-path</artifactId>
			<version>2.5.0</version>
			<scope>test</scope>
		</dependency>

		<!-- RestAssured -->
		<dependency>
			<groupId>com.jayway.restassured</groupId>
			<artifactId>rest-assured</artifactId>
			<version>2.9.0</version>
			<scope>test</scope>
		</dependency>

		<!-- Architecture testing -->
		<dependency>
			<groupId>com.tngtech.archunit</groupId>
			<artifactId>archunit-junit5</artifactId>
			<version>0.18.0</version>
			<scope>test</scope>
		</dependency>

		<!-- Containers to run infrastructure deps (also should add specific component lib) -->
		<dependency>
			<groupId>org.testcontainers</groupId>
			<artifactId>testcontainers</artifactId>
			<version>1.15.3</version>
			<scope>test</scope>
		</dependency>
		<!-- only for JUnit5 -->
		<dependency>
			<groupId>org.testcontainers</groupId>
			<artifactId>junit-jupiter</artifactId>
			<version>1.15.3</version>
			<scope>test</scope>
		</dependency>

		<!-- Generate docs from MockMVC tests -->
		<dependency>
			<groupId>org.springframework.restdocs</groupId>
			<artifactId>spring-restdocs-mockmvc</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<plugin>
				<groupId>org.asciidoctor</groupId>
				<artifactId>asciidoctor-maven-plugin</artifactId>
				<version>1.5.8</version>
				<executions>
					<execution>
						<id>generate-docs</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>process-asciidoc</goal>
						</goals>
						<configuration>
							<backend>html</backend>
							<doctype>book</doctype>
						</configuration>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>org.springframework.restdocs</groupId>
						<artifactId>spring-restdocs-asciidoctor</artifactId>
						<version>${spring-restdocs.version}</version>
					</dependency>
				</dependencies>
			</plugin>

			<!--
			configure Surefire to run integration tests separately
			- `mvn test` to run UT
			- `mvn integration-tests` to run UT and IT
			- `mvn verify` to run everything
			Flags:
			- `skipTests`
			- `skipUnitTests`
			- `skipIntegrationTests`
			-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skipTests>${skipUnitTests}</skipTests>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-failsafe-plugin</artifactId>
				<configuration>
					<includes>
						<include>**/*IT.java</include>
					</includes>
					<skipTests>${skipIntegrationTests}</skipTests>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>integration-test</goal>
							<goal>verify</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<!--
				Coverage Test Report
				- report file: `target/site/jacoco/index.html`
			-->
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>0.8.8</version>
				<executions>
					<execution>
						<id>jacoco-initialize</id>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<execution>
						<id>jacoco-site</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<!--
				Mutation test
				- report file:
			-->
			<plugin>
				<groupId>org.pitest</groupId>
				<artifactId>pitest-maven</artifactId>
				<version>1.7.5</version>
				<executions>
					<execution>
						<id>report</id>
						<!-- <phase>prepare-package</phase> -->
						<phase>test</phase>
						<goals>
							<goal>mutationCoverage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment