Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialShaun Wetzel
9,085 PointsUse the @Before method to instantiate a new Calculator and use that instance in each method.
Pretty sure my code is correct but it will not pass.
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 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;
}
}
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Shaun Wetzel ! I received a request for assistance and so I took a look at this and it looked correct to me. I ran it through the challenge but it didn't work. On a hunch, I tried entirely deleting the lines that are commented out and it passed!
Short story: delete the commented out lines entirely. I'm not sure why this is happening, but it's causing the challenge to fail. I will bring it up with our education team, though
Hope this helps!
Shaun Wetzel
9,085 PointsShaun Wetzel
9,085 PointsThank you that worked, a silly little bug!