-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
small example showing testing and code coverage with Maven / JUnit / …
…JaCoCo
- Loading branch information
0 parents
commit 41808e9
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/maven_testing_example/InvalidOpCodeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |