Skip to content

Commit

Permalink
update bug handling in test exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleinen committed Dec 4, 2023
1 parent d9bb90e commit 11a1c27
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
23 changes: 3 additions & 20 deletions hugo/content/classes/ws2023/info1/labs/exercise-08.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,9 @@ This lab concerns chapter 9 in the book; in the 6th edition the example had been

## Documenting Bugs in Test Cases

If you find bugs in the project, you will most probably have a failing (red) test case. Depending on the Test Framework, there are various methods to deactivate or skip certain tests. As JUnit doesn't offer a method without
installing an extension, use this simple method as we did in class to replace the assertion:

```java
public static final boolean FAIL_BUGS = true;

public void assertEqualsBug(Object expected, Object actual){
if (!FAIL_BUGS)
return;
StackTraceElement caller = Thread.currentThread().getStackTrace()[2];
String context = caller.toString();
assertEquals(expected,actual,"found BUG in "+caller+":");
}

@Test
public void testShowingBug()
{
assertEqualsBug(false, true);
}
```
If you find bugs in the project, you will most probably have a failing (red) test case. Depending on the Test Framework, there are various methods to deactivate or skip certain tests. As JUnit requires extensions to exclude test cases, I've provided a simple helper with special assertions

See class `BugTestDemo` for examples how to use it!

## Instructions
1. Install the [diary project in the lab](https://github.com/htw-imi-info1/chapter09_testing/tree/master/diary-prototype) - switch to the branch of the current term first to see the changes we did in class. Create a test method in DayTest to check that `findSpace` returns the value of 10 for a one-hour appointment, if a day already has a single one-hour appointment at 9 a.m. In essence, you need to perform similar steps to those used to create `testFindSpace9` in the lecture, use `makeAppointment` for the test fixture and `findSpace` for the second appointment. If you want to add an assertion for the result of the makeAppointment call, you should do so in a separate test method as test method one should only have one assertion.
Expand Down
48 changes: 48 additions & 0 deletions hugo/content/material/info1/junit_in_bluej.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: JUnit in BlueJ
author: kleinen
draft: false
tags: ['bluej', 'junit', 'testing']
courses: ['info1']
---

## Monday, 04.December 2023

JUnit is not part of the Java API, it is a third-party software.
You find all information about JUnit, including a [user guide](https://junit.org/junit5/docs/current/user-guide/) and information about and download of the latest version on [https://junit.org](https://junit.org).

As of this writing, the current version is 5.10.1, but the version included in BlueJ 5.2.1 is JUnit 5.5.2, as you can check within the installation of BlueJ:

```zsh
BlueJ.app/Contents/Java>ls junit*
junit-4.12.jar junit-jupiter-params-5.5.2.jar junit-platform-suite-api-1.5.2.jar
junit-jupiter-5.5.2.jar junit-platform-commons-1.5.2.jar junit-vintage-engine-5.5.2.jar
junit-jupiter-api-5.5.2.jar junit-platform-engine-1.5.2.jar
junit-jupiter-engine-5.5.2.jar junit-platform-launcher-1.5.2.jar
```

As we only use a *very* small portion of JUnit this should not matter - but if you look something up
in the BlueJ documentation, keep this in mind. (you could also try updating JUnit in the above directory, of course. I haven't tried it.)

Also, there have been major changes for writing test cases between JUnit 4 and JUnit 5. You may find old BlueJ-Example projects which still use JUnit 4. As you can see above, both junit-4.12.jar and the vintage engine are included in BlueJ, so they will work. While mixing JUnit 4 and 5 in your source code will work, I recommend migrating old examples to JUnit 5 if you want use them, as anything else will turn out to be confusing.
You can tell by the imports which version is used in the examples:

## Minimal JUnit imports

### JUnit 5 imports

```java
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
```

### Old JUnit 4 imports

```java
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
```

Note that the annotation of the setup method has also changed from `@Before` to `@BeforeEach`.

0 comments on commit 11a1c27

Please sign in to comment.