• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Unit testing post method with invalid input  RSS feed

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. I want to test this method with invalid input (for example book does not have an author which make thsi variable NULL):


Entity:


Test I tried to write:


Errors:



From Postman I know it is 5xx error:



How should I write this test properly? I tried with expected and actual String but do not know how to make it.
 
Rancher
Posts: 3797
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's not (in my view) much point testing that conrtoller.
It just calls the service (which is correct).
Test the service.

As for your issue, your test mocks out the service and then has the service pas a valid book back from the create call.
If you want it to throw a validation exception then that call should throw the exception.
 
must Janik
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... I do not understand what you've written.  What I should modify to make this test works properly and throw exception.
 
Dave Tolls
Rancher
Posts: 3797
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your test cod you have this:


That says that you have mocked out the bookCrudOperationsService to return the newBook object on a createBook call.

Looking at the error you want to get back it looks like you get an error that is thrown by that service (that is, it is thrown when your service attempts to save the Book).

So, if you want this to test the service then either don't mock out the service (which implies this is an integration test), or simply have the service throw the exception rather than return the book.

Is this supposed to be a unit test (ie testing the controller and only the controller), or an integration test (testing hwo the parts talk to each other)?
 
must Janik
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to test only just this method so it is unit test.
I just want to throw and exception when any variable of the book is NULL.
Hope you understood me.
 
Dave Tolls
Rancher
Posts: 3797
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this method tests for nothing.
All it does is call the service method.
That's why I don't see the point of testing this at all.

But, if you want it to throw an exception then use thenThrow(some exception) rather than thenReturn.
 
must Janik
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okey, I will delete this test.
Thanks for help anyway.
 
must Janik
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey. I want to test service class now.
Here is the method I want to test:



Is this test properly written? Should I change something?

 
Dave Tolls
Rancher
Posts: 3797
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing you really need to test here (similar to the Controller, in fact) is that it calls the correct method in the bookRepository, with the correct value and returns the List unchanged.

One question to ask yourself at this point is whether it shold be doing anything else?
How does it handle a null value sent in?
Should this do some validation, or simply rely on the underlying call to handle that.

This is the sort of thing that would come out if you wrote the tests first, rather than writing tests to match the methods you've already written.
 
must Janik
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've written this test correctly?

If it gets null value (or value that does not exist) it returns no books. Should I test it? If I tried to sent null value to create book, you said it is not necessary to test this method.
 
Dave Tolls
Rancher
Posts: 3797
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case there's probably no point.
Largely because the class was written before the tests were.

Which is what I meant when I said it all depends on what you expect this class to deal with.
What its responsibilities are.

If (as it is at the moment) simply passing the request down and doing no processing of its own, then yes, this test is fine.
 
must Janik
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I write some other tests for this method?
 
Dave Tolls
Rancher
Posts: 3797
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's entirely down to what you want this code to do.
You test the requirements, not the code as written.

if the requirements are simply to call the find method, and not do anything else, then your test covers that.
 
must Janik
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last question.
Here is the service responsible for renting books.
As you can see, there is some logic here.
How am I supposed to unit test it when these methods are void?
 
Dave Tolls
Rancher
Posts: 3797
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You test that the relevant calls are made to the repository classes, using the verfiy methods of Mockito, and test that the correct exceptions are thrown by the method when certain events occur.
 
Dave Tolls
Rancher
Posts: 3797
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if the repository methods are not defined as never returning null, then test what happens when those methods do return null.
 
I do some of my very best work in water. Like this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!