2

Let's assume that I want to invoke some method of third party API. I want to make sure that the method returns correct result. What type of exception should be thrown when the above method returns incorrect value?

Example code:

// I use third party API
Car car = findCarById(10); 
int speed = car.getSpeed();

// I assume that the value must be >= 0
if (speed < 0) {
    throw new [ExceptionClass]("The speed of car cannot be less than zero");
}

Google Guava provides Verify class that thrown VerifyException but it is not a standard exception. Let's assume that I cannot use Google Guava. What type of exception should be thrown in this case? I also want to known what is an alternative to Verify class? Apache commons provides Validate class - it is suitable tool for the above goal?

user3364391
  • 965
  • 2
  • 8
  • 12
  • 3
    What about creating your own custom Exception take a look at this [How to create custom exceptions in Java?](https://stackoverflow.com/questions/1754315/how-to-create-custom-exceptions-in-java) – Youcef LAIDANI Jun 07 '18 at 17:33
  • 2
    I think that you shouldn't be testing libraries you use. You either trust that they do what they say they do, or you don't trust them and don't use them. Normally, you would have high-level blackbox acceptance testing which would test together your system and third party. – Jaroslaw Pawlak Jun 07 '18 at 17:36
  • This question is highly opinion based. You can throw whatever you want essentially, it's up to you. Here's an opinion: throw java.lang.IllegalArgumentException. – rustyx Jun 07 '18 at 18:02
  • @JaroslawPawlak: you are right but sometimes I have not choice. Writing tests is ok but sometimes the incorrect data may arrive suddenly in runtime. I described this case as comment to another post. – user3364391 Jun 08 '18 at 08:13

2 Answers2

2

javax.validation.ValidationException is a pretty standard choice.

Alternatively, you could create your own exception within your own exception hierarchy (MyFrameworkValidationException, or something of the sort).

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Each API designer defines how that particular API handles exceptions.

Most return a success/error code of some sort.

You would need to examine the documentation for the API you're using for information on how to tell whether it was successful or not.

FWIW, your example does not have anything to do with an API error.

You are just trapping data you don't like. The API has (supposedly) worked correctly, since it returned a result and not an error.

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32
  • 1
    I would like to check that the API works correctly because I don't trust it. Moreover I assume that the result is may not deterministic. For example: the technical data of the car may be downloaded from a website. If the website will be changed then I will potentially get incorrect data. I know that the API designer should prevent it but I don't trust him. – user3364391 Jun 08 '18 at 07:57
  • I have no idea how you can validate the data unless you can obtain it from multiple sources and compare. The Space Shuttle, for example has 3 computers that "vote" on the correct answer. – Terry Carmen Jun 08 '18 at 13:28