2017-07-25 60 views
1

我想在Finatra HttpClient中编写以下函数的测试。嘲笑使用Mockito和斯卡拉的通用方法

def executeJson[T: Manifest](
    request: Request, 
    expectedStatus: Status = Status.Ok): Future[T] = {...} 

根据另一个问题在StackOverflow上回答。 mocking generic scala method in mockito。这是一个速记:

def executeJson[T](
    request: Request, 
    expectedStatus: Status = Status.Ok) 
    (implicit T: Manifest[T]): Futuren[T] = {...} 

所以,我想,

verify(httpClientMock, times(1)).executeJson[JiraProject] 
    (argThat(new RequestMatcher(req)))(Matchers.any()) 

不幸的是,它并没有解决我的问题。我仍然有以下错误。

Invalid use of argument matchers! 
0 matchers expected, 1 recorded: 
-> at org.specs.mock.MockitoStubs$class.argThat(Mockito.scala:331) 

This exception may occur if matchers are combined with raw values: 
//incorrect: 
someMethod(anyObject(), "raw String"); 
When using matchers, all arguments have to be provided by matchers. 
For example: 
//correct: 
someMethod(anyObject(), eq("String by matcher")); 

我也试过Matchers.eq(Manifest[JiraProject]),它抱怨说value Manifest of type scala.reflect.ManifestFactory.type does not take type parameters

我是新来的斯卡拉和Mockito,有什么我做错了或我误解?

在此先感谢您的帮助!


发现问题了!所以executeJson实际上需要3个参数 - request,expectedStatus和一个Manifest。但是,因为expectedStatus是可选的,所以我没有明确地通过它,这就是为什么它抱怨。所以,最终的代码应该是verify(httpClientMock, times(1)).executeJson[JiraProject](argThat(new RequestMatcher(req)), Matchers.any[Status])(Matchers.any())

回答

0

你的问题是,你在第一个参数和第二个匹配器中使用等式。尝试为所有参数使用匹配器。

但我在这里感觉到的一个更大的问题是,你试图模拟第三方库 - 这是你应该避免的东西。这也可以解决你的问题。这里有一些额外阅读 - enter link description here

+0

是的,我认为你是对的,我应该避免嘲笑第三方库。 –