PowerMockito + Junit-模拟系统.getenv [英] PowerMockito+Junit - Mocking System.getenv

查看:119
本文介绍了PowerMockito + Junit-模拟系统.getenv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在课堂上尝试模仿的行是:

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
}

据我所写,如下:

@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)时,仍然发现它为空

On debug, I get null pointer in for loop line. On inspecting System.getenv("values") in codebase, it is still found to be null

请支持该分辨率

编辑:
确切的可复制方案:

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();

    }


推荐答案

在您的测试用例,您正在调用 System.getenv( values)。split(,),但是您没有告诉PowerMock从返回任何内容System.getenv( values),因此您的代码在尝试对null响应调用 split(,)时将抛出NPE来自 System.getenv( values)

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").

我不清楚您的测试目的,但以下测试将通过,它显示了如何在 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

更新

根据上面问题中精确方案的规定,以下测试将通过,即 System.getenv( values) junkclass.tests()中调用时将返回模拟值。

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();
    }
}

这篇关于PowerMockito + Junit-模拟系统.getenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆