Java Mockito跳过新对象调用
对于以上内容,我如何跳过方法Java Mockito跳过新对象调用,java,junit,mockito,Java,Junit,Mockito,对于以上内容,我如何跳过方法create.populate() 以下是我所做的: public void createId() { Customer cust = findCustomer(this.custId); Address addr = findAddress(this.addr); UniqueIdCreator create = new UniqueIdCreator(this.custId, this.
create.populate()代码>
以下是我所做的:
public void createId() {
Customer cust = findCustomer(this.custId);
Address addr = findAddress(this.addr);
UniqueIdCreator create = new UniqueIdCreator(this.custId, this.addr, this.name);
create.populate();
this.name.setName(create.customerName.getName());
}
错误:方法“create.populate();”内的空指针
这是给莫基托的 我想你可以做到以下几点:
加
去你的测试班。然后重写你的测试
@RunWith(PowerMockRunner.class)
@PrepareForTest(IdCreator.class)
PS:这只是代码草案。A没有检查。我想您可以执行以下操作:
加
去你的测试班。然后重写你的测试
@RunWith(PowerMockRunner.class)
@PrepareForTest(IdCreator.class)
PS:这只是代码草案。A没有检查它。在较高级别上,您编写的代码不够灵活,无法满足您的要求createId
的实现,无论好坏,都会创建一个具有该行为的UniqueIdCreator的新实例
尽管您可以在此处使用完全依赖项注入并传入一个UniqueIdCreatorFactory
,但将创建内容提取到一个可重写的方法中可能会简单得多:
@Test
public void test {
long custId = 1;
IdCreator objectToTest = Mockito.spy(new IdCreator(cust,addr,null));
when(objectToTest.findCustomer(custId)).thenReturn(mock(Customer.class)).
when(objectToTest.findAddress(custId)).thenReturn(mock(Address.class)).
UniqueIdCreator creatorMock = mock(UniqueIdCreator.class);
PowerMockito.whenNew(UniqueIdCreator.class).withAnyArguments().thenReturn(creatorMock);
Mockito.doNothing().when(creatorMock.populate());
objectToTest.createId();
}
这为您提供了跳过实现所需的所有机会,而无需PowerMock甚至Mockito:
public void createId() {
Customer cust = findCustomer(this.custId);
Address addr = findAddress(this.addr);
UniqueIdCreator create = makeIdCreator();
this.name.setName(create.customerName.getName());
}
protected UniqueIdCreator makeIdCreator() {
UniqueIdCreator create = new UniqueIdCreator(
this.custId, this.addr, this.name);
create.populate();
return create;
}
但是,当然,你也可以使用你的间谍:
@Test
public void test {
Customer cust = new Customer();
cust.setId(1);
cust.setName("test");
Address addr = new Address();
addr.setStreet("test-st");
IdCreator c = Mockito.spy(new IdCreator(cust,addr,null) {
@Override protected UniqueIdCreator makeIdCreator() {
return Mockito.mock(UniqueIdCreator.class);
}
});
getDao.getPresist.add(cust);
getDao.getPresist.add(addr);
c.createId();
}
在较高的层次上,您编写的代码不够灵活,无法满足您的要求createId
的实现,无论好坏,都会创建一个具有该行为的UniqueIdCreator的新实例
尽管您可以在此处使用完全依赖项注入并传入一个UniqueIdCreatorFactory
,但将创建内容提取到一个可重写的方法中可能会简单得多:
@Test
public void test {
long custId = 1;
IdCreator objectToTest = Mockito.spy(new IdCreator(cust,addr,null));
when(objectToTest.findCustomer(custId)).thenReturn(mock(Customer.class)).
when(objectToTest.findAddress(custId)).thenReturn(mock(Address.class)).
UniqueIdCreator creatorMock = mock(UniqueIdCreator.class);
PowerMockito.whenNew(UniqueIdCreator.class).withAnyArguments().thenReturn(creatorMock);
Mockito.doNothing().when(creatorMock.populate());
objectToTest.createId();
}
这为您提供了跳过实现所需的所有机会,而无需PowerMock甚至Mockito:
public void createId() {
Customer cust = findCustomer(this.custId);
Address addr = findAddress(this.addr);
UniqueIdCreator create = makeIdCreator();
this.name.setName(create.customerName.getName());
}
protected UniqueIdCreator makeIdCreator() {
UniqueIdCreator create = new UniqueIdCreator(
this.custId, this.addr, this.name);
create.populate();
return create;
}
但是,当然,你也可以使用你的间谍:
@Test
public void test {
Customer cust = new Customer();
cust.setId(1);
cust.setName("test");
Address addr = new Address();
addr.setStreet("test-st");
IdCreator c = Mockito.spy(new IdCreator(cust,addr,null) {
@Override protected UniqueIdCreator makeIdCreator() {
return Mockito.mock(UniqueIdCreator.class);
}
});
getDao.getPresist.add(cust);
getDao.getPresist.add(addr);
c.createId();
}
可能是模拟构造函数的重复,然后是模拟方法调用。我尝试了这个,但似乎仍然没有跳过。我尝试了mock方法b,然后用donothing调用b。但是tc.methodA仍然会阅读你的尝试。也许我们能找到他们的毛病。不确定你能看到什么样的信息。新对象给我带来了问题,因为我无法跳过它。可能是模拟构造函数的重复,然后是模拟方法调用。我尝试了此操作,但似乎仍然没有跳过。我尝试了mock方法b,然后用donothing调用b。但是tc.methodA仍然会阅读你的尝试。也许我们能找到他们的毛病。不确定你能看到什么样的信息。新对象给我带来了问题,因为我无法跳过它。