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

SystemOutRule: Validate output messages

'SystemOutRule' intercepts the writes to 'System.out' and make assertions about the text written to 'System.out'.

 

Example

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

Now, you can write some text to ‘System.out’ and validate the written text using SystemOutRule instance.

@Test
public void writesTextToSystemOut() {
	System.out.print("Hello How Are You!!!!");

	assertEquals("Hello How Are You!!!!", systemOutRule.getLog());
}


SystemOutRuleTest.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.SystemOutRule;

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

	@Test
	public void writesTextToSystemOut() {
		System.out.print("Hello How Are You!!!!");

		assertEquals("Hello How Are You!!!!", systemOutRule.getLog());
	}
}


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

Wednesday, 24 February 2021

RestoreSystemProperties: Restore system properties

RestoreSystemProperties rule undoes changes of system properties when the test finishes.

 

Example

@Rule
public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

Find the below working application.

 

RestoreSystemPropertiesTest.java

package com.sample.app.tests;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;

public class RestoreSystemPropertiesTest {

	private static final String COUNTRY_KEY = "country";
	private static final String STATE_KEY = "state";

	@Rule
	public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

	@BeforeClass
	public static void beforeAllTests() {
		System.out.println("In beforeAllTests()");

		System.setProperty(COUNTRY_KEY, "no_country");
		System.setProperty(STATE_KEY, "no_state");

		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}

	@Before
	public void setup() {
		System.out.println("\nIn setup()");

		System.setProperty(COUNTRY_KEY, "India");
		System.setProperty(STATE_KEY, "Andhra Pradesh");

		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}

	@Test
	public void sysPropTest() {
		System.out.println("\nIn sysPropTest()");

		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));

		assertEquals(System.getProperty(COUNTRY_KEY), "India");
		assertEquals(System.getProperty(STATE_KEY), "Andhra Pradesh");
	}

	@After
	public void cleanUp() {
		System.out.println("\nIn cleanUp()");
		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}

	@AfterClass
	public static void afterAllTests() {
		System.out.println("\nIn afterAllTests()");
		System.out.println("\t" + COUNTRY_KEY + "-> " + System.getProperty(COUNTRY_KEY));
		System.out.println("\t" + STATE_KEY + "-> " + System.getProperty(STATE_KEY));
	}
}


Output

In beforeAllTests()
	country-> no_country
	state-> no_state

In setup()
	country-> India
	state-> Andhra Pradesh

In sysPropTest()
	country-> India
	state-> Andhra Pradesh

In cleanUp()
	country-> India
	state-> Andhra Pradesh

In afterAllTests()
	country-> no_country
	state-> no_state


You can download complete working application from this link.

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

 



 

 

Previous                                                    Next                                                    Home