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 Using Before to not repeat yourself

Gary Mclean
Gary Mclean
5,138 Points

use @Before to instantiate a object in JUnit

hi,

the question seems to be easy enough but cant seem to see what i am doing wrong here, it asks me to instantiate a new object and i do that however, it doesnt seem to work.

Any help would be awesome!

P.s the question is I've started the shell for the @Before fixture for these tests. See how the Arrange part is duplicated in both tests, can you help me fix that please?

Use the @Before method to instantiate a new Calculator and use that instance in each method.

com/example/CalculatorTest.java
package com.example;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class CalculatorTest {

private Calculator calculator;

    @Before
    public void setUp() throws Exception {
      Calculator calculator = new Calculator();

    }

    @Test
    public void addingMultipleNumbersProducesResult() throws Exception {
        int answer = calculator.addNumbers(1 ,2, 3);

        assertEquals(6, answer);
    }

    @Test
    public void addingSingleNumberTotalsAppropriately() throws Exception {
        int answer = calculator.addNumbers(1);

        assertEquals(1, answer);
    }
}
com/example/Calculator.java
package com.example;

public class Calculator {

    public int addNumbers(int... numbers) {
        int total = 0;
        for (int number : numbers) {
            total += number;
        }
        return total;
    }
}

4 Answers

Miguel Canas
seal-mask
.a{fill-rule:evenodd;}techdegree
Miguel Canas
Python Development Techdegree Student 10,807 Points

Hi Gary,

You're super close. Here's a hint: consider the scope of the calculator variable in this method:

    @Before
    public void setUp() throws Exception {
      Calculator calculator = new Calculator();
    }

Based on what you've written here, will calculator be accessible outside of the setUp() method? What can you do to make calculator available to other methods in this class?

Hope that helps!

~ Miguel

Gary Mclean
Gary Mclean
5,138 Points

Hi, thanks for the tip i made a private field however, it makes the tests fail. any idea why?

" Bummer! 2 out of 2 tests failed. See preview for more details."

"2 FAILURES

addingSingleNumberTotalsAppropriately: null addingMultipleNumbersProducesResult: null

"

Miguel Canas
seal-mask
.a{fill-rule:evenodd;}techdegree
Miguel Canas
Python Development Techdegree Student 10,807 Points

OK

    @Before
    public void setUp() throws Exception {
      Calculator calculator = new Calculator();
    }

On this line Calculator calculator = new Calculator() you're declaring the variable in the method's local scope by preceding it with the class name, and not assigning it to the private field.

Gary Mclean
Gary Mclean
5,138 Points

Ah, ok, that fixed it. Thanks

I still don't get it, it is still giving me an error

Austin Gladfelter
Austin Gladfelter
1,801 Points

I'm getting the same thing. Did you ever manage to resolve this?

Hi im having the same issue as you gary, my tests both fail. How did you end up resolving that?

Nevermind I figured it out. I had Calculator calculator = new Calculator();

When it needed to just be calculator = new Calculator();

David Kaiser
David Kaiser
2,079 Points

So my code is below.

It throws the error "Bummer! Please only initialize the Calculator object once in the setUp method that is annotated with @Before."

package com.example;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class CalculatorTest {

  Calculator calculator; 

    @Before
    public void setUp() throws Exception {
      calculator = new Calculator();
    }

    @Test
    public void addingMultipleNumbersProducesResult() throws Exception {
        //Calculator calculator = new Calculator();

        int answer = calculator.addNumbers(1 ,2, 3);

        assertEquals(6, answer);
    }

    @Test
    public void addingSingleNumberTotalsAppropriately() throws Exception {
        //Calculator calculator = new Calculator();

        int answer = calculator.addNumbers(1);

        assertEquals(1, answer);
    }
}

so I only declared the variable once, inside the Setup method, as in the code below, and got this error: There is a compiler error. Please click on preview to view your syntax errors!

A check of the preview shows this:

./com/example/CalculatorTest.java:19: error: cannot find symbol
        int answer = calculator.addNumbers(1 ,2, 3);
                     ^
  symbol:   variable calculator
  location: class CalculatorTest
./com/example/CalculatorTest.java:28: error: cannot find symbol
        int answer = calculator.addNumbers(1);
                     ^
  symbol:   variable calculator
  location: class CalculatorTest
2 errors
package com.example;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class CalculatorTest {

    @Before
    public void setUp() throws Exception {
      Calculator calculator = new Calculator();
    }

    @Test
    public void addingMultipleNumbersProducesResult() throws Exception {
        //Calculator calculator = new Calculator();

        int answer = calculator.addNumbers(1 ,2, 3);

        assertEquals(6, answer);
    }

    @Test
    public void addingSingleNumberTotalsAppropriately() throws Exception {
        //Calculator calculator = new Calculator();

        int answer = calculator.addNumbers(1);

        assertEquals(1, answer);
    }
}