Testing Questions with Answers

14 practice questions on Testing (Spring Boot & JPA) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Easy

Which annotation activates a profile for a test class?

  1. @ActiveProfiles("test")
  2. @Env("test")
  3. @Profile("test") on the test
  4. @TestProfile

Answer: A. @ActiveProfiles("test")

Explanation

@ActiveProfiles sets the active profiles for the test context.

Step-by-step solution

  1. The question asks which annotation activates a profile for a test class.
  2. @ActiveProfiles with the value 'test' sets the active profiles for the test context.
  3. So the answer is option A.

Why the other options are wrong

  • B. @Env is not a Spring test annotation.
  • C. @Profile marks beans that belong to a profile; it does not activate one for the test.
  • D. @TestProfile is not a Spring annotation.

Tip: Use @ActiveProfiles to switch profiles for a test class.

Question 2Easy

@SpringBootTest is used to:

  1. Load the full context
  2. Measure performance
  3. Deploy the app
  4. Test only a single method without Spring

Answer: A. Load the full context

Explanation

It boots the whole context, useful for integration tests.

Step-by-step solution

  1. @SpringBootTest starts the Spring application context for the test.
  2. It loads the full context, which suits integration tests.
  3. So the purpose is option A, load the full context.

Why the other options are wrong

  • B. Measuring performance is not its purpose.
  • C. Deploying the app is not a test feature.
  • D. Testing one method without Spring is done with plain unit tests.

Tip: Use @SpringBootTest for integration tests and plain JUnit with mocks for unit tests.

Question 3Easy

MockMvc is used to:

  1. Benchmark CPU
  2. Mock the database driver
  3. Test controllers in-process
  4. Generate random data

Answer: C. Test controllers in-process

Explanation

mockMvc.perform(get("/api")).andExpect(status().isOk()) tests the MVC layer in-process.

Step-by-step solution

  1. MockMvc sends simulated HTTP requests to the controllers.
  2. It runs inside the test, with no real network server needed.
  3. So it tests controllers in-process, option C.

Why the other options are wrong

  • A. MockMvc does not benchmark the CPU.
  • B. MockMvc does not mock the database driver; it tests the web layer.
  • D. Generating random data is not its purpose.

Tip: MockMvc tests the MVC layer with perform() and andExpect().

Question 4Easy

spring-boot-starter-test includes which libraries?

  1. Selenium and Cucumber
  2. JMeter with load plugins
  3. JUnit 5, Mockito and AssertJ
  4. Postman

Answer: C. JUnit 5, Mockito and AssertJ

Explanation

It also includes Spring Test, Hamcrest and JSONassert.

Step-by-step solution

  1. Spring-boot-starter-test bundles the common test libraries.
  2. It includes JUnit 5 for tests, Mockito for mocks and AssertJ for assertions.
  3. So the answer is option C.

Why the other options are wrong

  • A. Selenium and Cucumber are UI and BDD tools that the starter does not include.
  • B. JMeter is a load-testing tool, not part of the starter.
  • D. Postman is an API client, not a library in the starter.

Tip: Remember the core trio in the starter: JUnit 5, Mockito and AssertJ.

Question 5Medium

Why are pure unit tests of a service (no Spring context) fast?

  1. They skip assertions
  2. They run in production
  3. No context starts
  4. They use a faster JVM

Answer: C. No context starts

Explanation

Constructor injection lets you new the service with mocks directly.

Step-by-step solution

  1. A plain unit test creates the service directly with new and passes in mocks.
  2. No Spring context is started, so there is no startup cost.
  3. So the test is fast because no context starts, option C.

Why the other options are wrong

  • A. Fast unit tests still run their assertions.
  • B. Unit tests run in test code, not in production.
  • D. They run on the same JVM; no special faster JVM is involved.

Tip: Constructor injection lets you test a service with plain new and mocks.

Question 6Medium

Why are @DataJpaTest tests usually independent of each other's data changes?

  1. Each test rolls back at the end
  2. Tests run in separate JVMs
  3. The database is deleted between tests
  4. JPA forbids writes in tests

Answer: A. Each test rolls back at the end

Explanation

@DataJpaTest is transactional, rolling back after each test method by default.

Step-by-step solution

  1. @DataJpaTest runs each test method inside a transaction.
  2. At the end of each test, that transaction is rolled back by default.
  3. So one test's data does not remain for the next, option A.

Why the other options are wrong

  • B. Separate JVMs are not the reason; the changes are rolled back.
  • C. The database is not deleted; the changes are rolled back.
  • D. JPA does allow writes in tests; they are simply rolled back afterwards.

Tip: Each @DataJpaTest method rolls back at the end, so tests do not see each other's data.

Question 7Medium

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) starts:

  1. A mock database
  2. A server on port 80
  3. No server at all
  4. A real server

Answer: D. A real server

Explanation

Useful for end-to-end tests with a real HTTP client.

Step-by-step solution

  1. RANDOM_PORT makes Spring Boot start an embedded server on a random free port.
  2. The tests can then send real HTTP calls to that server.
  3. So it starts a real server, option D.

Why the other options are wrong

  • A. A mock database is not started by this setting.
  • B. Port 80 is not used; the port is random.
  • C. A server is started, so no server at all is wrong.

Tip: Use RANDOM_PORT with a real HTTP client for end-to-end tests.

Question 8Medium

Which annotation overrides properties for a specific test class?

  1. @Value
  2. @TestPropertySource
  3. @Profile
  4. @Primary

Answer: B. @TestPropertySource

Explanation

@TestPropertySource(properties = "feature.x=true") adds high-precedence test properties.

Step-by-step solution

  1. The question asks for an annotation that overrides properties for one test class.
  2. @TestPropertySource adds properties to the test environment with high precedence.
  3. So the answer is option B.

Why the other options are wrong

  • A. @Value injects a property into a field; it does not set test properties.
  • C. @Profile marks beans for profiles; it does not override properties.
  • D. @Primary chooses one bean among several of the same type.

Tip: Use @TestPropertySource to override properties for a single test class.

Question 9Medium

Which annotation creates a test slice for Spring MVC controllers with MockMvc, without loading the full context?

  1. @JsonTest
  2. @SpringBootTest
  3. @DataJpaTest
  4. @WebMvcTest

Answer: D. @WebMvcTest

Explanation

@WebMvcTest loads web-layer beans only; services must be mocked.

Step-by-step solution

  1. The question asks for a test slice that covers Spring MVC controllers.
  2. @WebMvcTest loads only web-layer beans and MockMvc, not the full context.
  3. So the annotation is option D, @WebMvcTest.

Why the other options are wrong

  • A. @JsonTest slices JSON serialisation tests, not controllers.
  • B. @SpringBootTest loads the full context, which the question rules out.
  • C. @DataJpaTest slices the persistence layer, not the controllers.

Tip: Each test slice loads one layer: @WebMvcTest for controllers, @DataJpaTest for repositories.

Question 10Hard

Testcontainers is used in Spring Boot tests to:

  1. Speed up unit tests by skipping Spring
  2. Run real databases in Docker
  3. Package the app as a container image
  4. Mock every HTTP call

Answer: B. Run real databases in Docker

Explanation

Tests run against real databases or brokers in throwaway containers.

Step-by-step solution

  1. Testcontainers starts Docker containers during a test run.
  2. Tests can then use a real database or message broker running in those containers.
  3. So the purpose is option B, running real databases in Docker.

Why the other options are wrong

  • A. It does not skip Spring; it provides real services to the tests.
  • C. Packaging the app as an image is a build task, not what Testcontainers does.
  • D. It runs real services; mocking every HTTP call is a different approach.

Tip: Use Testcontainers when a test must run against the same kind of database that production uses.

Practise all 14 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Spring Boot & JPA topics · All topics