Advanced JUnit & Mockito test (Advanced)
40 questions · 40 minutes · Free
Test design, coverage, testing legacy code and integration tests.
Take the Advanced JUnit & Mockito test
What it covers
- Java test design
- Code coverage with JaCoCo
- Testing legacy Java code
- Java integration tests with Testcontainers
- Mutation testing
Interview format
- Questions: 40 questions — 24 multiple choice, 12 multi-answer, 4 predict-the-output
- Time: 40 minutes, timed from the first question
- Difficulty: Advanced
- Skill area: Testing & QA
- Price: Free
Earn a certificate
The Advanced JUnit & Mockito test is one of the 8 tests of the Software Engineer (Java): Advanced learning path. Pass it and the other 7 tests at 70% or more and readyforhires issues you a verifiable certificate in your name, with its own code and verify link to share or add to LinkedIn.
Sample questions
5 sample questions from the Advanced JUnit & Mockito test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
What is the correct approach when working with Testcontainers for Cassandra to ensure your tests are resilient to container configuration changes?
- Ignore port mapping and directly use the default Cassandra port (9042)
- Use the container's `getJdbcUrl()` method to retrieve the connection string
- Hard-code the Cassandra connection URL and port in the test configuration
- Use an in-memory Cassandra implementation instead of Testcontainers
Answer: B. Use the container's `getJdbcUrl()` method to retrieve the connection string
When using Testcontainers for Cassandra, it is a common mistake to hard-code port numbers or connection details. Instead, the container's `getJdbcUrl()` method should be used to dynamically retrieve the correct connection string, ensuring that the test is resilient to changes in port mappings and other container-specific configurations.
Which of the following statements accurately describes the design of JUnit 5's Jupiter test engine?
- JUnit 5's Jupiter engine is based on a plugin system that dynamically loads test runners at runtime, increasing complexity and reducing performance.
- JUnit 5's Jupiter engine uses a monolithic architecture to ensure maximum compatibility with legacy JUnit 3 and 4 tests.
- JUnit 5's Jupiter engine is designed for performance and simplicity, using a modular architecture that separates the test engine from the platform and extensions.
- JUnit 5's Jupiter engine is a direct replacement for the JUnit 4 engine with no changes in architecture or design.
Answer: C. JUnit 5's Jupiter engine is designed for performance and simplicity, using a modular architecture that separates the test engine from the platform and extensions.
JUnit 5's Jupiter engine is designed for performance and simplicity, using a modular architecture that separates the test engine from the platform and extensions. This architecture allows for a more streamlined and efficient testing process. The other options are incorrect because they either describe features of other test engines or are not accurate descriptions of Jupiter's design.
Which of the following statements accurately describes how JUnit 5 manages test instances by default?
- JUnit 5 creates a new test instance per test method by default (PER_METHOD), so instance fields do not leak between tests.
- JUnit 5 creates a single test instance for all test methods in a class, allowing instance fields to be reused across tests.
- JUnit 5 creates a new test instance only when explicitly annotated with @TestInstance(PER_METHOD).
- JUnit 5 creates a new test instance for each test class, ensuring that instance fields are reset for each class.
Answer: A. JUnit 5 creates a new test instance per test method by default (PER_METHOD), so instance fields do not leak between tests.
JUnit 5 creates a new test instance per test method by default, ensuring that instance fields do not leak between tests. This helps in maintaining test independence and reliability. The other options are either incorrect or unrelated to the concept being tested.
You are integrating Testcontainers into a project that requires a custom Docker image for testing. Which approach correctly sets up a container using a custom image?
- Use the `@Testcontainers` annotation on the test class and specify the custom image name in a `@DynamicPropertySource` method.
- Define a custom `DockerImageName` using `fromImage("my-custom-image:latest")` and annotate the container field with `@Container`.
- Use the `@Container` annotation with a custom `DockerImageName` but do not override `getImageName()` in the container subclass.
- Use the `@Container` annotation with a custom `DockerImageName` and override the `getImageName()` method in a subclass of `GenericContainer`.
Answer: D. Use the `@Container` annotation with a custom `DockerImageName` and override the `getImageName()` method in a subclass of `GenericContainer`.
To use a custom Docker image with Testcontainers, you must define a `GenericContainer` subclass that overrides `getImageName()` to return the custom image name. The `@Container` annotation is used to declare the container in a test class, and `DockerImageName` is used to specify the image name and tag. Other approaches, such as using a `@Testcontainers` annotation alone or relying on default image names, would not correctly reference the custom image.
You are writing an integration test for a Java application that interacts with MongoDB. How should you use Testcontainers to ensure accurate testing of MongoDB interactions?
- Mock MongoDB interactions using Mockito and avoid running a real MongoDB instance.
- Use Testcontainers to start a MongoDB container and connect to it using the container's JDBC URL.
- Use H2 in-memory database to simulate MongoDB behavior for integration tests.
- Use MongoDB's embedded mode for integration tests without Docker.
Answer: B. Use Testcontainers to start a MongoDB container and connect to it using the container's JDBC URL.
When testing applications that use MongoDB, Testcontainers can be used to spin up a real MongoDB instance in a Docker container. This ensures that the integration tests are running against an actual MongoDB server, which is more reliable than mocks or in-memory databases. The container's JDBC URL should be used to connect to the database, as Testcontainers dynamically assigns ports and provides the correct connection details. Using H2 or other in-memory databases would not accurately replicate MongoDB's behavior, and mocking MongoDB would not catch issues related to its specific features or performance characteristics.
Frequently asked questions
- How many questions are in the Advanced JUnit & Mockito test?
- 40 questions (24 multiple choice, 12 multi-answer, 4 predict-the-output), with 40 minutes to answer them.
- What does the Advanced JUnit & Mockito test cover?
- Java test design, Code coverage with JaCoCo, Testing legacy Java code, Java integration tests with Testcontainers, Mutation testing.
- Is the Advanced JUnit & Mockito test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Advanced JUnit & Mockito test?
- Advanced, in Testing & QA.