问题描述
所以我开始为我们的 Java-Spring 项目编写测试.
So I started writing tests for our Java-Spring-project.
我使用的是 JUnit 和 Mockito.据说,当我使用 when()...thenReturn() 选项时,我可以模拟服务,而无需模拟它们左右.所以我想做的是,设置:
What I use is JUnit and Mockito. It's said, that when I use the when()...thenReturn() option I can mock services, without simulating them or so. So what I want to do is, to set:
when(classIwantToTest.object.get().methodWhichReturnsAList(input))thenReturn(ListcreatedInsideTheTestClass)
但是无论我使用哪个 when 子句,我总是得到 NullpointerException,这当然是有道理的,因为输入为空.
But no matter which when-clause I do, I always get a NullpointerException, which of course makes sense, because input is null.
此外,当我尝试从对象模拟另一个方法时:
Also when I try to mock another method from an object:
when(object.method()).thenReturn(true)
我还得到了一个空指针,因为该方法需要一个未设置的变量.
There I also get a Nullpointer, because the method needs a variable, which isn't set.
但我想使用 when()..thenReturn() 来绕过创建这个变量等等.我只是想确保,如果任何类调用此方法,那么无论如何,只需返回 true 或上面的列表.
But I want to use when()..thenReturn() to get around creating this variable and so on. I just want to make sure, that if any class calls this method, then no matter what, just return true or the list above.
这基本上是我的误解,还是有其他问题?
Is it a basically misunderstanding from my side, or is there something else wrong?
代码:
public class classIWantToTest implements classIWantToTestFacade{
@Autowired
private SomeService myService;
@Override
public Optional<OutputData> getInformations(final InputData inputData) {
final Optional<OutputData> data = myService.getListWithData(inputData);
if (data.isPresent()) {
final List<ItemData> allData = data.get().getItemDatas();
//do something with the data and allData
return data;
}
return Optional.absent();
}
}
这是我的测试课:
public class Test {
private InputData inputdata;
private ClassUnderTest classUnderTest;
final List<ItemData> allData = new ArrayList<ItemData>();
@Mock
private DeliveryItemData item1;
@Mock
private DeliveryItemData item2;
@Mock
private SomeService myService;
@Before
public void setUp() throws Exception {
classUnderTest = new ClassUnderTest();
myService = mock(myService.class);
classUnderTest.setService(myService);
item1 = mock(DeliveryItemData.class);
item2 = mock(DeliveryItemData.class);
}
@Test
public void test_sort() {
createData();
when(myService.getListWithData(inputdata).get().getItemDatas());
when(item1.hasSomething()).thenReturn(true);
when(item2.hasSomething()).thenReturn(false);
}
public void createData() {
item1.setSomeValue("val");
item2.setSomeOtherValue("test");
item2.setSomeValue("val");
item2.setSomeOtherValue("value");
allData.add(item1);
allData.add(item2);
}
推荐答案
你还没有存根的方法的默认返回值是 false
布尔方法,一个空集合或映射的方法返回集合或映射,否则 null
.
The default return value of methods you haven't stubbed yet is false
for boolean methods, an empty collection or map for methods returning collections or maps and null
otherwise.
这也适用于 when(...)
中的方法调用.在您的示例中 when(myService.getListWithData(inputData).get())
将导致 NullPointerException 因为 myService.getListWithData(inputData)
是 null - 之前没有被存根.
This also applies to method calls within
when(...)
. In you're example when(myService.getListWithData(inputData).get())
will cause a NullPointerException because myService.getListWithData(inputData)
is null
- it has not been stubbed before.
一种选择是为所有中间返回值创建模拟并在使用前存根它们.例如:
One option is create mocks for all intermediate return values and stub them before use. For example:
ListWithData listWithData = mock(ListWithData.class);
when(listWithData.get()).thenReturn(item1);
when(myService.getListWithData()).thenReturn(listWithData);
或者,您可以在创建模拟时指定不同的默认答案,以使方法返回新模拟而不是 null:
RETURNS_DEEP_STUBS
Or alternatively, you can specify a different default answer when creating a mock, to make methods return a new mock instead of null:
RETURNS_DEEP_STUBS
SomeService myService = mock(SomeService.class, Mockito.RETURNS_DEEP_STUBS);
when(myService.getListWithData().get()).thenReturn(item1);
你应该阅读Mockito.RETURNS_DEEP_STUBS 更详细地解释了这一点,并且还有一些关于其用法的警告.
You should read the Javadoc of Mockito.RETURNS_DEEP_STUBS which explains this in more detail and also has some warnings about its usage.
我希望这会有所帮助.请注意,您的示例代码似乎有更多问题,例如缺少 assert 或 verify 语句以及在模拟上调用 setter(没有任何效果).
I hope this helps. Just note that your example code seems to have more issues, such as missing assert or verify statements and calling setters on mocks (which does not have any effect).
这篇关于Mockito - 存根方法时出现 NullpointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!