How to write Junit 5 organized

I will transcribe how to write the Junit 5 test class that I practiced in the past as a memorandum. I hope it will be helpful to everyone.

Test class storage

First of all, the place to put the test class. スクリーンショット 2019-12-01 12.36.08.png

Keep the test class separate from the product class and folder. When using Gradle, src / test / java is the standard folder.

package name

Avoid the same name as the product class for the test class package name. However, I definitely want to link it with the test class. So, I think you don't have to use the form "test + package name to be tested".

The reason why the package name is different is that when using Spring, It is a countermeasure such as DI duplication in the test class. (Sure) With the same package name, I couldn't DI the test folder and DI for the class I wanted to test, so It is designed so that it can be distinguished from the beginning of the package name.

name of the class

Don't use this too, let's use "class name to be tested + Test". If the class name is the same, it will be annoying when complementing.

How to write a test

Tested class

Greeting.java


public class Greeting {
  
  public String hello() {    
    return "Hello";
  }
  
  public String helloWorld() {
    return "Hello World";
  }
}

Test class

GreetingTest.java


class GreetingTest {

  @Nested
  @DisplayName("method : hello")
  public class Hello {

    private Greeting test = new Greeting();

    @Test
    @DisplayName("Test case that works properly")
    public void case1() {
      Assertions.assertEquals("Hello", test.hello());
    }
  }
  
  @Nested
  @DisplayName("method : helloWorld")
  public class HelloWold {
    
    private Greeting test = new Greeting();
    
    @Test
    @DisplayName("Test case that works properly")
    public void case1() {
      Assertions.assertEquals("Hello World", test.helloWorld());
    }
    
  }
}

Class modifier

It is safe not to attach anything. If you add something like public, it will be displayed as a complement, so it is difficult to see.

Separate classes for each method

The test class is divided into test classes for each method so that it is easy to see. This form makes it easy to add test cases when adding test cases.

The method name is easy to understand for the inner class name.

Draw a test case firmly with DisplayName

From experience, the method name of the test case may be longer or more appropriate. It's terribly hard to read. Therefore, if you want to give a long method name, use DisplayName firmly. Let's write the test content.

Eclipse test result confirmation sample

If you write Display, it will be reflected on the coverage screen, so it will be easier to understand. スクリーンショット 2019-12-01 12.57.44.png

Summary

The content of the test class is important, but it is also important to write it in the same way. By writing in a formal way, let's write a test class that is easy to change not only during development but also during maintenance.

Recommended Posts

How to write Junit 5 organized
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to use JUnit 5
How to write migrationfile
JUnit 5: How to write test cases in enum
How to use JUnit (beginner)
Bit Tetris (how to write)
How to write java comments
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to write Rails routing
How to migrate from JUnit4 to JUnit5
[Creating] How to use JUnit
How to run JUnit in Eclipse
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
Baseball ball count (how to write)
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
How to filter JUnit Test in Gradle
[Basic] How to write a Dockerfile Self-learning ②
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
How to fix system date in JUnit
How to test private scope with JUnit
How to write Spring AOP pointcut specifier
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
JDBC promises and examples of how to write
Rails: How to write a rake task nicely
[Java] How to test for null with JUnit
[JavaFX] How to write Eclipse permissions in build.gradle
[Rails] How to write when making a subquery
How to execute and mock methods using JUnit
How to test interrupts during Thread.sleep with JUnit
How to write an if statement to improve readability-java
Notes on how to use each JUnit Rule
Offline real-time how to write F06 implementation example
How to write code that thinks object-oriented Ruby
How to write test code with Basic authentication
How to write React Native bridge ~ Android version ~
[Java] Memo on how to write the source
How to write Java String # getBytes in Kotlin?
Notes on how to write comments in English
How to develop OpenSPIFe
How to call AmazonSQSAsync
How to use rbenv