5. J-Unit Flashcards
(85 cards)
What is JUnit?
JUnit is a widely used testing framework for Java that allows developers to write and run repeatable tests. It supports test-driven development (TDD) and helps ensure that code works as intended.
What are the main annotations used in JUnit?
The main annotations in JUnit include @Test (marks a method as a test method), @Before (executes before each test method), @After (executes after each test method), @BeforeClass (executes once before any test methods in the class), and @AfterClass (executes once after all test methods in the class).
What is the purpose of the @Test annotation?
The @Test annotation is used to indicate that a method is a test method that should be executed by the JUnit framework. It can also include parameters such as expected exceptions and timeout values.
What is the difference between @Before and @BeforeClass?
@Before is executed before each test method in the current class, allowing for setup that is specific to each test. In contrast, @BeforeClass is executed once before any of the test methods in the class, making it suitable for setup that is shared across tests.
Explain the use of assertions in JUnit.
Assertions in JUnit are used to validate the expected outcomes of tests. Common assertion methods include assertEquals, assertTrue, assertFalse, assertNull, and assertNotNull. They help ensure that the code behaves as expected.
How can you handle exceptions in JUnit tests?
You can handle exceptions in JUnit tests using the ‘expected’ parameter of the @Test annotation, which allows you to specify that a test should throw a particular exception. Alternatively, you can use try-catch blocks within the test method and use assertions to verify that the exception was thrown.
What is a test suite in JUnit?
A test suite in JUnit is a collection of test classes that can be run together. It allows for organizing and executing multiple tests as a single unit, making it easier to manage large test sets.
Describe the use of the @Ignore annotation.
The @Ignore annotation is used to temporarily disable a test method or a test class. This is useful when a test is not ready to run or is known to fail, allowing developers to focus on other tests without removing the test code.
What is the purpose of the @RunWith annotation?
The @RunWith annotation is used to specify a custom runner for a test class. It allows for the extension of JUnit’s functionality by providing custom behavior during the test execution process.
What is parameterized testing in JUnit?
Parameterized testing in JUnit allows a test to be run multiple times with different sets of input values. This is achieved using the @RunWith(Parameterized.class) annotation, enabling the testing of various scenarios with minimal code duplication.
How do you create a parameterized test in JUnit?
To create a parameterized test in JUnit, you need to define a static method annotated with @Parameters that returns a collection of data. Each set of data will be passed to the test method, which is annotated with @Test.
What is the role of the JUnit test runner?
The JUnit test runner is responsible for executing tests and reporting the results. It handles the lifecycle of tests, including setup and teardown, and provides feedback on the success or failure of each test.
What is the difference between JUnit 4 and JUnit 5?
JUnit 5 introduces a new architecture and is divided into three main components: the JUnit Platform, the JUnit Jupiter API, and the JUnit Vintage API. JUnit 5 provides more powerful and flexible features, such as better support for extensions, tagging, and conditional execution, compared to JUnit 4.
Explain the concept of test fixtures in JUnit.
Test fixtures in JUnit refer to the fixed state of a set of objects used as a baseline for running tests. They ensure that tests can run in a consistent environment, allowing for reliable and repeatable test results.
What is the purpose of mocking in unit testing?
Mocking is used in unit testing to create simulated objects that mimic the behavior of real objects. This allows for testing in isolation, ensuring that tests focus on the unit of work without dependencies affecting the outcome.
Name a popular mocking framework used with JUnit.
Mockito is a popular mocking framework used with JUnit. It allows developers to create mock objects and define their behavior, making it easier to test components in isolation.
What is the use of the @Nested annotation in JUnit 5?
@Nested annotation in JUnit 5 is used to create nested test classes, allowing for better organization of tests that share common setup or context. It helps in grouping related tests together while maintaining a clear structure.
How do you assert exceptions in JUnit 5?
In JUnit 5, you can assert exceptions using the ‘assertThrows’ method, which takes the exception class and a lambda expression that executes the code expected to throw the exception. This allows for concise and clear exception testing.
What is the purpose of the @DisplayName annotation in JUnit 5?
The @DisplayName annotation is used to provide a custom display name for a test class or test method. This enhances readability and makes it easier to understand the purpose of the test when reviewing test results.
What is the Java 8 feature that JUnit 5 leverages for lambda expressions?
JUnit 5 leverages Java 8’s feature of lambda expressions to provide a more concise and expressive way to write tests, especially in scenarios such as asserting exceptions and parameterized tests.
What are extensions in JUnit 5?
Extensions in JUnit 5 provide a way to add custom behavior to tests. They can be used to modify the test execution process, provide additional functionality such as lifecycle callbacks, and integrate with other frameworks.
Explain the purpose of the @TestInstance annotation in JUnit 5.
@TestInstance annotation in JUnit 5 is used to specify the lifecycle of test instances. It can be set to PER_CLASS, meaning a single instance of the test class is created for all tests, allowing for shared state between tests.
How can you skip a test in JUnit 5?
To skip a test in JUnit 5, you can use the @Disabled annotation. This marks the test or test class as disabled, preventing it from being executed until the annotation is removed.
What is the purpose of the @Timeout annotation in JUnit 5?
The @Timeout annotation in JUnit 5 is used to specify a time limit for the execution of a test. If the test exceeds the specified timeout, it will be automatically aborted and marked as failed.