PowerMockito+Junit - 模拟 System.getenv
PowerMockito+Junit - Mocking System.getenv
我在 class 中尝试模拟的行是:
String x[] = System.getenv("values").split(",")
for(int i=0;i<=x.length;i++){
//do something
}
目前我写的如下:
@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");
}
}
在调试时,我在 for 循环行中得到空指针。在检查代码库中的 System.getenv("values") 时,仍然发现它是 null
请赞一下决议
编辑:
确切的问题可复制场景:
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();
}
在您的测试用例中,您正在调用 System.getenv("values").split(",")
但您没有告诉 PowerMock return 来自 System.getenv("values")
的任何内容,因此您的代码将在尝试调用时抛出 NPE split(",")
来自 System.getenv("values")
.
的空响应
我不清楚你的测试目的,但下面的测试会通过,它展示了如何设置对 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]);
}
}
上面的代码会打印出:
ab
cd
更新:
根据上述问题中 "exact scenario" 的规定,以下测试将通过,即 System.getenv("values")
将 return 在 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();
}
}
我在 class 中尝试模拟的行是:
String x[] = System.getenv("values").split(",")
for(int i=0;i<=x.length;i++){
//do something
}
目前我写的如下:
@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");
}
}
在调试时,我在 for 循环行中得到空指针。在检查代码库中的 System.getenv("values") 时,仍然发现它是 null
请赞一下决议
编辑: 确切的问题可复制场景:
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();
}
在您的测试用例中,您正在调用 System.getenv("values").split(",")
但您没有告诉 PowerMock return 来自 System.getenv("values")
的任何内容,因此您的代码将在尝试调用时抛出 NPE split(",")
来自 System.getenv("values")
.
我不清楚你的测试目的,但下面的测试会通过,它展示了如何设置对 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]);
}
}
上面的代码会打印出:
ab
cd
更新:
根据上述问题中 "exact scenario" 的规定,以下测试将通过,即 System.getenv("values")
将 return 在 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();
}
}