Is it possible in JUnit to add a brief description of the test for the future reader (e.g. what's being tested, some short explanation, expected result, ...)? I mean something like in ScalaTest, where I can write:
test("Testing if true holds") { assert(true) }
Ideal approach would be using some annotation, e.g.
@Test @TestDescription("Testing if true holds") public void testTrue() { assert(true); }
Therefore, if I run such annotated tests using Maven (or some similar tool), I could have similar output to the one I have in SBT when using ScalaTest:
- Testing if entity gets saved correctly - Testing if saving fails when field Name is not specified - ...
Currently I can either use terribly long method names or write javadoc comments, which are not present in the build output.
Thank you.
You can write them smartly, e.g. entityIsSavedCorrectly , saveFailsWhenNameMissing , etc. Be sure to leave off the test prefix as it is redundant (esp. with the @Test annotation). ScalaTest is only giving a string where JUnit's method name would be, so all you are "losing" is spaces.
JUnit is a unit testing open-source framework for the Java programming language. Java Developers use this framework to write and execute automated tests. In Java, there are test cases that have to be re-executed every time a new code is added. This is done to make sure that nothing in the code is broken.
JUnit Annotations is a special form of syntactic meta-data that can be added to Java source code for better code readability and structure. Variables, parameters, packages, methods and classes can be annotated. Annotations were introduced in Junit4, which makes Java code more readable and simple.
The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure.
In JUnit 5, there is @DisplayName annotation:
@DisplayName is used to declare a custom display name for the annotated test class or test method. Display names are typically used for test reporting in IDEs and build tools and may contain spaces, special characters, and even emoji.
Example:
@Test @DisplayName("Test if true holds") public void checkTrue() { assertEquals(true, true); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With