mockito/mockito-scala

IdiomaticMockitoSyntax appears not to handle thrown by without annotation

justinallenreeves opened this issue · 0 comments

Using Scala 2.12 and mockito-scala 1.14.4 (upgraded from 1.10.3 to verify bug)

Given a simple candidate for mocking:

  case class Car(id: Int)
  class VehicleService {
   private var cars = Map.empty[Int, Car]
   cars += (0 -> Car(0))
 
    def getCar(id: Int): Option[Car] = cars.get(id)
    def saveCar(car: Car) = {
      cars += car.id -> car;
      car 
    }
  } 
  //test with IdiomaticMockito with ArgumentMatcherSugar mixed into Spec
  val vehicleService = mock[VehicleService]
  val error = new Exception("A wild exception appears")
  error willBe thrown by vehicleService.getCar(any[Int])

will fail to compile unless getCar is annotated @throws(classDef[Exception])

Checked exception is invalid for this method!
Invalid: java.lang.Exception: 
org.mockito.exceptions.base.MockitoException: 
Checked exception is invalid for this method!
Invalid: java.lang.Exception: 

alternative syntax

  //test with IdiomaticMockito with ArgumentMatcherSugar
  val vehicleService = mock[VehicleService]
  val error = new Exception("A wild exception appears")
  vehicleService.getCar(any[Int]) throws error

compiles and behaves as expected