Exam 2 - L14 - Introduction to JUnit Flashcards
What is JUnit?
A unit testing framework for Java
It helps you write and run automated tests to make sure code works correctly
What are the key features of JUnit 5?
Modularity, improved architecture, extensibility, enhanced assertions.
What does ‘@Test’ do in JUnit 5?
Marks a method as a test method to be run by JUnit.
When is @BeforeEach used?
Runs before every test method to set up test data.
What does @AfterAll do?
Runs once after all tests have completed, used for cleanup.
What is a best practice when naming tests?
Use meaningful names like ‘shouldReturnTrueWhenInputIsValid’
What is the Arrange-Act-Assert pattern?
A simple testing pattern used to organize unit tests.
Arrange: Set up everything you need: Create objects, initialize variables, configure the environment.
Act: Do the thing you are testing : call the method under test.
Assert : Check that the result is what you expect. Verify the outcome matches the expected behavior.
What pattern should you follow when writing a test?
Arrange-Act-Assert
Why avoid putting logic in tests?
Tests should only check results, not do complex operations.
What is a parameterized test in JUnit 5?
A test that runs multiple times with different inputs
How do you run JUnit 5 tests?
Using an IDE, Maven/Gradle, or the command line.
What tools help with mocking and coverage?
Mockito (mocking), JaCoCo (coverage).
What’s an example of a simple assertion?
assertEquals(4, 2 + 2);
Why isn’t 100% code coverage always a good goal?
You can have 100% coverage but still have bad or useless tests.
How does Maven know which files are tests?
Using ‘<include>**/*Test.java</include.>' in 'pom.xml'</include>
What does @Disabled do?
Skips or disables a test
What does ‘@ExtendWith’ allow you to do?
Add custom extensions to enhance JUnit behavior