我有代码,我填充Resultset用CallableStatement.executeQuery().我已经嘲笑了ResultSet,CallableStatement但为了测试我必须填充的方法ResultSet.
这是我正在测试的方法的代码
ResultSet rset = cs.executeQuery();
while (rset.next()) {
IndexVolatilityImpl tsImpl = new IndexVolatilityImpl();
tsImpl.setTradeDate(rset.getString("trade_date"));
tsImpl.setTradeTime(rset.getString("trade_time"));
tsImpl.setExprDate(rset.getString("expr_date"));
tsImpl.setSymbol(rset.getString("symbol"));
tsImpl.setTradePrice(rset.getDouble("trade_price"));
tsImpl.setContractMonth(rset.getString("contract_month"));
tsImpl.setMilliSecs(rset.getString("trade_time_thou"));
colIndexVolatilityImpl.add(tsImpl);
Run Code Online (Sandbox Code Playgroud)
我已经嘲笑了CallableStatement和ResultSet,因为他们被嘲笑我的rset是空的.我想填充Resultset并按如下方式执行
resultSetMock = Mockito.mock(ResultSet.class);
Mockito.when(resultSetMock.getString("trade_date")).thenReturn("03/10/2011");
Mockito.when(resultSetMock.getString("trade_time")).thenReturn("12:24:56");
Mockito.when(resultSetMock.getString("expr_date")).thenReturn("03/19/2011");
Mockito.when(resultSetMock.getString("symbol")).thenReturn("VIX1");
Mockito.when(resultSetMock.getDouble("trade_price")).thenReturn(Double.valueOf("20.96"));
Mockito.when(resultSetMock.getString("contract_month")).thenReturn("1");
Mockito.when(resultSetMock.getString("trade_time_thou")).thenReturn("165");
Mockito.doReturn(resultSetMock).when(callableStatementMock).executeQuery();
Run Code Online (Sandbox Code Playgroud)
不过rset是null.
我有一个非常简单的测试用例,它使用的是Mockito和Spring Test框架.当我做
when(pcUserService.read("1")).thenReturn(pcUser);
Run Code Online (Sandbox Code Playgroud)
我得到了这个例外.
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
at com.project.cleaner.controller.test.PcUserControllerTest.shouldGetPcUser(PcUserControllerTest.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
Run Code Online (Sandbox Code Playgroud)
我尝试过不同的方法,但继续收到此错误消息.我正在使用Spring 3.1.0.RELEASE和Mockito.请分享并指导我正确的方向.
我正在设置一个类的静态方法.我必须在@Before注释的JUnit设置方法中执行此操作.
我的目标是设置类来调用实际方法,除了我明确模拟的那些方法.
基本上:
@Before
public void setupStaticUtil() {
PowerMockito.mockStatic(StaticUtilClass.class);
// mock out certain methods...
when(StaticUtilClass.someStaticMethod(anyString())).thenReturn(5);
// Now have all OTHER methods call the real implementation??? How do I do this?
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,在StaticUtilClass方法中,public static int someStaticMethod(String s)不幸的是抛出一个RuntimeExceptionif null值.
所以我不能简单地将调用真实方法的明显路线作为默认答案,如下所示:
@Before
public void setupStaticUtil() {
PowerMockito.mockStatic(StaticUtilClass.class, CALLS_REAL_METHODS); // Default to calling real static methods
// The below call to someStaticMethod() will throw a RuntimeException, as the arg is null!
// Even …Run Code Online (Sandbox Code Playgroud) 我是mockito的新手.
需要知道存根与何时的区别
1. stub(cpproxy.getBinList()).toReturn(gettestbins());
2. when(cpproxy.getBinList()).thenReturn(gettestbins());
Run Code Online (Sandbox Code Playgroud)
这两者之间有什么区别?
我遇到了Mockito和Hamcrest的仿制问题.
请假设以下界面:
public interface Service {
void perform(Collection<String> elements);
}
Run Code Online (Sandbox Code Playgroud)
以下测试片段:
Service service = mock(Service.class);
// ... perform business logic
verify(service).perform(Matchers.argThat(contains("a", "b")));
Run Code Online (Sandbox Code Playgroud)
所以我想验证我的业务逻辑实际上是用一个包含"a"和"b"的集合来调用服务.
但是,返回类型contains(...)是Matcher<Iterable<? extends E>>,所以在我的情况下Matchers.argThat(...)返回Iterable<String>,这自然不适用于所需的Collection<String>.
我知道我可以使用Hamcrest hasItem和Mockito中提出的参数捕获器验证不一致,但我非常愿意不这样做.
有什么建议!谢谢!
我无法弄清楚为什么doNothing不能为此工作?有任何想法吗?
@Captor
ArgumentCaptor<GenericClass<someOtherClass>> captor;
...
Mockito.doNothing().when(mockObject.methodToStub(captor.capture()));
Run Code Online (Sandbox Code Playgroud)
错误是:
Exception: when(java.lang.Void) in Stubber cannot be applied to void
我很困惑他们之间有什么区别,在哪种情况下选择哪一个.一些差异可能是显而易见的,比如any和eq,但我把它们都包括在内只是为了确定.
我想知道他们之间的差异,因为我遇到了这个问题:我在Controller类中有这个POST方法
public Response doSomething(@ResponseBody Request request) {
return someService.doSomething(request);
}
Run Code Online (Sandbox Code Playgroud)
并希望在该控制器上执行单元测试.我有两个版本.第一个是简单的,就像这样
@Test
public void testDoSomething() {
//initialize ObjectMapper mapper
//initialize Request req and Response res
when(someServiceMock.doSomething(req)).thenReturn(res);
Response actualRes = someController.doSomething(req);
assertThat(actualRes, is(res));
}
Run Code Online (Sandbox Code Playgroud)
但我想使用像这样的MockMvc方法
@Test
public void testDoSomething() {
//initialize ObjectMapper mapper
//initialize Request req and Response res
when(someServiceMock.doSomething(any(Request.class))).thenReturn(res);
mockMvc.perform(post("/do/something")
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(req))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$message", is("done")));
}
Run Code Online (Sandbox Code Playgroud)
两者都运作良好.但我希望我someServiceMock.doSomething()在MockMvc方法中接收req,或者至少是一个具有相同变量值的对象req(不仅仅是任何Request类),并返回res,就像第一个一样.我知道使用MockMvc方法是不可能的(或者是吗?),因为实际调用中传递的对象总是与mock中传递的对象不同.无论如何我能做到吗?或者这样做甚至有意义吗?或者我应该满意使用any(Request.class)?我试过了eq,same但是所有这些都失败了.
先感谢您.我希望我能很好地解释自己.
考虑一个将接口实现作为参数的函数,如下所示:
interface Callback {
fun done()
}
class SomeClass {
fun doSomeThing(callback: Callback) {
// do something
callback.done()
}
}
Run Code Online (Sandbox Code Playgroud)
当我想测试这个函数的调用者时,我可以做类似的事情
val captor = ArgumentCaptor.forClass(Callback::class)
Mockito.verify(someClass).doSomeThing(captor.capture())
Run Code Online (Sandbox Code Playgroud)
为了测试调用回调时其他类的作用,我可以这样做
captor.value.done()
Run Code Online (Sandbox Code Playgroud)
问题:如果我用高阶函数替换回调接口,我该怎么做呢?
fun doSomeThing(done: () -> Unit) {
// do something
done.invoke()
}
Run Code Online (Sandbox Code Playgroud)
这可以用ArgumentCaptor来完成,我必须使用哪个类 ArgumentCaptor.forClass(???)
我有一个JUnit类,使用不同的方法来执行不同的测试.
我使用Mockito在真实实例上创建一个间谍,然后覆盖一些与我执行的实际测试无关的方法.
有没有办法,只是为了清理我以防万一我的测试后运行的其他一些测试也使用相同的实例,可能会执行一个模拟的方法,他们没有要求模拟,取消模拟方法?
说我有一个名为'wareHouseSpy'的间谍对象
说我重写方法isSomethingMissing:
doReturn(false).when(wareHouseSpy).isSomethingMissing()
Run Code Online (Sandbox Code Playgroud)
什么是正确的方式取消覆盖,并使间谍恢复正常,即使下一个isSomethingMissing的调用运行真正的方法?
就像是
doReturn(Mockito.RETURN_REAL_METHOD).when(wareHouseSpy).isSomethingSpy()
Run Code Online (Sandbox Code Playgroud)
或者可能
Mockito.unmock(wareHouseSpy)
Run Code Online (Sandbox Code Playgroud)
谁知道?我在那个地方找不到任何东西
谢谢!
阿萨夫
让我们假设我在某些服务类中有以下方法:
public SomeEntity makeSthWithEntity(someArgs){
SomeEntity entity = new SomeEntity();
/**
* here goes some logic concerning the entity
*/
return repository.merge(entity);
}
Run Code Online (Sandbox Code Playgroud)
我想测试这个方法的行为,因此想以repository.merge下面的方式模拟:
when(repository.merge(any(SomeEntity.class))).thenReturn(objectPassedAsArgument);
Run Code Online (Sandbox Code Playgroud)
然后模拟的存储库返回makesSthWithEntity传递给它的内容,我可以轻松地测试它.
任何想法我怎么能强迫mockito返回objectPassedAsArgument?
mockito ×10
java ×9
junit4 ×2
unit-testing ×2
controller ×1
difference ×1
generics ×1
hamcrest ×1
junit ×1
kotlin ×1
matcher ×1
mocking ×1
powermock ×1
spring-test ×1
spy ×1
testing ×1