Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IdiomaticMockitoSyntax appears not to handle thrown by without annotation #251

Closed
justinallenreeves opened this issue Jun 15, 2020 · 0 comments

Comments

@justinallenreeves
Copy link

justinallenreeves commented Jun 15, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants