Thursday 25 February 2021

SystemErrRule, SystemOutRule: Mute log messages

 Some times to increase the speed of executing test cases, you may want to mute the log messages. You can mute the log messages using ‘mute’ method. One problem with mute method is, it mutes all the log messages, irrespective of whether test is passed or not.

@Rule
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().mute();

@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().mute();

 To get more understanding of failed test cases, we require error messages in the log. To achieve this, you can mute the log messages only for successful tests. You can do that by using ‘muteForSuccessfulTests’ method.

 

@Rule
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();

@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

 

SystemOutErrMuteLog.java

package com.sample.app.tests;

import static org.junit.Assert.assertEquals;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;

public class SystemOutErrMuteLog {
	@Rule
	public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();

	@Rule
	public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

	@Test
	public void newLineText() {
		String msg = "Hello World";

		System.err.print(msg);
		System.out.print(msg);

		assertEquals(msg, systemErrRule.getLog());
		assertEquals(msg, systemOutRule.getLog());
	}
}

 

You can download complete working application from this link.

https://github.com/harikrishna553/system-rules

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment