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.

praveen gudimetla
464 PointsCalculatorTest JUnit issues
Hi,
I tried my test class as below. I get error Bummer! please only intitialze Calculator once in Before Gags. how to fix this error. please advuse
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 {
//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);
}
}
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 {
//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);
}
}
package com.example;
public class Calculator {
public int addNumbers(int... numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
}
}
5 Answers

AJ Longstreet
Treehouse Project ReviewerYou actually did it correctly but tests can be finicky.
Delete the commented code (//Calculator calculator = new Calculator();) and you will be on to the next one!!

praveen gudimetla
464 Pointspackage com.example;
import org.junit.Before; import org.junit.Test;
import static org.junit.Assert.*; Calculator calculator; public class CalculatorTest {
@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);
}
}
i removed commented lines as above getting compilation errors like
./com/example/CalculatorTest.java:7: error: class, interface, or enum expected Calculator calculator; ^ ./com/example/CalculatorTest.java:13: error: cannot find symbol calculator=new Calculator(); ^ symbol: variable calculator location: class CalculatorTest ./com/example/CalculatorTest.java:21: error: cannot find symbol int answer = calculator.addNumbers(1 ,2, 3); ^ symbol: variable calculator location: class CalculatorTest ./com/example/CalculatorTest.java:30: error: cannot find symbol int answer = calculator.addNumbers(1); ^ symbol: variable calculator location: class CalculatorTest 4 errors
please advise

praveen gudimetla
464 Pointspackage 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 {
int answer = calculator.addNumbers(1 ,2, 3);
assertEquals(6, answer);
}
@Test
public void addingSingleNumberTotalsAppropriately() throws Exception {
int answer = calculator.addNumbers(1);
assertEquals(1, answer);
}
}
above gives below error Bummer! There is a compiler error. Please click on preview to view your syntax errors!

AJ Longstreet
Treehouse Project ReviewerYou just need to declare it in the global scope and instantiate it in the @Before. Then delete the other 2 declarations and you're golden.
This code passes.
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);
}
}

praveen gudimetla
464 Pointsit works, thank you