'How do I run a JUnit test on an implemented class in Eclipse?
package evenodd;
public class HW3 { public static void main(String[] args) {
MyInteger int1 = new MyInteger(5);
MyInteger int2 = new MyInteger(38);
MyInteger int3 = new MyInteger(10);
MyInteger int4 = new MyInteger(5);
System.out.printf("%d is even? %s%n", int1.getValue(), int1.isEven());
System.out.printf("%d is even? %s%n", int2.getValue(), int2.isEven());
System.out.printf("%d is even? %s%n", int3.getValue(), int3.isEven());
System.out.printf("93 is odd? %s%n", MyInteger.isOdd(93));
System.out.printf("%d equals %d? %s%n", int1.getValue(), int4.getValue(), int1.equals(int4));
System.out.println("The value of int1 is equal to 5? " + int1.equals(5));
}
}
class MyInteger {
private int mValue;
public MyInteger(int value) {
mValue = value;
}
public int getValue() {
return mValue;
}
public boolean isEven() {
return (mValue % 2) == 0;
}
public boolean isOdd() {
return (mValue % 2) == 1;
}
public static boolean isEven(int myInt) {
return (myInt % 2) == 0;
}
public static boolean isOdd(int myInt) {
return (myInt % 2) == 1;
}
public static boolean isEven(MyInteger myInt) {
return myInt.isEven();
}
public static boolean isOdd(MyInteger myInt) {
return myInt.isOdd();
}
public boolean equals(int testInt) {
if (testInt == mValue)
return true;
return false;
}
public boolean equals(MyInteger myInt) {
if (myInt.mValue == this.mValue)
return true;
return false;
}
}
I am trying to test the MyInteger class with JUnit in Eclipse but it keeps saying, "Class under test does not exist in current project." Does anyone know how to fix this? Also, I created a second source file in the java project with a package of the same name (evenodd) to put the JUnit test into because that's what this tutorial I was following said to do.
Solution 1:[1]
If you need to perform a unit test on the MyInteger class, you can simply add a Test Case to your project and create your @Tests:
import static org.junit.Assert.*;
import org.junit.Test;
public class MyIntegerTest {
@Test
public void testIsOdd() {
MyInteger integer = new MyInteger(11);
assertTrue(integer.isOdd());
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
