Thursday 25 February 2021

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

No comments:

Post a Comment