JUnit assertTrue Usage Guide

In JUnit, the assertTrue() method is used to check if a certain condition is true. If the condition is true, the test passes; if the condition is false, the test fails. Its syntax is:

assertTrue(boolean condition);

For example, let’s say we want to test a simple method that checks if a number is greater than 10, we can use the assertTrue() method to verify it:

public class SimpleTest {
    
    @Test
    public void testGreaterThanTen() {
        int num = 15;
        assertTrue(num > 10);
    }
}

In this example, the assertTrue() method will return true and the test will pass if num is greater than 10. If num is less than or equal to 10, the assertTrue() method will return false and the test will fail.

bannerAds