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

Austin Gladfelter
Austin Gladfelter
1,801 Points

Compiler errors, but nothing in console. What could be the problem here?

I have declared a private variable (field) for calculator just before the Before method, in there a new Calculator object is created for the Test Cases to use. When I run I get a compiler error but there is nothing in the console, also I am not sure what could be wrong with this code.

I have also tried putting the private variable in the setUp() method, and just about every combination of including, not including, putting it in different locations, and even just doing "Calculator calculator = new Calculator(); which I know is wrong anyway. Nothing is working for me.

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 = 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;
    }
}
Austin Gladfelter
Austin Gladfelter
1,801 Points

I have passed the test now, and to the best of my ability the only difference I can see in the code that passed and the code above is spacing and indentation. Perhaps I didn't know that could actually make a difference; can it?

https://www.dropbox.com/s/8zjp2iwoe6bufwf/Screen%20Shot%202018-05-20%20at%2010.29.25%20PM.png?dl=0

Of the dozens of times I'm sure the system is wrong not me, I was always doing some little mistake. Here I'm not sure that is the case. The forums also seem to have some people who are stuck in the same problem I just had. If a moderator or staff could check this out, I would appreciate it.

To the community: Can whitespace and/or indentation cause errors in Java?