Thursday 25 February 2021

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

No comments:

Post a Comment