How to Mock System.Getproperty Using Mockito

MockitoException when trying to mock java.lang.System

While Mockito since 3.4.0 version allows mocking static methods it is not allowed to mock the Thread and System static methods, see this comment on github

Finally note that Mockito forbids mocking the static methods of System (and Thread). Those methods are to much cemented into class loading which happens in the same thread. At some point, we might add instrumentation to class loading to temporarily disable the static mocks within it to make mocking these classes, too, where we also would need to disable their intensification properties. You can however easily mock Instant.now().

Set system property in jmockit unit test

Make sure you call System.setProperty("property.name", "https://justatest.com/dav/bulk"); before the class being tested is instantiated, otherwise the static field will always be null.

Consider using a @BeforeClass setup method for this:

@BeforeClass
public static void setup() {
System.setProperty("property.name", "https://justatest.com/dav/bulk");
// Instantiate your CsvConfig instance here if applicable.
}

and then

@Test
public void getUrl(){
System.setProperty("property.name", "https://justatest.com/dav/bulk");
String fileUrl = csvConfig.getFileUrl();
assertEquals(fileUrl, "https://justatest.com/dav/bulk/otherstuff");
}

PowerMockito+Junit - Mocking System.getenv

In your test case you are invoking System.getenv("values").split(",") but you haven't told PowerMock to return anything from System.getenv("values") so your code wil throw an NPE when it attempts to invoke split(",") on the null response from System.getenv("values").

The purpose of your test isn't clear to me but the following test will pass and it shows how to set an expectation on System.getenv("values"):

@Test
public void junk() {
String input = "ab,cd";

PowerMockito.mockStatic(System.class);

// establish an expectation on System.getenv("values")
PowerMockito.when(System.getenv("values")).thenReturn(input);

// invoke System.getenv("values") and assert that your expectation was applied correctly
Assert.assertEquals(input, System.getenv("values"));

String x[] = System.getenv("values").split(",");
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
}

The above code will print out:

ab
cd

Update:

Based on the provision of the "exact scenario" in the above question, the following test will pass i.e. System.getenv("values") will return the mocked value when invoked in junkclass.tests() ...

@RunWith(PowerMockRunner.class)
@PrepareForTest({System.class, junkclass.class})
public class Wtf {

@Test
public void junk() {
String x = "ab,cd";

PowerMockito.mockStatic(System.class);

// establish an expectation on System.getenv("values")
PowerMockito.when(System.getenv("values")).thenReturn(x);

// invoke System.getenv("values") and assert that your expectation was applied correctly
Assert.assertEquals(x, System.getenv("values"));
jclass.tests();
}
}


Related Topics



Leave a reply



Submit