Thursday 25 February 2021

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

No comments:

Post a Comment