Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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: 22118
114
Eclipse IDE Spring VI Editor Chrome Java 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: 2061
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: 2061
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.
 
today's feeble attempt to support the empire
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic