Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

sorry Im confused about the whole thing.......I dont even know where Im at Oh **** I need help please

just explain to me whats going on......tell me whats whats

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(0) throws Exception {
        Creditor creditor = new Creditor();

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

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

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

        int refund = creditor.refund(10);

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

public class Creditor {
    private int funds;

    public Creditor(0) {
        funds = 0;
    }

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

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

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

    public int getAvailableFunds(0) {
        return funds;
    }

}
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Treehouse moderator here. I've edited the title of your question. Please refrain from using profanity in the Community. Thank you! :sparkles:

O okay cuhhhh!!!

4 Answers

Very confusing what they are asking, but here is my interpretation.

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?

This tells me we are working with the refundingReturnsAllAvailableFunds() method in the CreditorTest class and that we are creating a new method so we can 'only have one behavior that we are asserting per test'.

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.

Tells me to

  • 'making a new test' -----> (create a new class)
  • 'that does the same' -----> (use the same Arrange, Act and Assert)
  • 'named refundingResetsAvailableFundsToZero' -----> (new method is named refundingResetsAvailableFundsToZero ())

The final code looks like this.

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());
    }

    @Test
    public void refundingResetsAvailableFundsToZero() throws Exception { //<----new method
        Creditor creditor = new Creditor();
        creditor.addFunds(10);

        int refund = creditor.refund();

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

good looking out Philip

Jeff Chiu
Jeff Chiu
465 Points

thanks for the explanation.

thanks man,,i was stuck too,, buh thanks to you,,, am a little beeter,,cheers man

I appreciate the help Philip!