标题翻译
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:
-
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? -
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.
专注分享java语言的经验与见解,让所有开发者获益!
评论