[Java] [Spring] Test the behavior of the logger

request

Method

sample

Target class

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Sample {
  private Logger logger = LoggerFactory.getLogger(Sample.class);

  public void run() {
    logger.info("Sample message");
  }
}

Test class

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;


class SampleTest {
  private Sample sample = new Sample();

  //Capture the arguments passed for logger confirmation
  @Captor
  ArgumentCaptor<String> logMessageCaptor;

  @Mock
  private Logger mockLogger;

  @BeforeEach
  public void setUp(){
    MockitoAnnotations.initMocks(this); //By doing this, you can replace the field variables of the class under test with mock.
  }

  @Test
  void testRun() {
    sample.run();
    verify(mockLogger, times(1)).info(logMessageCaptor.capture()); //Here, while checking the info execution count of the logger, the argument is captured.
    assertEquals("Sample message", logMessageCaptor.getValue());
  }
}

About verify

Recommended Posts

[Java] [Spring] Test the behavior of the logger
Check the behavior of Java Intrinsic Locks with bpftrace
Check the behavior of include, exclude, ExhaustedRetryException of Spring Retry
I didn't understand the behavior of Java Scanner and .nextLine ().
Java beginners briefly summarized the behavior of Array and ArrayList
[Java] Delete the elements of List
[Java version] The story of serialization
The origin of Java lambda expressions
Filter the result of BindingResult [Spring]
The story of not knowing the behavior of String by passing Java by reference
Spring Java
See the behavior of entity update with Spring Boot + Spring Data JPA
The behavior of JS running on `Java SE 8 Nashorn` suddenly changed
Test the behavior of the log output logger when using an external logging API such as SLF4J
The story of encountering Spring custom annotation
Get the result of POST in Java
Check the contents of the Java certificate store
Examine the memory usage of Java elements
[Java] Get the day of the specific day of the week
After 3 months of Java and Spring training
Test the integrity of the aggregation using ArchUnit ②
Memo: [Java] Check the contents of the directory
Compare the elements of an array (Java)
[day: 5] I summarized the basics of Java
About the initial display of Spring Framework
What are the updated features of java 13
Looking back on the basics of Java
Output of the book "Introduction to Java"
Features of spring framework for java developers
The story of writing Java in Emacs
[Java] Check the number of occurrences of characters
Test the integrity of the aggregation using ArchUnit ③
Primality test Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
JAVA: jar, aar, view the contents of the file
The story of making ordinary Othello in Java
[Android] [Java] Manage the state of CheckBox of ListView
About the official start guide of Spring Framework
Inspect the contents of log output during Java unit test (no mock used)
About the description order of Java system properties
About the idea of anonymous classes in Java
[Java] Access the signed URL of s3 (signed version 2)
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
Try calling the CORBA service from Spring (Java)
I compared the characteristics of Java and .NET
[Java] Be careful of the key type of Map
Organize the differences in behavior of @NotBlank, @NotEmpty, @NotNull with Spring Boot + Thymeleaf
The story of raising Spring Boot 1.5 series to 2.1 series
RSpec-Results of reviewing the test code for'users validation'
Let's check the feel of Spring Boot + Swagger 2.0
Java standard logger
Automatic creation of Java unit test result report
Calculate the similarity score of strings with JAVA
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
[Java / Spring Boot] Spring security ④ --Implementation of login process
Try the free version of Progate [Java II]