[JAVA] Introduction to JUnit (study memo)

What is a unit test?

A test to make sure that the code you write works as expected. Also called a unit test. JUnit is a very famous framework for unit testing in Java. Writing test programs using a common framework makes it easier for others to modify their test programs.

Super basic flow of testing

    1. Write a test method that has the same functionality as the method you want to test.
  1. Compare the result required for the method with the actual result using the assert method (below).
    1. If there is a difference in the comparison results, the test fails, otherwise the test succeeds.

Setup (using IntelliJ)

https://stackoverflow.com/questions/19330832/setting-up-junit-with-intellij-idea https://www.slideshare.net/SatoshiKubo1/junitjava

type of assert method

Various asserts are used to confirm the test results. assertEquals() → Determine if the expected result is the same as the actual result. Argument 1: (expected, actual) Argument 2: (String message, expected, actual) --A message is displayed if the test fails. Argument 3: (expected, actual, delta) --delta is the tolerance. Argument 4: (String message, double expected, double actual, double delta)- assertNotNull() → Judge whether the given Object is not null. Argument 1: (Object object) Argument 2: (String message, Object object) --A message is displayed if the test fails. assertNull() → Judge whether the given Object is Null. Argument 1: (Object object) Argument 2: (String message, Object object) --A message is displayed if the test fails. assertSame() → Judge whether the given two Objects refer to the same Object. Argument 1: (Object expected, Object actual) Argument 2: (String message, Object object) --A message is displayed if the test fails. assertTrue() → Judge whether the given conditions are correct. Argument 1: (Object expected, Object actual) Argument 2: (String message, Object object) --A message is displayed if the test fails. assertThat() In combination with Matcher, it is more convenient than assertEquals (). There is a limit to assertEquals () because only one equals () can be created for an object. Since it is Matcher, not assertThat, that is verified, it is highly extensible. The situation of writing assert Equals on multiple lines no longer occurs. Arguments: (Object actual, Matcher matcher) Details

important point

The order doesn't matter

The methods in the test class are not executed in order from the top. Therefore, do not write test methods that assume the above test results.

Does not create side effects

Do not create "side effects" that change the state of the system, such as updating the database when testing.

Give a descriptive method name

So that those who read the code later can quickly understand the purpose of the test.

Write easy-to-understand code

Same as above. Simple and easy to understand without waste.

Document your test plan in javadoc

Writing manually + many mistakes + hard to keep up with updates.

As small and fast as possible

To be able to test frequently and easily.

Other useful methods

If you want to perform common processing (database connection / disconnection, etc.) at the beginning and end of multiple methods, use the setUp () and tearDown () methods. setUp() Executed before each test method is called. tearDown() Executed after the end of each test method.

TestSuite class

As mentioned above, the test methods are not executed in the order they are written, but you can use the TestSuite class to specify the order in which the test methods are executed.

Reference source

Recommended Posts

Introduction to JUnit (study memo)
XVim2 introduction memo to Xcode12.3
Introduction to Ruby 2
Introduction to SWING
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
JUnit5 usage memo
Introduction to JAR files
Introduction to Ratpack (8)-Session
Migrate from JUnit 4 to JUnit 5
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
[Java ~ Method ~] Study memo (5)
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Dot installation study memo 01
Introduction to Practical Programming
Introduction to javadoc command
[Java ~ Array ~] Study memo 4
Introduction to jar command
Introduction to Ratpack (2)-Architecture
How to use JUnit 5
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
[Personal memo] I tried to study object orientation lightly
Introduction to Design Patterns (Builder)
Ubuntu Ethernet driver introduction memo
Play Framework Study Memo Database ①
Ruby study memo (conditional branching)
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Ratpack (5) --Json & Registry
How to use JUnit (beginner)
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
[Java ~ Boolean value ~] Study memo (2)
Introduction to Design Patterns (Composite)
Introduction to Micronaut 2 ~ Unit test ~
Introduction to Spring Boot ① ~ DI ~
Introduction to design patterns (Flyweight)
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
Introduction to design patterns Prototype
Java study memo 2 with Progate
GitHub Actions Introduction to self-made actions
How to write Junit 5 organized