Friday 26 February 2021

ExpectedSystemExit: Test System exits

Follow below steps to test system exit codes.

 

Step 1: Create a rule of type ExpectedSystemExit.

@Rule

public final ExpectedSystemExit exit = ExpectedSystemExit.none();

 

Step 2: Use 'expectSystemExitWithStatus' method to test system exit with status code.

exit.expectSystemExitWithStatus(0);

 

SystemExitApp.java

package com.sample.app;

public class SystemExitApp {

	public void exitApp() {
		System.exit(0);
	}

}

 

SystemExitAppTest.java

package com.sample.app.tests;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;

import com.sample.app.SystemExitApp;

public class SystemExitAppTest {
	@Rule
	public final ExpectedSystemExit exit = ExpectedSystemExit.none();

	@Test
	public void exitApp() {
		exit.expectSystemExitWithStatus(0);

		SystemExitApp app = new SystemExitApp();

		app.exitApp();
	}

}

 

You can download complete working application from this link.

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

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment