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
Sonarqube: Missing Blame Information for the Following Files
How to Create a Java Code to Check Multiple Question
Spring Boot - Json Object Array to Java Array
Webdriver/Pageobject/Findby: How to Specify Xpath With Dynamic Value
How to Get Resources Directory Path Programmatically
How to Pass Authentication Credentials in Vba
How to Unpackage and Repackage a War File
How to Test If Json Collection Object Is Empty in Java
How to Test Class Which Implements Runnable With Junit
Jackson Deserialization Issue for Zoneddatetime
Reading a Text File Character by Character into a 2D Array in Java
How to Change the Background Color of a Textfield Without Changing the Border in Javafx
Custom Pagination by Array List
Alternative to If-Else Statements or Better Approach
Getting Java.Lang.Classnotfoundexception: Javax.Servlet.Servletcontext in Junit
Spring Jpa/Hibernate Transaction Force Insert Instead of Update