Assumption: You're using JUnit 4 (seems like a safe assumption given that the code you provided is using the @Test annotation).
You would want something like this for your test class:
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SavingsAccountTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@After
public void cleanUpStreams() {
System.setOut(null);
System.setErr(null);
}
@Test
public void test() {
SavingsAccount savingsAcct = new SavingsAccount(...);
savingsAcct.setNumberWithdrawals(...);
assertEquals("You already have more than 3 withdraw!!\r\n", outContent.toString());
}
}
Other thoughts:
Given the code you posted in your question and code on github some things seem a little strange. It might just be that some of the code is still missing, but I want to point out a few things that stand out:
You posted this code:
assertEquals(true, sa1.setTest(4));
Now according to what I read on github, sa1
is an instance of SavingsAccount
yet I don't see a definition for this setTest(...)
method anywhere. I do see this SavingsAccount extends Account
so I suppose setTest(...)
could be defined in the Account
class. The reason this stands out is that you said you were trying to test SavingsAccount.setNumberWithdrawals(...)
but you're not invoking setNumberWithdrawals
in your test. Again, I suppose that this setTest
method could indirectly invoke setNumberWithdrawals
but that's not clear from everything you posted.
You said in your question:
make sure it can't withdraw more than 3
The logic you have is if (getNumberWithdrawals() > 3)
which means the number of withdrawals will have to be more than 3 to trigger this logic. This seems to violate your requirement.