Skip to content

Commit

Permalink
small example showing testing and code coverage with Maven / JUnit / …
Browse files Browse the repository at this point in the history
…JaCoCo
  • Loading branch information
kyledewey committed Jan 26, 2022
0 parents commit 41808e9
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Mac stuff
.DS_Store

# Emacs temp files
*~
\#*\#

# Maven build stuff
target/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Basic Testing Setup for Java #

This code uses [`maven`](https://maven.apache.org/) as a Java build tool.
Maven is responsible for managing dependencies and automating running the code.

- To compile all code: `mvn compile`
- To run all tests: `mvn test`
- To determine coverage information:
- `mvn jacoco:report`
- Open `target/site/jacoco/index.html` for an HTML coverage report
46 changes: 46 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>trees</groupId>
<artifactId>trees</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>maven_testing_example</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
21 changes: 21 additions & 0 deletions src/main/java/maven_testing_example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package maven_testing_example;

public class Example {
public static int doOperation(final int opCode,
final int operand1,
final int operand2) throws InvalidOpCodeException {
if (opCode == 0) {
// addition
return operand1 + operand2;
} else if (opCode == 1) {
return operand1 - operand2;
} else if (opCode == 2) {
return operand1 * operand2;
} else if (opCode == 3) {
return operand1 / operand2;
} else {
throw new InvalidOpCodeException(opCode);
}
} // doOperation
} // Example

13 changes: 13 additions & 0 deletions src/main/java/maven_testing_example/InvalidOpCodeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package maven_testing_example;

public class InvalidOpCodeException extends Exception {
// ---BEGIN INSTANCE VARIABLES---
public final int opCode;
// ---END INSTANCE VARIABLES---

public InvalidOpCodeException(final int opCode) {
super("Invalid op code: " + opCode);
this.opCode = opCode;
}
} // InvalidOpCodeException

31 changes: 31 additions & 0 deletions src/test/java/maven_testing_example/ExampleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package maven_testing_example;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class ExampleTest {
@Test
public void testOpCode0() throws InvalidOpCodeException {
assertEquals(3, Example.doOperation(0, 1, 2));
}

@Test
public void testOpCode1() throws InvalidOpCodeException {
assertEquals(5, Example.doOperation(1, 8, 3));
}

@Test
public void testOpCode2() throws InvalidOpCodeException {
assertEquals(20, Example.doOperation(2, 5, 4));
}

@Test
public void testOpCode3() throws InvalidOpCodeException {
assertEquals(3, Example.doOperation(3, 15, 5));
}

@Test(expected = InvalidOpCodeException.class)
public void testInvalidOpCode() throws InvalidOpCodeException {
Example.doOperation(4, 0, 0);
}
} // ExampleTest

0 comments on commit 41808e9

Please sign in to comment.