Commit 7b885af2 authored by Berk Yavuz's avatar Berk Yavuz
Browse files

junit5 update

parent 70c81289
Showing with 231 additions and 0 deletions
+231 -0
README.md 0 → 100644
## Index
* [Test Coverage](#test-coverage)
* [Jacoco](#jacoco)
* [Exercise: Jacoco with Maven](#exercise-jacoco-with-maven)
---
## Test Coverage
It will include gathering information about which parts of a program are actually executed when running the test suite in order to determine which branches of conditional statements have been taken.
---
## Jacoco
JaCoCo has a plugin on eclipse marketplace and it also has Maven/Gradle plugin and can be integrated with SonarQube.
> We will use jacoco maven implementation with `pom.xml`
### Exercise: Jacoco with Maven
1. Open Eclipse IDE and create Maven project
![maven](./img/mvncreate.png)
2. Make the project GroupId `com.eteration` and Artifact Id `jacoco-demo`
![maven2](./img/projectmvn.png)
3. After theese you will have a project structre like that
```
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │   └── com
│   │   └── eteration
│   │   └── jacoco_demo
│   │   └── App.java
│   └── test
│   └── java
│   └── com
│   └── eteration
│   └── jacoco_demo
│   └── AppTest.java
└── target
├── classes
│   ├── com
│   │   └── eteration
│   │   └── jacoco_demo
│   │   └── App.class
│   └── META-INF
│   ├── MANIFEST.MF
│   └── maven
│   └── com.eteration
│   └── jacoco-demo
│   ├── pom.properties
│   └── pom.xml
└── test-classes
└── com
└── eteration
└── jacoco_demo
└── AppTest.class
```
4. Open `App.java` inside `src/main/java` or create if does not exist and paste code below
```java
package com.eteration.jacoco_demo;
public class App {
public boolean checkPalindrome(String s) {
String reverse = new StringBuffer(s).reverse().toString();
if (s.equals(reverse)) {
return true;
}
else {
return false;
}
}
}
```
5. Open `pom.xml` and change the dependency of JUnit as below
```xml
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
```
6. Open `pom.xml` and add the following lines
```xml
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
7. Open `AppTest.java` inside`src/test/java` and edit as below
```java
package com.eteration.jacoco_demo;
import org.junit.Test;
public class AppTest {
@Test
public void palindromeAlgorithmTest() {
}
}
```
8. Run the commands and open `jacoco-demo/target/site/jacoco/index.html`
```
$ mvn clean install test
```
As you can see the coverage is 0% because we are not cover the code with test yet.
![index](./img/nocoverage.png)
9. We will cover the code with a simple test. Open `AppTest.java` inside `src/test/java` and add the method as below
```java
package com.eteration.jacoco_demo;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class AppTest {
@Test
public void palindromeAlgorithmTest() {
App palindromeTester = new App();
assertTrue(palindromeTester.checkPalindrome("civic"));
}
}
```
10. Run the command again and open `jacoco-demo/target/site/jacoco/index.html`. To access the details showing in the image below, just click the packages and classes.
```
$ mvn clean install test
```
![missed](./img/missedbranch.png)
| Color | Explanaiton |
| ----------- |:-----------------------------------:|
| **Red** | code is not tested or covered. |
| **Green** | code is tested or covered |
| **Orange** | code is partially tested or covered.|
> We missed 1 branch which is `else` condition and 1 `line`. We cover the all methods but it is not enough to get 100% coverage.
11. Add the lines into `palindromeAlgorithmTest` method inside `AppTest.java`
```java
assertFalse(palindromeTester.checkPalindrome("example"));
```
12. After adding line into `palindromeAlgorithmTest`, the `AppTest.java` should look like the code below
```java
package com.eteration.jacoco_demo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class AppTest {
@Test
public void palindromeAlgorithmTest() {
App palindromeTester = new App();
assertTrue(palindromeTester.checkPalindrome("civic"));
assertFalse(palindromeTester.checkPalindrome("example"));
}
}
```
13. Run the commands again to examine the report from Jacoco.
```
$ mvn clean install test
```
14. Open `jacoco-demo/target/site/jacoco/index.html`
![full-cover](./img/fullcover.png)
img/fullcover.png

19.9 KB

img/missedbranch.png

39.2 KB

img/mvncreate.png

40.6 KB

img/nocoverage.png

19.5 KB

img/projectmvn.png

28.1 KB

Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment