Reputation: 183
I am trying to learn junit testing in eclipse and i have tried to start off with something simple. I wrote a basic program with 2 methods, an add and subtract method which both take 2 arguments. So I tried testing the add function only, with a test that looked like this
import static org.junit.Assert.*;
import org.junit.Test;
public class addtest {
@Test
public void testAdd() {
class1 calc = new class1();
int expected = 7;
int actual = class1.add(3, 4);
assertEquals("adding 3 and 4 ", expected, actual);
expected = 15;
actual = class1.add(5, 10);
assertEquals("adding 5 and 10 ", expected, actual);
}
}
and it was working as i wanted.
But then when i tried to implement the second test for subtraction, i noticed that it never seemed to test that section. I even tried having a second test method in case my subtraction method was incorrect. This is what i have for my latest test.
import static org.junit.Assert.*;
import org.junit.Test;
public class addtest {
@Test
public void testAdd() {
class1 calc = new class1();
int expected = 7;
int actual = class1.add(3, 4);
assertEquals("adding 3 and 4 ", expected, actual);
expected = 15;
actual = class1.add(5, 10);
assertEquals("adding 5 and 10 ", expected, actual);
}
public void testMinus() {
class1 calc = new class1();
int expected = 0;
int actual = class1.add(12, 4);
assertEquals("subtracting 4 from 12", expected, actual);
}
}
I noticed that the test testMinus never does anything, whether the assert is true of not. Is there something wrong with my implementation? I have tried running the source folder as a junit test already, nothing was fixed.
Thanks a lot for any help
EDIT: I tried commenting out the first test, so that my test function has only testMinus inside it, and now it is testing the second method. Is there a reason the program stops testing after going through the first method?
Upvotes: 1
Views: 920
Reputation: 190
`
@Test
public void testMinus() {
class1 calc = new class1();
int expected = 0;
int actual = calc.add(12, 4); // Issue with current implementation, method should be called with instance of a class.
assertEquals("subtracting 4 from 12", expected, actual);
}
`
All Junit tests should be annotated with with @Test.
Upvotes: 0
Reputation: 645
add @Test
above your minusTest method also. It looks like
@Test
public void testMinus() {
class1 calc = new class1();
int expected = 0;
int actual = class1.add(12, 4);
assertEquals("subtracting 4 from 12", expected, actual);
}
Upvotes: 4