Reloading static variables at the beginning of each unit test

I am testing an SDK with many static methods. And I do not want the operations that I performed in test1 () to affect the operations that I perform in test2 (). At the beginning of each test, I need all the static variables in the SDK to return to an uninitialized state, as if they were not loaded. And then download them again. Any suggestions on how to do this? Are there any provisions in Robolectric? Because I use this for unit testing. In terms of English, what I basically want is a clean list at the beginning of each test.

@Test public void test1() throws Exception { // Setup all the device info values MyDeviceInfoClass.setDeviceModel().equals("Nexus 4"); MyDeviceInfoClass.setDeviceOperatingSystem().equals("Android_3.4b5"); // Verify all the device info values set previously assertTrue(MyDeviceInfoClass.getDeviceModel().equals("Nexus 4")); assertTrue(MyDeviceInfoClass.getDeviceOperatingSystem().equals("Android_3.4b5")); } 

This was the first test, and he succeeded. It should be so. Then in the second test:

 @Test public void test2() throws Exception { // Setup all the device info values MyDeviceInfoClass.setDeviceOperatingSystem().equals("Android_4.2"); //The following line also succeeds if test1() is finished. But I do not want that. This line should throw an assertion error because we did not specify what the device is over here in test2(). assertTrue(MyDeviceInfoClass.getDeviceModel().equals("Nexus 4")); //This will succeed just the way it should be. assertTrue(MyDeviceInfoClass.getDeviceOperatingSystem().equals("Android_4.2")); } 

I do not want the values ​​set in the first test to affect the values ​​obtained in the second test. The test shown above are simple examples. But the SDK, which I am unit testing, is more complex.

To make this clearer, I do not want the values ​​set in test1 () to have any effect on the operations performed in test2 (). If I set the device model in Nexus 4 to test1 (), like this MyDeviceInfoClass.setDeviceModel (). Equals ("Nexus 4"), the second test test2 () should not know about it when I get it through MyDeviceInfoClass.setDeviceModel (). Equals ("Nexus 4"). I want complete isolation between my unit tests.

Also, abandoning static methods is not an option. Please tell me how I can achieve this.

EDIT: Resetting all static variables before the start of the test is not an option due to certain difficulties associated with the project.

+4
source share
1 answer

The only time static variables are unloaded is when the class loader loading the class in question collects garbage.

One way to solve your problem is to use your own class loader. There's a big SO question here that covers this in some detail.

Another option is to simply reset the values ​​to each test. You can provide the annotated @Before method in your test class that will reset them using reflection if necessary.

+3
source

Source: https://habr.com/ru/post/1489981/


All Articles