A Classic-Style Assertion: assertTrue
Explore how to implement the classic assertTrue assertion in JUnit tests to validate boolean expressions. Understand its usage with Java's Account example, setup using @Before, and how it helps confirm expected behaviors in unit testing.
We'll cover the following...
The most basic Classic-Style Assertion is assertTrue:
org.junit.Assert.assertTrue(someBooleanExpression);
Since assertions are so pervasive in JUnit tests, most programmers use a static import to reduce the clutter:
import static org.junit.Assert.*;
An assertTrue ensures that a value is true, otherwise it gives a false. A couple of examples:
@Test
public void hasPositiveBalance() {
account.deposit(50);
assertTrue(account.hasPositiveBalance());
}
If the parameter in assertTrue evaluates as true, then the above test will pass.
@Test
public void depositIncreasesBalance() {
int initialBalance = account.getBalance();
account.deposit(100);
assertTrue(account.getBalance() > initialBalance);
}
The above test (depositIncreasesBalance) is testing whether the balance in an account is greater than the initial balance after a deposit of 100 is added. The assertTrue assertion makes sure that the condition is true.
@Before Account instance
The previous examples depend on the existence of an initialized Account instance. We can create an Account in an @Before method and store a reference as a field in the test class:
private Account account;
@Before
public void createAccount() {
account = new Account("an account name");
}
We have been provided with an Account class in this example. Run the tests below to check if they pass.
// usercode/Junit-Assert/test/scratch/AssertTest.java
package scratch;
import static org.junit.Assert.*;
import org.junit.*;
public class AssertTest {
// Creation of account instance
class Account {
int balance;
String name;
Account(String name) {
this.name = name;
}
void deposit(int dollars) {
balance += dollars;
}
public int getBalance() {
return balance;
}
public boolean hasPositiveBalance() {
return balance > 0;
}
}
private Account account;
@Before
public void createAccount() {
account = new Account("an account name");
}
/*Test if the account has positive balance after depositing
$50 in it*/
@Test
public void hasPositiveBalance() {
account.deposit(50);
assertTrue(account.hasPositiveBalance());
}
/*Test if the current account balance is greater than the
initial balance.*/
@Test
public void depositIncreasesBalance() {
int initialBalance = account.getBalance();
account.deposit(100);
assertTrue(account.getBalance() > initialBalance);
}
}The
Ok(2)result shows that the two tests, (hasPositiveBalanceanddepositIncreasesBalance) have passed.
If we replace the > symbol with the < symbol (line 51) in the above code example (AssertTest.java), will the test pass?
A test name such as depositIncreasesBalance is a general statement about the behavior we’re trying to verify. We can write assertions that are also generalizations. For example, we can assert that the balance after depositing is greater than zero. However, the code in our test provides a specific example. As such, we’re better off being explicit about the answer we expect.