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

Can Someone Help Me Out?

I don't really understand what I need to do for this problem

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;
    }
    public void refundingReturnsAllAvailableFunds() throws Execption { 
      creditor creditor =new creditor(); 
      creditor.addFunds(0);
      int refund = creditor.refund();

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

1 Answer

Thomas Trabue
Thomas Trabue
11,807 Points

Hey, Kevin,

Let's look at the question carefully. It is asking us to create a new test case so that we do not have two assert statement in our test case called refundingReturnsAllAvailableFunds. The question also states that we should name our new test case refundingResetsAvailableFundsToZero. Thus, we need to get rid of the second assert statement in our old refundingReturnsAllAvailableFunds method by moving it into our new refundingResetsAvailableFundsToZero method. So, let's start by adding a boilerplate for the new test case just below the old one:

@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 refundingReturnsAllAvailableFunds() throws Exception {

}

Great! So we have the template for our new test, but there's nothing inside of it yet. So next, we need to move that second assert statement to the new test case:

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

      int refund = creditor.refund();

      assertEquals(10, refund);
}

@Test
public void refundingReturnsAllAvailableFunds() throws Exception {
      assertEquals(0, creditor.getAvailableFunds());
}

Ok, we're almost done, but we have only completed the Assert step of our test. Remember from the video that we still need to Arrange and Act before we can Assert. Thankfully, we can copy over the Arrange and Act logic from the old test case because our setup for this case is exactly the same:

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

      int refund = creditor.refund();

      assertEquals(10, refund);
}

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

      int refund = creditor.refund();

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

And there we go! This should do exactly what the question asked us to do. Run it and find out!