Aru*_*run 7 java junit mockito
我正在使用Mockito进行单元测试,我得到以下异常.
org.mockito.exceptions.base.MockitoException:
`'setResponseTimeStampUtc'` is a *void method* and it *cannot* be stubbed with a *return value*!
Voids are usually stubbed with Throwables:
doThrow(exception).when(mock).someVoidMethod();
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. The method you are trying to stub is *overloaded*. Make sure you are calling the right overloaded version.
2. Somewhere in your test you are stubbing *final methods*. Sorry, Mockito does not verify/stub final methods.
3. A spy is stubbed using `when(spy.foo()).then()` syntax. It is safer to stub spies -
- with `doReturn|Throw()` family of methods. More in javadocs for Mockito.spy() method.
Run Code Online (Sandbox Code Playgroud)
这是实际的课程
@Repository
public class CardRepositoryImpl implements ICardRepository {
@Autowired
private OperationBean operation;
@Override
public OperationBean getCardOperation(final String cardHolderId, final String requestTimeStamp) {
operation.setRequestTimeStampUtc(requestTimeStamp);
operation.setResponseTimeStampUtc(DateUtil.getUTCDate());
return operation;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我写过的Test课程.
@RunWith(MockitoJUnitRunner.class)
public class CardRepositoryImplUnitTestFixture_Mockito {
@InjectMocks
private CardRepositoryImpl cardRepositoryImpl;
private OperationBean operationBean;
@Before
public void beforeTest() {
MockitoAnnotations.initMocks(this);
}
@Test
public void canGetCardOperation(){
when(cardRepositoryImpl.getCardOperation("2", Mockito.anyString())).thenReturn(operationBean);
}
}
Run Code Online (Sandbox Code Playgroud)
这是return语句的问题,因为format()方法是最终方法.
public static String getUTCDate() {
final TimeZone timeZone = TimeZone.getTimeZone("UTC");
final Calendar calendar = Calendar.getInstance(timeZone);
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");
simpleDateFormat.setTimeZone(timeZone);
return simpleDateFormat.format(calendar.getTime());
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
@InjectMocks
private CardRepositoryImpl cardRepositoryImpl;
when(cardRepositoryImpl.getCardOperation("2", Mockito.anyString()))
.thenReturn(operationBean);
Run Code Online (Sandbox Code Playgroud)
cardRepositoryImpl不是模拟,所以你不能存根。随着@InjectMocks你指示的Mockito构建一个真正的CardRepositoryImpl,并有自己的私人领域,并与测试用例领域取代构造函数的参数。你可能想改用间谍;稍后会详细介绍。
但首先,为什么会出现该错误消息?因为它不是模拟,所以调用cardRepositoryImpl.getCardOperation发生在实际的 CardRepositoryImpl 实例上。Mockito 看到与operationBean(似乎是模拟)的交互,将when和thenReturn视为与最近的模拟调用相对应,并错误地告诉您您正在使用返回值存根 void 方法(重要的是,错误的方法)。
在 CardRepositoryImpl 的测试中,您不应该模拟 CardRepositoryImpl,也不应该对其进行存根以返回值:这不会测试任何东西,除了 Mockito 可以工作。您可能应该重新考虑为了让您的测试工作需要存根什么。
但是,当存根一个方法时,您可能希望存根同一个类中的另一个方法。这可以通过间谍来完成:
@Test
public void canGetCardOperation(){
CardRepositoryImpl spyImpl = spy(cardRepositoryImpl);
when(spyImpl.getCardOperation(Mockito.eq("2"), Mockito.anyString()))
.thenReturn(returnValue);
// ...
spyImpl.someOtherMethod(); // Any matching calls to getCardOperation from
// someOtherMethod will use the stub above.
}
Run Code Online (Sandbox Code Playgroud)
旁注:您在存根时相邻地使用anyString和"2"。使用 Matchers like 时anyString,如果您将它们用于任何参数,则需要将它们用于所有参数。在这里查看更多。
| 归档时间: |
|
| 查看次数: |
7730 次 |
| 最近记录: |