2

In a Saving account for the withdraw variables i need to make sure it can't withdraw more than 3 in a month. I checked the criteria in the setter but I'm not sure how to do the testing in the JUintTesting. As both of them are void method and the testing method returns a boolean i am getting a error of void below is my code.

public void setNumberWithdrawals(int w)
{

    if (getNumberWithdrawals() > 3)
    {
        System.out.println("You already have more than 3 withdraw!!");
    }
    else
    {
        numberWithdrawals = w;
    }
}

For JUNit testing

SavingsAccount sa1;

 @Test
public void testsetNumberWithdrawals()
{
    assertEquals(true, sa1.setTest(4));
}

I am expecting it to return false since i pass 4 but i keep on getting the void and return type expected error, i even tried assigning the value to another int variable for number and tested it but i still got the error.

Pedro
  • 23
  • 3
  • you have to initialize `SavingsAccount` instance before setting value `SavingsAccount sa1 = new SavingsAccount();` Let me know, if that worked – Malakai Nov 28 '16 at 04:05
  • i did create a instance @Before public void setup() { sa1 = new SavingsAccount("Name",1234,50,.02,20); } It still wont let me do it, thank you – Pedro Nov 28 '16 at 04:10
  • Can you show all the code, please? i.e, setTest but maybe [JUnit test for System.out.println()](http://stackoverflow.com/questions/1119385/junit-test-for-system-out-println) can help – Aimee Borda Nov 28 '16 at 04:17
  • i have to agree with @aimee. As a suggestion try to use AssertTrue/AssertFalse like this: `@Test public void test() { sa1.setTest(4); assertFalse(true,sa1.getTest());` – Malakai Nov 28 '16 at 04:21
  • commit your code to github and share a link – Malakai Nov 28 '16 at 04:22
  • https://github.com/timilsina123/java1/projects i dont know if i did it right but here is the link – Pedro Nov 28 '16 at 04:33

1 Answers1

1

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.

D.B.
  • 4,523
  • 2
  • 19
  • 39
  • I think you need to study the following topics: [How to write methods](https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html) - especially the return type of methods; and [data types](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). Hopefully if you study these two topics you will understand why `assertEquals(3,sa)` does not make any sense. What this says is that you expect the `SavingsAccount` object be equal to the integer 3. – D.B. Nov 28 '16 at 05:30
  • Similarly when you write this `assertEquals(true,sa.setNumberWithdrawals());` you are saying that `setNumberWithdrawals()` is going to return a boolean value of true. Unless `setNumberWithdrawals()` has a return type of boolean this will never work. – D.B. Nov 28 '16 at 05:30
  • Thank you i was kinda a able to do what i needed too but as this line "assertEquals(4,sa.getNumberWithdrawals());" is true i get the true along with you already have 3 withdraw. I need it to some how throw the error i tried to pass the set in sa.set but it wont let me do that. public void testSetNumberWithdrawals() { SavingsAccount sa = new SavingsAccount("Name", 1234,50, 0.2,4); sa.setNumberWithdrawals(4); assertEquals(4,sa.getNumberWithdrawals()); } – Pedro Nov 28 '16 at 05:31
  • Thank you for the explanation, it makes more sense to why the set wont work. I will dig into it some more. – Pedro Nov 28 '16 at 05:38
  • The issue with `getNumberWithdrawals` returning 4 and getting "already have 3" at the same time is a logic bug. The method `setNumberWithdrawals` has a parameter `w` but the logic inside the method does not check the parameter when printing the error message: `if (getNumberWithdrawals() > 3)` does not use `w`. When you call `SavingsAccount(...4)` the method `getNumberWithdrawals` returns a value less than or equal to 3 which allows number of withdrawals to be set to 4. Then when you call `setNumberWithdrawals` it triggers the error message since `getNumberWithdrawals` will return 4 – D.B. Nov 28 '16 at 05:48
  • perfect thank you, i understood my whole loop criteria was not checking what i wanted it to do it, thank you – Pedro Nov 28 '16 at 07:35