我尝试模拟存储库并返回保存的相同对象。
这是我的代码段:
public TransactionType createTransactionType(TransactionTypeEnum type) {
TransactionType transactionType = new TransactionType();
transactionType.setStatus(type.code());
transactionType.setDescription(type.name());
return transactionTypeRepository.save(transactionType);
}
我试过嘲笑这样的事情:
1
public TransactionTypeRepositoryMockBuilder doSave(){
Mockito.when(this.target.save(Mockito.any(TransactionType.class)))
.then(new SaveMock<>())
.then(AdditionalAnswers.returnsFirstArg());
return this;
}
2
public TransactionTypeRepositoryMockBuilder doSave(){
Mockito.when(this.target.save(Mockito.any(TransactionType.class))).then(new SaveMock<>());
return this;
}
3
public TransactionTypeRepositoryMockBuilder doSave(){
Mockito.when(this.target.save(Mockito.any(TransactionType.class))).thenReturn(new TransactionType());
return this;
}
4
public TransactionTypeRepositoryMockBuilder doSave(){
Mockito.when(this.target.save(Mockito.any(TransactionType.class))).then(AdditionalAnswers.returnsFirstArg());
return this;
}
我的构建器类
public class TransactionTypeRepositoryMockBuilder {
private final TransactionTypeRepository target;
private TransactionTypeRepositoryMockBuilder() {
this.target = Mockito.spy(TransactionTypeRepository.class);
}
public static TransactionTypeRepositoryMockBuilder create(){
return new TransactionTypeRepositoryMockBuilder();
}
public TransactionTypeRepository build() {
return this.target;
}
public TransactionTypeRepositoryMockBuilder doSave(){
Mockito.when(this.target.save(Mockito.any(TransactionType.class))).thenReturn(new TransactionType());
return this;
}
}
我收到此错误: _java.lang.NoSuchMethodError:org.mockito.invocation.InvocationOnMock.getArgumentAt(ILjava / lang / Class;)Ljava / lang / Object; _ 我做错了什么?
答案 0 :(得分:0)
在mockito-2.1中有一个update。方法getArgumentAt()
采用不同的参数。所以你必须在你的模拟罐中有不一致的地方。
当(userDeviceService.save(任何(UserDevice.class))) .then(invocation - &gt; invocation.getArgumentAt(0,UserDevice.class));现在:
当(userDeviceService.save(任何())) .then(invocationOnMock - &gt; invocationOnMock.getArgument(0));