Testing and Debugging Flashcards
Name 3 frameworks you have used?
JUnit
Angular
React Native
What JUnit annotation allows developers to assign one or more tags to a test case, which can be used to filter the test cases that are executed?
@Tag
What JUnit annotation specifies the maximum amount of time that a test case should be allowed to run before it is considered to have failed
@Timeout
What annotation allows developers to specify a custom display name for the test case, which can be more descriptive than the method name.
@Displayname
What JUnit annotation marks a test case that should be executed multiple times with different input parameters.
@ParameterizedTest
What JUnit annotation allows developers to specify a set of input parameters that should be passed to the test case.?
@Parameters
What is JUnit, and what is it used for?
JUnit is a test framework that supports TDD and BDD
It does this by providing different annotations, assertions, and test runners that we can use to write and execute tests
It lets us define tests as methods with annotations that give it context and information on how it should be run
provide a set of assert methods that developers can use to test specific conditions and generate appropriate error messages if the tests fail
provides test runners that execute the tests and report the results to the developer ie., filter test results, run until the test passes
Name 6 the benefits of using JUnit for testing Java applications
Simplifies testing - easier for developers
Automates testing - helps catch errors earlier, making it easier and less costly to fix them
Modular testing - small tests, focus on specific parts of the code
Provides quick feedback - success / failure
Improves code quality
Supports test driven development
What are the most common JUnit annotations, and what are they used for?
@Test
@Before (before each)
@After (after each)
@BeforeClass (before all tests)
@AfterClass (after all tests)
What is the @Before annotation used for?
@Before - method should be run before EACH test
ie., initialise variables, create object
@Before
public void setUp() {
obj = new MyClass();
}
What is the @BeforeClass annotation used for?
@BeforeClass - method should be run once before ANY test method in the class is executed
Ie., initialising a database before any test is executed
@BeforeClass
public static void setUpClass() {
conn = DriverManager.getConnection(
“jdbc:mysql://localhost/mydb”, “user”, “pass”);
}
What is a JUnit test suite, and how is it created?
A test suite is a collection of test cases that are executed together as a group
Describe 5 steps for writing a JUnit test?
- Create a Test class annotated with @Test
- Create a test method and annotate it with @Test
- Write test code inside the test method to TEST THE BEHAVIOUR of the method or class under test
- Use assertions to verify the expected results are produced
- Run the test using a framework like JUnit and verify the tests pass
What is a mock object and how is it used in JUnit testing?
A mock object is a simulated object that mimics the behavior of a real object in a controlled way.
It can be created using frameworks such as Mockito
How do you create a mock object using Mockito?
Mockito.mock() method and then define the behavior of the mock object using the when() method.