In this section, we will explore the most commonly used assertions in JUnit. Assertions are essential for verifying that the code behaves as expected. They are the foundation of any unit test, allowing you to compare the actual output of your code against the expected output.
Key Concepts
- Assertions: Statements that check whether a condition is true. If the condition is false, the test fails.
- Common Assertions: Frequently used assertions provided by JUnit to validate different types of conditions.
Common Assertions in JUnit
JUnit provides a variety of assertions to help you validate your code. Here are some of the most commonly used ones:
assertEquals
assertEquals
Checks if two values are equal.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class ExampleTest { @Test public void testAddition() { int result = 2 + 3; assertEquals(5, result, "2 + 3 should equal 5"); } }
assertNotEquals
assertNotEquals
Checks if two values are not equal.
import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; public class ExampleTest { @Test public void testSubtraction() { int result = 5 - 3; assertNotEquals(1, result, "5 - 3 should not equal 1"); } }
assertTrue
assertTrue
Checks if a condition is true.
import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; public class ExampleTest { @Test public void testIsPositive() { int number = 5; assertTrue(number > 0, "Number should be positive"); } }
assertFalse
assertFalse
Checks if a condition is false.
import static org.junit.jupiter.api.Assertions.assertFalse; import org.junit.jupiter.api.Test; public class ExampleTest { @Test public void testIsNegative() { int number = -5; assertFalse(number > 0, "Number should not be positive"); } }
assertNull
assertNull
Checks if an object is null.
import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.Test; public class ExampleTest { @Test public void testObjectIsNull() { Object obj = null; assertNull(obj, "Object should be null"); } }
assertNotNull
assertNotNull
Checks if an object is not null.
import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Test; public class ExampleTest { @Test public void testObjectIsNotNull() { Object obj = new Object(); assertNotNull(obj, "Object should not be null"); } }
assertArrayEquals
assertArrayEquals
Checks if two arrays are equal.
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import org.junit.jupiter.api.Test; public class ExampleTest { @Test public void testArrayEquality() { int[] expected = {1, 2, 3}; int[] actual = {1, 2, 3}; assertArrayEquals(expected, actual, "Arrays should be equal"); } }
assertSame
assertSame
Checks if two references point to the same object.
import static org.junit.jupiter.api.Assertions.assertSame; import org.junit.jupiter.api.Test; public class ExampleTest { @Test public void testSameObject() { Object obj1 = new Object(); Object obj2 = obj1; assertSame(obj1, obj2, "Both references should point to the same object"); } }
assertNotSame
assertNotSame
Checks if two references do not point to the same object.
import static org.junit.jupiter.api.Assertions.assertNotSame; import org.junit.jupiter.api.Test; public class ExampleTest { @Test public void testDifferentObjects() { Object obj1 = new Object(); Object obj2 = new Object(); assertNotSame(obj1, obj2, "References should point to different objects"); } }
Practical Exercise
Exercise 1: Basic Assertions
Write a JUnit test to verify the following conditions:
- The sum of 10 and 20 is 30.
- The difference between 20 and 10 is not 15.
- A string "JUnit" is not null.
- An array
{1, 2, 3}
is equal to another array{1, 2, 3}
.
Solution
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class BasicAssertionsTest { @Test public void testBasicAssertions() { // 1. The sum of 10 and 20 is 30 assertEquals(30, 10 + 20, "10 + 20 should equal 30"); // 2. The difference between 20 and 10 is not 15 assertNotEquals(15, 20 - 10, "20 - 10 should not equal 15"); // 3. A string "JUnit" is not null String str = "JUnit"; assertNotNull(str, "String should not be null"); // 4. An array {1, 2, 3} is equal to another array {1, 2, 3} int[] expectedArray = {1, 2, 3}; int[] actualArray = {1, 2, 3}; assertArrayEquals(expectedArray, actualArray, "Arrays should be equal"); } }
Summary
In this section, we covered the most commonly used assertions in JUnit, including assertEquals
, assertNotEquals
, assertTrue
, assertFalse
, assertNull
, assertNotNull
, assertArrayEquals
, assertSame
, and assertNotSame
. These assertions are fundamental for writing effective unit tests, allowing you to validate various conditions in your code. In the next section, we will delve into advanced assertions to further enhance your testing capabilities.
JUnit Course
Module 1: Introduction to JUnit
Module 2: Basic JUnit Annotations
- Understanding @Test
- Using @Before and @After
- Using @BeforeClass and @AfterClass
- Ignoring Tests with @Ignore
Module 3: Assertions in JUnit
Module 4: Parameterized Tests
- Introduction to Parameterized Tests
- Creating Parameterized Tests
- Using @ParameterizedTest
- Custom Parameterized Tests
Module 5: Test Suites
Module 6: Mocking with JUnit
Module 7: Advanced JUnit Features
Module 8: Best Practices and Tips
- Writing Effective Tests
- Organizing Test Code
- Test-Driven Development (TDD)
- Continuous Integration with JUnit