1 Answers
π Quick Study Guide: JUnit Assertions
- π― Purpose: JUnit Assertions are static methods used within test methods to verify the expected behavior of the code under test. They compare an actual result with an expected result.
- βοΈ Core Principle: If an assertion fails, the test method immediately terminates, and the test is marked as failed. If all assertions pass, the test is marked as successful.
- π‘ Common Assertions:
- π
assertEquals(expected, actual, [message]): Checks if two values (primitives or objects using.equals()) are equal. - β
assertTrue(condition, [message]): Checks if a condition evaluates to true. - β
assertFalse(condition, [message]): Checks if a condition evaluates to false. - π«
assertNull(object, [message]): Checks if an object reference isnull. - π
assertNotNull(object, [message]): Checks if an object reference is notnull. - π
assertSame(expected, actual, [message]): Checks if two object references point to the exact same object (identity). - βοΈ
assertNotSame(expected, actual, [message]): Checks if two object references point to different objects. - π₯
assertThrows(expectedType, executable, [message]): Verifies that an executable (e.g., a lambda expression) throws an exception of the expected type. - β¨
assertDoesNotThrow(executable, [message]): Verifies that an executable does not throw any exception. - βοΈ
assertArrayEquals(expectedArray, actualArray, [message]): Checks if two arrays are equal element by element.
- π
- π Assertion Messages: Providing a clear, descriptive message as the last argument to an assertion is crucial for debugging failed tests. It helps identify *what* went wrong quickly.
- π Best Practices:
- π Aim for one assertion per test method to make tests focused and easier to understand, or a small group of highly related assertions.
- βοΈ Use descriptive test method names (e.g.,
testAddition_PositiveNumbers_ReturnsCorrectSum) to clearly indicate the test's purpose. - π Keep tests independent and repeatable; each test should be able to run in isolation without relying on the state set up by other tests.
π Practice Quiz: JUnit Assertions
1. Which JUnit assertion is used to verify that two primitive values or objects are equal based on their content (using the equals() method for objects)?
- A)
assertSame() - B)
assertTrue() - C)
assertEquals() - D)
assertNotNull()
2. You want to ensure that a specific method call throws an IllegalArgumentException. Which assertion should you use?
- A)
assertThrows(IllegalArgumentException.class, () -> myMethod()) - B)
assertException(IllegalArgumentException.class, () -> myMethod()) - C)
assertFalse(myMethod() instanceof IllegalArgumentException) - D)
assertTrue(myMethod() throws IllegalArgumentException)
3. What is the primary difference between assertEquals() and assertSame() when comparing two objects?
- A)
assertEquals()compares object references, whileassertSame()compares object content. - B)
assertEquals()compares object content (usingequals()), whileassertSame()compares object references (memory location). - C)
assertEquals()is for primitives,assertSame()is for objects. - D)
assertEquals()is deprecated;assertSame()is the modern approach.
4. Consider the following code snippet:String str = null;
Which assertion would pass for this variable?
- A)
assertNotNull(str, "String should not be null"); - B)
assertFalse(str == null, "String should be null"); - C)
assertNull(str, "String should be null"); - D)
assertEquals("", str, "String should be empty");
5. You have an array int[] actual = {1, 2, 3}; and you expect it to be int[] expected = {1, 2, 3};. Which assertion is most appropriate?
- A)
assertEquals(expected, actual); - B)
assertSame(expected, actual); - C)
assertTrue(expected.equals(actual)); - D)
assertArrayEquals(expected, actual);
6. What is the benefit of providing a message argument in a JUnit assertion (e.g., assertEquals(expected, actual, "Values should match"))?
- A) It improves the performance of the test.
- B) It makes the assertion mandatory.
- C) It provides helpful context in the test failure report, making debugging easier.
- D) It automatically corrects the actual value if it doesn't match the expected.
7. Which of the following is considered a good practice for writing JUnit test methods?
- A) Writing a single test method that asserts multiple unrelated conditions.
- B) Using generic test method names like
test()ortest1(). - C) Aiming for one focused assertion per test method (or a small group of related assertions).
- D) Relying solely on print statements for debugging instead of assertion messages.
π Click to see Answers
- C)
assertEquals() - A)
assertThrows(IllegalArgumentException.class, () -> myMethod()) - B)
assertEquals()compares object content (usingequals()), whileassertSame()compares object references (memory location). - C)
assertNull(str, "String should be null"); - D)
assertArrayEquals(expected, actual); - C) It provides helpful context in the test failure report, making debugging easier.
- C) Aiming for one focused assertion per test method (or a small group of related assertions).
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! π