Showing posts with label SystemErrRule. Show all posts
Showing posts with label SystemErrRule. Show all posts

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

SystemErrRule, SystemOutRule: Clear log

Using 'clearLog' method, you can clear the log.

 

SystemOutErrClearLog.java

package com.sample.app.tests;

import static org.junit.Assert.*;

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 SystemOutErrClearLog {
	@Rule
	public final SystemErrRule systemErrRule = new SystemErrRule().enableLog();

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

	@Test
	public void clearErrorLog() {
		System.err.print("Hello World");

		systemErrRule.clearLog();

		System.err.print("ABCD");

		assertEquals("ABCD", systemErrRule.getLog());

	}

	@Test
	public void clearOutLog() {
		System.out.print("Hello World");

		systemOutRule.clearLog();

		System.out.print("ABCD");

		assertEquals("ABCD", systemOutRule.getLog());
	}
}

 

You can download complete working application from this link.

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

 

 

 

 

 

Previous                                                    Next                                                    Home

SystemOutRule, SystemErrRule: Verify binary data

'getLogAsBytes' method is used to verify binary data.

 

Example

assertArrayEquals(data, systemErrRule.getLogAsBytes());

assertArrayEquals(data, systemOutRule.getLogAsBytes());

 

SystemOutErrBinaryDataTest.java

package com.sample.app.tests;

import static org.junit.Assert.assertArrayEquals;

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 SystemOutErrBinaryDataTest {
	@Rule
	public final SystemErrRule systemErrRule = new SystemErrRule().enableLog();

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

	@Test
	public void newLineText() {
		byte[] data = { 2, 3, 5, 7, 11 };

		System.err.write(data, 0, data.length);
		System.out.write(data, 0, data.length);

		assertArrayEquals(data, systemErrRule.getLogAsBytes());
		assertArrayEquals(data, systemOutRule.getLogAsBytes());
	}
}

 

You can download complete working application from this link.

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

 

 

 

Previous                                                    Next                                                    Home

SystemOutRule, SystemErrRule: Handle new line characters

In Linux, new line is treated as \n, whereas in Windows new line is treated as \r\n. Since new line character is different in different operating systems, we need a better way to handle this.

 

If you have new lines in the text, then use ‘getLogWithNormalizedLineSeparator’ method, it replace new line characters with \n.

 

Example

assertEquals(msg, systemErrRule.getLogWithNormalizedLineSeparator());
assertEquals(msg, systemOutRule.getLogWithNormalizedLineSeparator());

 

Find the below working application.

 

SystemOutErrNewLineCharTest.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 SystemOutErrNewLineCharTest {
	@Rule
	public final SystemErrRule systemErrRule = new SystemErrRule().enableLog();

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

	@Test
	public void newLineText() {
		String msg = "Hello\nHow\nAre\tYou\n";

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

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

 

You can download complete working application from this link.

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

 

 


 

Previous                                                    Next                                                    Home

SystemErrRule: Test Error Messages

SystemErrRule is used to intercepts the writes to System.err and make assertions about the text written to System.err.

 

Example

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

 

Now, you can write error messages to ‘System.err’ and validate.

@Test
public void writesTextToSystemErr() {
	System.err.print("You can't access the resource");
	assertEquals("You can't access the resource", systemErrRule.getLog());
}

 

SystemErrRuleTest.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;

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

	@Test
	public void writesTextToSystemErr() {
		System.err.print("You can't access the resource");
		assertEquals("You can't access the resource", systemErrRule.getLog());
	}
}

 

You can download complete working application from this link.

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

 

 

 

 

 

Previous                                                    Next                                                    Home