Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit test description

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.

like image 268
semberal Avatar asked Oct 31 '12 13:10

semberal


People also ask

How do you write a description in a JUnit test case?

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.

What is a JUnit test?

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.

What are the JUnit annotations?

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.

What is @test annotation in JUnit?

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.


1 Answers

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); } 
like image 146
semberal Avatar answered Sep 24 '22 07:09

semberal