Java Mockito NotaMockException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29611893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito NotaMockException
提问by learningMyWayThru
I am facing an issue with Mockito junit testing. I am new to it and am a bit confused with the problem I am facing. Any help on this would be appreciated.
我正面临 Mockito junit 测试的问题。我是新手,对我面临的问题有点困惑。对此的任何帮助将不胜感激。
class Activity{
public void firstMethod(){
String str = secondMethod();
}
public String secondMethod(){
String str = null;
/* some Code */
return str;
}
}
Getting exception :
获取异常:
*org.mockito.exceptions.misusing.NotAMockException:
Argument passed to when() is not a mock!*
in the below code
在下面的代码中
class ActivityTest(){
Activity act;
@Before
public void setup(){
act = new Activity();
}
@Test
public void testFirstMethod(){
Mockito.doReturn(Mockito.anyString()).when(act).secondMethod();
act.firstMethod();
verify(act).secondMethod();
}
}
I am aware that activity is not a mock but I am not sure for a way around this as secondMethod()
is a method in the same class. I need to write rule for secondMethod()
as I have already done its Unit Testing. The definition of secondMethod()
consists has external dependencies. Should I be mocking the external dependencies present in secondMethod()
and writing rules for them rather than rule for secondMethod()
?
我知道活动不是模拟,但我不确定有没有办法解决这个问题,就像secondMethod()
同一个类中的方法一样。我需要编写规则,secondMethod()
因为我已经完成了单元测试。contains 的定义secondMethod()
具有外部依赖关系。我应该嘲笑存在于其中的外部依赖项secondMethod()
并为它们编写规则而不是规则secondMethod()
吗?
I found this post: Mockito Spy'ing on the object being unit testedHowever separating the secondMethod() into a different class does not make sense. My method is related to this class. Creating a different class for testing does not seem right to me. Even mocking the actual class using spy() is not the most correct way as already explained in the post.
我找到了这篇文章: Mockito Spy'ing on the object being unittesting 然而,将 secondMethod() 分成不同的类是没有意义的。我的方法与这个类有关。为测试创建一个不同的类对我来说似乎不合适。即使使用 spy() 模拟实际类也不是最正确的方法,正如帖子中已经解释的那样。
I don't think I should be creating a mock of the Activity class as that is the class I am testing. I would really appreciate help and insights into this.
我认为我不应该创建 Activity 类的模拟,因为这是我正在测试的类。我真的很感激对此的帮助和见解。
采纳答案by Mureinik
As you noted, act
is not a mock, and therefore you cannot record behavior on it. You could use Mockito.spy
to, well, spy (or partially mock) the act
object so that you only record the behavior of secondMethod
and execute the actual code for firstMethod
.
正如您所指出的,act
不是模拟,因此您无法在其上记录行为。你可以使用Mockito.spy
,好了,间谍(或部分模拟)的act
对象,所以你只记录的行为secondMethod
和执行实际的代码firstMethod
。
Note, however, that matchers can't be used in doReturn
calls regardles of how you're mock
ing or spy
ing your object. A return value must be a concrete object.
但是请注意,doReturn
无论您是如何调用mock
或调用spy
对象,都不能在调用中使用匹配器。返回值必须是具体的对象。
class ActivityTest() {
Activity act;
@Before
public void setup(){
act = Mockito.spy(new Activity()); // Here!
}
@Test
public void testFirstMethod(){
Mockito.doReturn("someString").when(act).secondMethod();
act.firstMethod();
verify(act).secondMethod();
}
}
A slightly more elegant syntax allows you to use annotations instead of explicitly calling Mockito.spy
, but it's a matter of taste really:
稍微优雅的语法允许您使用注释而不是显式调用Mockito.spy
,但这确实是一个品味问题:
@RunWith(MockitoJUnitRunner.class)
class ActivityTest() {
@Spy
Activity act = new Activity();
@Test
public void testFirstMethod(){
Mockito.doReturn("someString").when(act).secondMethod();
act.firstMethod();
verify(act).secondMethod();
}
}
回答by Pavan Kumar
Here are some hints:
这里有一些提示:
- Mock the Activity.
- Tweak the behavior of secondMethod with when / then / doReturn
- Use doCallRealMethod when firstMethod is invoked.
- 模拟活动。
- 使用 when / then / doReturn 调整 secondMethod 的行为
- 调用 firstMethod 时使用 doCallRealMethod。
Hope it helps.
希望能帮助到你。