2015-08-06 1 views
1

J'ai besoin d'aide pour scalatest et mockito. Je veux écrire test pour une méthode simple générique:Lors de l'utilisation de scalatest avec mockito, le scalatest lève une exception étrange

trait RestClient { 
    def put[T: Marshaller](url: String, data: T, query: Option[Map[String, String]] = None) : Future[HttpResponse] 
} 

ma classe de test:

class MySpec extends ... with MockitoSugar .... { 
    ... 
    val restClient = mock[RestClient] 
    ... 
    "Some class" must { 
    "handle exception and print it" in { 
     when(restClient.put(anyString(), argThat(new SomeMatcher()), any[Option[Map[String, String]])).thenReturn(Future.failed(new Exception("Some exception"))) 
     ... 
    } 
    } 
} 

quand je lance test, il jette exception suivante:

Invalid use of argument matchers! 
4 matchers expected, 3 recorded: 

Ainsi, pourquoi il demande 4 matchers, si ma méthode n'a que 3 paramètres? Est-ce à cause de génériques?

versions:

  • scala 2.11.7
  • scalatest 2.2.4
  • Mockito 1.10.19

Répondre

2

En effet, la notation suivante

def put[T: Marshaller](a: A, b: B, c: C) 

est équivalent à

def put[T](a: A, b: B, c: C)(implicit m: Marshaller[T]) 

Vous avez donc besoin de passer dans un matcher pour le placier:

put(anyA, anyB, anyC)(any[Marshaller[T]]) 
+0

Tu as sauvé ma vie, merci beaucoup! – RBS

+0

Heureux d'aider :-) – Eric