• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

How to mock database operations to unit test Spring Boot REST API?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a REST API written in Spring Boot.

Now I want to create unit tests with JUnit and Mockito that mock the database operations.

How to do that?

Here is the endpoint that inserts a new customer to the db.



Test:




I'm getting a null pointer exception on the following line:



 
Sheriff
Posts: 22082
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mock doesn't do anything by itself. You need to have something to activate it:

* In JUnit 4, you would annotate your test class with @RunWith(MockitoJUnitRunner.class).
* In JUnit 5, you would annotate your test class with @ExtendWith(MockitoExtension.class). You need this additional dependency.
* If you can't use either, explicitly call MockitoAnnotations.initMocks(this) in your test class. This would preferably be done in an @Before / @BeforeEach method.
 
Jf Okeeffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Thanks for your answer.

I'm using org.junit.jupiter.api that comes with a Spring Boot new install.
Guess that is JUnit 5. Not sure.

Anyway, I could solve the issue with the following code.
Didn't need to add the class level annotations you mentioned.

I had tried with initMocks(this) but didn't work.
Found out that is deprecated.

This code is passing the tests.
Please take a look and let me know if you think it's a good solution.

Thank you.


 
Bartender
Posts: 2038
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, how about doing this:


Here is an example:
https://stackoverflow.com/questions/41168352/unit-test-post-with-webmvctest-mockbean-service-returns-null
 
Jf Okeeffe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Himai,
Thanks for your suggestion.
How does it improve my solution?
 
Himai Minh
Bartender
Posts: 2038
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Jf ,
Nowadays, I see people use @MockBean to inject beans.
Injecting beans is a practice of inversion of control.
 
It's exactly the same and completely different as this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic