The line i try to mock in class is:
String x[] = System.getenv("values").split(",")
for(int i=0;i<=x.length;i++){
//do something
}
As far I have written is as follows:
@RunWith(PowerMockRunner.class)
@PrepareForTest({System.class})
public class test{
@Test
public void junk(){
PowerMockito.mockStatic(System.class);
PowerMockito.when( System.getenv("values"))).thenReturn("ab,cd");
}
}
On debug, I get null pointer in for loop line. On inspecting System.getenv("values") in codebase, it is still found to be null
Please favour on the resolution
Edit: Exact issue replicable scenario:
package com.xyz.service.impl;
public class Junkclass {
public String tests(){
String xx[] = System.getenv("values").split(",");
for (int i = 0; i < xx.length; i++) {
return xx[i];
}
return null;
}
}
package com.xyz.service.impl
@InjectMocks
@Autowired
Junkclass jclass;
@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();
}