Friday 19 February 2021

ProvideSystemProperty: Provide System Properties

ProvideSystemProperty rule is used to set an arbitrary value to given system property. After the test the original value is restored.

 

Example

@Rule
public final ProvideSystemProperty COUNTRY_PROPERTY = new ProvideSystemProperty(COUNTRY_KEY, "India");

@Rule
public final ProvideSystemProperty STATE_PROPERTY = new ProvideSystemProperty(STATE_KEY, "Andhra Pradesh");

 

Find the below working application.

 

ProvideSystemPropertiesTest.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.ProvideSystemProperty;

public class ProvideSystemPropertiesTest {
	private static final String COUNTRY_KEY = "country";
	private static final String STATE_KEY = "state";

	@Rule
	public final ProvideSystemProperty COUNTRY_PROPERTY = new ProvideSystemProperty(COUNTRY_KEY, "India");

	@Rule
	public final ProvideSystemProperty STATE_PROPERTY = new ProvideSystemProperty(STATE_KEY, "Andhra Pradesh");

	@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.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 even use single instance of ProvideSystemProperty to set multiple properties.

 

@Rule

public final ProvideSystemProperty properties = new ProvideSystemProperty(COUNTRY_KEY, "India").and(STATE_KEY, "Andhra Pradesh");

 

ProvideSystemPropertiesTest1.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.ProvideSystemProperty;

public class ProvideSystemPropertiesTest1 {
	private static final String COUNTRY_KEY = "country";
	private static final String STATE_KEY = "state";

	@Rule
	public final ProvideSystemProperty properties = new ProvideSystemProperty(COUNTRY_KEY, "India").and(STATE_KEY,
			"Andhra Pradesh");

	@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.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));
	}
}


You can download complete working application from this link.

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

 




 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment