From 11a1c27ee14c28b53e129f70c961281cea16e90b Mon Sep 17 00:00:00 2001 From: Barne Kleinen Date: Mon, 4 Dec 2023 12:44:04 +0100 Subject: [PATCH] update bug handling in test exercise --- .../classes/ws2023/info1/labs/exercise-08.md | 23 ++------- hugo/content/material/info1/junit_in_bluej.md | 48 +++++++++++++++++++ 2 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 hugo/content/material/info1/junit_in_bluej.md diff --git a/hugo/content/classes/ws2023/info1/labs/exercise-08.md b/hugo/content/classes/ws2023/info1/labs/exercise-08.md index ebcdcbecc..52ca4d2e0 100644 --- a/hugo/content/classes/ws2023/info1/labs/exercise-08.md +++ b/hugo/content/classes/ws2023/info1/labs/exercise-08.md @@ -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. diff --git a/hugo/content/material/info1/junit_in_bluej.md b/hugo/content/material/info1/junit_in_bluej.md new file mode 100644 index 000000000..124490274 --- /dev/null +++ b/hugo/content/material/info1/junit_in_bluej.md @@ -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`. \ No newline at end of file