0

Consider I have a course object

public Course(int id, String name, String description, int thumbnailFileId, int locationId, boolean deleted) {

        this.id = id;
        this.name = name;
        this.description = description;
        this.thumbnailFileId = thumbnailFileId;
        this.locationId = locationId;
        this.deleted = deleted;
}

As you can see my course has 3 values as int/long. When it comes time to send my course object to rest assured to be sent off in the request body, I am now limited to what type of tests I can do.

I cannot test the service when LocationId is greater than the max value of long it is expecting, in this instance should we use everything as String instead because then we are not restricted by values supported by the primitive types of int/long? With strings we can throw any data we like at the service e.g "2891738917321897321871", "StringInsteadOfLong"

So whats the bad sides to this? I assume we are skewing the test results somewhat? how in industry do we best overcome this problem?

Summary: I want to test values greater than maxLong or sending a word "foobar" when expecting a long at the server side but my Object variables are of type int/long for those variables because that is what the service expects and returns and I am deserializing such responses to the same object (course) that I am constructing to send in the request

symon
  • 670
  • 1
  • 7
  • 20
  • If I understand, you want to test how your backend code behaves if you send values that are out of range, is that right? That won't really test your code. It will test the framework and its JSON unmarshaller. If you really want to do that, why not just generate JSON by hand instead of mapping a Course to JSON? – JB Nizet Oct 26 '17 at 18:18
  • I guess because its just one small test in the grand scheme of things and the other 40 tests are all using this course object successfully, I just find it restricting for these 2-3 scenarios, should I be having course as all String instead of the int/long to cater to this? or bin the test altogether – symon Oct 26 '17 at 18:21
  • for these tests I may be best just building the test code out to explicitly cater to it and keep my course object as it is – symon Oct 26 '17 at 18:22
  • So you want to compromise the type safety and correctness of your object 'and your API) in order to test what happens when values are out of range? That's not a good idea at all. And that won't allow testing out of range values anyway, because nothing will be out of range if the values are not numbers anymore. – JB Nizet Oct 26 '17 at 18:23
  • to add a bit of clarity, the web service is built in c# and im writing automated tests against it in java, I have an internal service which takes a course object and then calls the c# services saveCourse call passing in that data. I am just struggling on how best to check the service for these unexpected invalid inputs – symon Oct 26 '17 at 18:26
  • There are ways that you can validate date before sending the Json to be deserialized. One method is the @JsonDeserialize annotation which lets you deserialize a field with custom code. There are other things like Validators which provide a framework for this: http://www.baeldung.com/spring-data-rest-validators – davesbrain May 18 '18 at 20:31

0 Answers0