1 Attachment(s)
JUnit initialization error
Hi all, I'm trying a simple JUnit test with Eclipse but I'm getting an initialization error (attached screenshot). Just looking for some help as to how to clear this error. Here are my classes:
IntegerCalculator
Code :
public class IntegerCalculator {
private int amount = 0;
public IntegerCalculator() {}
public IntegerCalculator(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
public void add(int val) {
amount += val; // This code is correct.
}
public void subtract(int val) {
amount = val; // This code is wrong.
}
public void divide(int val) {
amount /= val; // This code could generate and error.
}
}
IntegerCalculatorTest
Code :
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class IntegerCalculatorTest {
private IntegerCalculator n;
@Before
public void setUp() throws Exception {
n = new IntegerCalculator(7);
}
@Test
public void testAdd() {
n.add(5);
assertEquals("(7 + 5)", 12, n.getAmount());
}
@Test
public void testSubtract() {
n.subtract(3);
assertEquals("(7 - 3)", 4, n.getAmount());
}
@Test
public void testDivide() {
n.divide(0);
fail();
}
}
Re: JUnit initialization error
hello, i just imported your code in eclipse and i could compile and run your JUnit code succesful. what i created in eclipse is the following: i create a new source folder named test and then i right-clicked this folder and then choosed new -> JUnit Test Case and enter the name IntegerCalculatorTest and then i just copy/pasted your code and finished. when i created the JUnit Test Case i was asked if i wanted to create a JUnit 3 or JUnit 4 and i choosed JUnit 4. try the same and report the result.
http://javapup.i-networx.de/JUnitTest.jpg
Re: JUnit initialization error