如何模拟传递到构造函数中的参数

huangapple 未分类评论66阅读模式
标题翻译

How to mock a parameter that being passed into a constructor

问题

我有一个如下所示的类

public Class CLZA {

     private Object obj1;
     private Object obj2;
     private StringBuilder strBuilder;

     public CLZA (CLZB objB) {
          obj1 = objB.fetchValueOfObj(Request.type1);
          obj2 = objB.fetchValueOfObj(Request.type2);
          strBuilder = new StringBuilder();
     } 

     public double computeValue(...) {
          if (this.obj1 == 1.0) {
              ...
          }
          if (this.obj2 == 2.0) {
              ...
          }
          strBuilder.append("This is the line I got NPE now");
     }
}

现在我正在尝试做类似这样的事情

@RunWith(PowerMockRunner.class)
@PrepareForTest({CLZA.class, StringBuilder.class})
public class ClzATest {

    private CLZA objA;

    @Mock
    private CLZB objB;

    @Before
    public void setUp() throws Exception {
        objA = new CLZA(objB);
        when(objB.fetchValueOfObj(any(Request.class))).thenAnswer(
                invocation -> {
                    Object argument = invocation.getArguments()[0];
                    if (argument.equals(Request.type1)) {
                        return 1.0;
                    } else if (argument.equals(Request.type2)) {
                        return 2.0;
                    } else {
                        return 0.0;
                    }
                });

    }

    @Test
    public void testComputeValue() {
        double value = objA.computeValue(...);
        assertEquals(2.0, value);
    }
}

我有以下问题/问题

 1. 正如您所见我想模拟返回的objB的值在实例化类CLZA时我能否将模拟的objB传递给构造函数我是否需要模拟CLZA如果需要模拟CLZA如何将其与模拟的objB相关联

 2. 现在我一直在获取NullPointerException位于`strBuilder.append("This is the line I got NPE now");`这一行似乎没有初始化strBuilder在这种情况下有谁可以帮助我如何进行测试

非常感谢
英文翻译

I have a class as below

public Class CLZA {

     private Object obj1;
     private Object obj2;
     private StringBuilder strBuilder;

     public CLZA (CLZB objB) {
          obj1 = objB.fetchValueOfObj(Request.type1);
          obj2 = objB.fetchValueOfObj(Request.type2);
          strBuilder = new StringBuilder();
     } 

     public double computeValue(...) {
          if (this.obj1 == 1.0) {
              ...
          }
          if (this.obj2 == 2.0) {
              ...
          }
          strBuilder.append("This is the line I got NPE now");
     }
}

Right now, I am trying to do something like this.

@RunWith(PowerMockRunner.class)
@PrepareForTest({CLZA.class, StringBuilder.class})
public class ClzATest {

    private CLZA objA;

    @Mock
    private CLZB objB;

    @Before
    public void setUp() throws Exception {
        objA = new CLZA(objB);
        when(objB.fetchValueOfObj(any(Request.class))).thenAnswer(
                invocation -> {
                    Object argument = invocation.getArguments()[0];
                    if (argument.equals(Request.type1)) {
                        return 1.0;
                    } else if (argument.equals(Request.type2)) {
                        return 2.0;
                    } else {
                        return 0.0;
                    }
                });
        
    }

    @Test
    public void testComputeValue() {
        double value = objA.computeValue(...);
        assertEquals(2.0, value);
    }
}

I have following questions/issues:

  1. As you can see, I want to mock the value of the objB returned. Can I
    pass the mocked objB to the constructor when I instantiate the Class
    CLZA? should I mock CLZA also? If I need to mock CLZA, how can I
    related it to my mocked objB?

  2. Right now, I keep getting NullPointerException at the line
    strBuilder.append("This is the line I got NPE now");. It seems strBuilder is not instantiated. Can anyone
    help on how to test in this scenario?

Appreciate it.

答案1

得分: 0

原来这个 NPE 是由另一个问题引起的。这个单元测试工作正常。为误导之处道歉。

英文翻译

It turns out the NPE was caused by another issue. This unit testing is working fine. Sorry for the misleading.

huangapple
  • 本文由 发表于 2020年3月4日 08:55:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/60517635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定