【发布时间】:2015-01-28 17:29:01
【问题描述】:
我有以下测试,它在 IDE 中执行时成功运行,但是当我执行 mvn install 时,它失败并显示以下 msg
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
下面是测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = SpringockitoContextLoader.class,locations={"classpath:conf/test-context.xml"})
public class ServiceTest
{
@ReplaceWithMock
@Autowired
private Object1 object1
@Autowired
private Service service;
@Before
public void setup()
{
MockitoAnnotations.initMocks(this);
}
@Test
public void test()
{//below line is failing while doing mvn install
String validateMe="1234"
Mockito.when(object1.validate(validateMe)).thenReturn(true)
}
}
我需要任何其他额外的配置才能让它在 maven 中工作吗?
更新 在进一步分析来自 IDE 和 maven 的结果的行为之后。我在测试方法中添加了下面的代码,发现从 IDE 运行时 object1 是模拟的并且返回 true,但是从 maven 测试运行时它打印为 false。 知道为什么会发生这种情况
new MockUtil().isMock(object1)
【问题讨论】:
-
你需要用@Mock 代替@ReplaceWithMock 吗?
-
@Bret RepaceWithMock 是 springockito 的东西..它适用于 spring bean 和上下文......正如我所说的测试运行就像通过 IDE 运行时一样
-
这对我来说看起来不正确.. 它甚至可以为您编译吗? “字符串”在那里做什么? Mockito.when(object1.validate(String validateMe)).thenReturn(true)
-
我想你需要一个参数匹配器,比如 anyString()..
-
@unigeek ...我使用的是示例,因为我不应该分享我的公司代码...原始代码与此类似...
标签: testing junit mockito spring-test springockito