📅  最后修改于: 2023-12-03 15:17:06.647000             🧑  作者: Mango
JUnit is a popular testing framework for Java. It provides a set of annotations and APIs to write and execute unit tests. One useful method in JUnit is assertTrue()
, which is used to check if a condition is true. In this article, we will explore how to use assertTrue()
in JUnit.
public static void assertTrue(boolean condition)
The assertTrue()
method takes a boolean value as a parameter. It throws an AssertionError
if the value is false.
Let's look at an example of how to use assertTrue()
in JUnit:
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class ExampleTest {
@Test
public void testCheckAge() {
int age = 25;
assertTrue(age > 18);
}
}
In the above example, we have a test method called testCheckAge()
. It checks if the age
variable is greater than 18 using assertTrue()
. If the condition is false, it will throw an AssertionError
.
assertTrue()
is a handy method in JUnit to check if a condition is true. It helps in writing effective unit tests for your Java code. Make sure to use it wisely while writing your unit tests.