0

How to write Junit test case for Log statement which is written inside a method in service or controller class.

1 Answers1

0

You can try OutputCaptureExtension which will capture all things that are printed out by System.out and System.err. An example is :

@ExtendWith(OutputCaptureExtension.class)
public class MyTest {

    @Test
    public void theTest(CapturedOutput output) {
        FooService fooService = new FooService();
        fooService.doSomthing();

        assertThat(output.getOut()).contains("bar");
    }

}

It will then expect that fooService.doSomthing() will print out some logging statements which contains the word "bar" to System.out

Ken Chan
  • 84,777
  • 26
  • 143
  • 172