The system.out.println function is used to display the application output in the console window. In some cases, when you run the test methods, the System.out.println command out may not be printed in the console window. The test output was not visible in the console. If any test case failed during the test, the current output of the test could not be seen. The test output could not be asserted without knowing the outcome of the test case. To debug the test case failure, use System.out.println to print the test output in the console window.

Spring boot JUnit test cases are created in order to perform unit testing in the application. The JUnit test cases suppress the logs and provide a summary of the test case execution, such as how many test cases are executed, how many test cases fail, how many test cases are in error, and how many test cases are skipped without executing. If there is an error or failure in the test cases, it is critical to understand why the test case failed. The actual data used in the test cases should be known in order to debug the failed or error thrown test cases. The system.out.println function will assist in determining the actual value.

It is difficult to determine the cause of failed or error-thrown test cases if system.out.println is not working in the test case. In this section, we’ll look at how to get system.out.println to print the output in the console window.

Solution 1

When the logger is enabled in the spring boot application, all system.out.println output is routed to a log file. An error may have occurred during the creation of the logger file or while writing to the logger file. The log will not be visible in the log file or the console window. Check the logger configuration in the file located in both the src/main/resources and the src/test/resources folders. In the resources folder, logger configuration files such as, logbook.xml, and log4j.xml may be added. Analyze these logger-related configurations in the application and remove them. The output will be printed in the console window by the System.out.println command.


Solution 2

The logger will be set up via the command line. The external configuration will disable the system.out.println when the spring boot application starts.  To print the output in the console window, use system.out.println. Check the spring boot application’s command line invocation. If any of the following code is added, remove the logger code to print the system.out.println to output in the console window

java -Dspring.main.banner-mode=log -Dlogging.pattern.console= -jar logging.file=logs/test.log SpringBootLogApplication-0.0.1.jar

Solution 3

The system.out.println command buffers the content rather than sending it to the console window. You won’t be able to see the console output until the buffer is full. In this case, you must use the system.flush command to flush the buffer. The flush command sends the content of the buffer to the console window and clears it.


Solution 4

If the default out writer buffer is changed or redirected to another custom written buffer, system.out.println will not print. The code below will redirect the default buffer to the custom buffer. The output will not be printed to the console window by the code. The custom written buffer is either flushed without printing or redirected to a file. Analyze the code below, which has been added to the application. Remove any existing code or flush the output to the console window.

package com.yawintutor.repository;

import static org.junit.jupiter.api.Assertions.assertEquals;


import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

public class EmployeeSalaryTest {

	private final PrintStream defaultOut = System.out;
	private final PrintStream defaultErr = System.err;
	private final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
	private final ByteArrayOutputStream errStream = new ByteArrayOutputStream();

	public void setUpStreams() {
		System.setOut(new PrintStream(outStream));
		System.setErr(new PrintStream(errStream));

	public void restoreStreams() {

	public void outTest() {
		assertEquals("test", outStream.toString());

	public void errTest() {
		assertEquals("test", errStream.toString());

Solution 5

The following code will print the system.out.println output to the console window in its entirety. Run the spring boot application after adding the following code to see the system.out.println in the console window.


package com.yawintutor.service;

import org.springframework.stereotype.Component;

public class EmployeeSalary {
	public String getDesignation() {
		String designation = "Manager";
		System.out.println("EmployeeSalary : getDesignation : " + designation);
		return designation;


package com.yawintutor.service;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.yawintutor.service.EmployeeSalary;

public class EmployeeSalaryTest {

	EmployeeSalary employeeSalary;
	public void getDesignationTest() {
		System.out.println("Testing EmployeeSalaryTest -> getDesignationTest starting");
		String designation = employeeSalary.getDesignation();
		System.out.println("Testing EmployeeSalaryTest -> getDesignationTest end");


2022-01-31 15:34:21.567  INFO 81819 --- [           main] c.y.service.EmployeeSalaryTest        : Started EmployeeSalaryTest in 3.824 seconds (JVM running for 4.761)
Testing EmployeeSalaryTest -> getDesignationTest starting
EmployeeSalary : getDesignation : Manager
Testing EmployeeSalaryTest -> getDesignationTest end
2022-01-31 15:34:21.700  INFO 81819 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

Leave a Reply