Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the test method with the correct annotation.
JUnit
@[1]
public void testAddition() {
// test code here
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Before or @After instead of @Test
Forgetting the '@' symbol
✗ Incorrect
The @Test annotation marks a method as a test method in JUnit.
2fill in blank
mediumComplete the code to create an instance of Calculator in the Arrange step.
JUnit
public void testAddition() {
Calculator calculator = new [1]();
int result = calculator.add(2, 3);
// assertions
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names instead of class names
Using lowercase instead of class name capitalization
✗ Incorrect
In the Arrange step, we create an object of the class we want to test, here Calculator.
3fill in blank
hardFix the error in the assertion statement to check the expected result.
JUnit
assertEquals([1], result); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the actual result as expected value
Using a string instead of an integer
✗ Incorrect
The expected value should be the number 5, not the variable or string.
4fill in blank
hardFill both blanks to complete the Arrange and Act steps correctly.
JUnit
Calculator calculator = new [1](); int result = calculator.[2](4, 6);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong class name for object creation
Calling a method that does not exist or is incorrect
✗ Incorrect
We create a Calculator object and call its add method.
5fill in blank
hardFill all three blanks to complete the Arrange, Act, and Assert steps in the test method.
JUnit
@Test
public void testMultiply() {
Calculator calculator = new [1]();
int result = calculator.[2](3, 7);
assertEquals([3], result);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong class name
Calling wrong method
Asserting wrong expected value
✗ Incorrect
The test creates a Calculator, calls multiply with 3 and 7, and asserts the result is 21.