You are assigned to write unit tests for a Spring Boot application where a method in the service layer is interacting with the database. How would you test this method ensuring that any interaction with the database is mocked?

  • Use a live database for testing to ensure realistic results.
  • Configure a separate test database for unit tests.
  • Use a database mocking framework like H2 for testing.
  • Manually mock the database interactions in the test code.
To ensure that database interactions are mocked in unit tests, you should use a database mocking framework like H2 or configure a separate test database. This allows you to simulate database behavior without connecting to a live database.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *