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
No comments:
Post a Comment