【问题标题】:Junit test fails when run on package, but success when run on fileJunit测试在包上运行时失败,但在文件上运行时成功
【发布时间】:2014-05-15 14:40:51
【问题描述】:

重大更新: 有没有人遇到过这种情况?

我在一个 Maven 项目中使用 JUnit 4.5 和 Mockito 1.7。

我在 testCaseFolder 包中有 testCaseA.java。 如果我打开testCaseA.java,在代码中右击,选择“Run as”-“Junit test”就可以了。 但是如果我右键包,选择“Run as”-“Junit test”,就会失败:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced argument matcher detected!
Somewhere before this line you probably misused Mockito argument matchers.
For example you might have used anyObject() argument matcher outside of verification or stubbing.
Here are examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"));
    at testCaseA.setUP(testCaseA.java:33) 

第 33 行是://MockitoAnnotations.initMocks(this);***//it said this is error***

代码如下:

SomeService service;
@Mock
private CommonService commonService;
@Mock
public Errors errors;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);***//it said this is error***
}

@Test
public void testvalidate() {
    //fail even here is empty
}

【问题讨论】:

  • 经过一番挖掘,我找到了问题所在,我只是不知道为什么以及如何解决。问题不在于 MockitoAnnotations.initMocks(this);问题是mock,@Mock private CommonService commonService;如果我不嘲笑任何东西,它就会被发现。如果在这里模拟,就会出错。
  • 我回滚了您的编辑,因为有一个答案与您的原始版本有关。将来,避免进行使现有问题答案无效的编辑。这可能会导致遇到类似问题的其他人对您的具体错误是什么以及您如何解决它感到困惑。

标签: java junit mockito bamboo


【解决方案1】:

更新 从错误中您得到的应该是正确的。

你的 Mockito.when 语句是错误的。

Mockito.when(commonService.get(Mockito.eq(contactGroupId))).thenReturn(disseminationProfile);

或者

Mockito.when(commonService.get(Mockito.anyLong())).thenReturn(disseminationProfile); 

【讨论】:

  • 不,这没关系,我可以在方法 public void testvalidate() 中对所有代码进行 cets。仍然会得到同样的错误。
  • 谢谢ohiocowboy,我以前看过,这是一个标准的文档,我按照它说的做了,有问题,所以它没有解决我的问题。
  • 您的测试类是否使用@Before public void initMocks() 扩展了一个测试类
  • 你好@ohiocowboy,我的测试类不使用@before扩展测试类.....它本身有@Before方法。
  • @Jack 为什么你不使用 Mockito 1.9.5?我也在乱用 Mockito 的注释。这些在 pas 版本中很麻烦。
猜你喜欢
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
相关资源
最近更新 更多