• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 89
  • 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: 22784
131
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
Ranch Hand
Posts: 89
  • 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: 2419
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
Ranch Hand
Posts: 89
  • 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: 2419
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic