Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Unit Testing in Java How to Test Single Assertions Make For Better Tests

praveen gudimetla
praveen gudimetla
464 Points

crediter test

Challenge Task 1 of 1

Now that we know that we should only have one behavior that we are asserting per test, can you help me break out the refundingReturnsAllAvailableFunds test case into a new one?

How about making a new test that does the same, Arrange, Act and Assert named refundingResetsAvailableFundsToZero that separates out that final assertion in the first test.

i was trying above challenge. I was not very clear.I feel below code is already doing what is being asked above. please advise what else neds to be modified here?

package com.teamtreehouse.vending;

import org.junit.Test;

import static org.junit.Assert.*;

public class CreditorTest {

@Test
public void addingFundsIncrementsAvailableFunds() throws Exception {
    Creditor creditor = new Creditor();

    creditor.addFunds(25);
    creditor.addFunds(25);

    assertEquals(50, creditor.getAvailableFunds());
}

@Test
public void refundingReturnsAllAvailableFunds() throws Exception {
    Creditor creditor = new Creditor();
    creditor.addFunds(10);

    int refund = creditor.refund();

    assertEquals(10, refund);
    assertEquals(0, creditor.getAvailableFunds());
}

}yy

com/teamtreehouse/vending/CreditorTest.java
package com.teamtreehouse.vending;

import org.junit.Test;

import static org.junit.Assert.*;

public class CreditorTest {

    @Test
    public void addingFundsIncrementsAvailableFunds() throws Exception {
        Creditor creditor = new Creditor();

        creditor.addFunds(25);
        creditor.addFunds(25);

        assertEquals(50, creditor.getAvailableFunds());
    }

    @Test
    public void refundingReturnsAllAvailableFunds() throws Exception {
        Creditor creditor = new Creditor();
        creditor.addFunds(10);

        int refund = creditor.refund();

        assertEquals(10, refund);
        assertEquals(0, creditor.getAvailableFunds());
    }
}
com/teamtreehouse/vending/Creditor.java
package com.teamtreehouse.vending;

public class Creditor {
    private int funds;

    public Creditor() {
        funds = 0;
    }

    public void addFunds(int money) {
        funds += money;
    }

    public void deduct(int money) throws NotEnoughFundsException {
        if (money > funds) {
            throw new NotEnoughFundsException();
        }
        funds -= money;
    }

    public int refund() {
        int refund = funds;
        funds = 0;
        return refund;
    }

    public int getAvailableFunds() {
        return funds;
    }

}

3 Answers

All that is asking is to create a new test method with an appropriate name, instantiate the Creditor, add funds, refund and assert that the available funds are 0. You are right telling the code already does that, but you should assert that in a different @test.

Lorton McPherson
Lorton McPherson
777 Points

package com.teamtreehouse.vending;

import org.junit.Test;

import static org.junit.Assert.*;

public class CreditorTest {

@Test
public void addingFundsIncrementsAvailableFunds() throws Exception {
    Creditor creditor = new Creditor();

    creditor.addFunds(25);
    creditor.addFunds(25);

    assertEquals(50, creditor.getAvailableFunds());
}

@Test
public void refundingReturnsAllAvailableFunds() throws Exception {
    Creditor creditor = new Creditor();
    creditor.addFunds(10);

    int refund = creditor.refund();

    assertEquals(10, refund);

}
@Test
public void refundingReturnsAllAvailableFundsToZero() throws Exception {
    Creditor creditor = new Creditor();
    creditor.addFunds(10);

    int refund = creditor.refund();

        assertEquals(0, creditor.getAvailableFunds());
}

}

praveen gudimetla
praveen gudimetla
464 Points

@Test public void refundingResetsAvailableFundsToZero () throws Exception { Creditor creditor = new Creditor();

    creditor.addFunds(0);

    assertEquals(0, creditor.getAvailableFunds());
}

like above right? any improvements to above. it passes all tests