loggher.warn ()
etc.) in the JUNIT testimport org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Sample {
private Logger logger = LoggerFactory.getLogger(Sample.class);
public void run() {
logger.info("Sample message");
}
}
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());
}
}
times (n)
Recommended Posts