Reputation: 13
I'm new at programming and I'm starting to learn JUnit in class. My task is to test if the user typed a number correctly instead of a letter or something else.
My teacher gave me the main class and I just need to code the JUnit.
Here is the main code:
public class CheckPoint {
public static String readString(String msg) throws java.io.IOException {
DataInputStream din = new DataInputStream(System.in);
System.out.print(msg);
return din.readLine();
}
public static int getNum(char c) throws java.io.IOException {
int n;
int indicator = 1;
for (int i = 0; i < 1; i++) {
String s = readString("Type a number: ");
try {
n = Integer.parseInt(s);
System.out.println("Invalid number: " + n);
}
catch (NumberFormatException nfe) {
System.out.println("Wrong.");
indicator = 2;
}
}
System.out.println("End.");
return indicator;
}
}
and here is what I'm trying in the JUnit class:
class numericTest {
@Test
public void numericTest() {
int n = 50;
int expectedIndicator = 1;
CheckPoint cp = new CheckPoint(n); //"The constructor CheckPoint(int) is undefined"
int originalIndicator = cp.getNum();
assertEquals(expectedIndicator, originalIndicator); //"The method getNum(char) in the type CheckPoint is not applicable in the arguments ()"
}
}
I commented what errors I'm getting in their respective lines.
What should I do?
Upvotes: 1
Views: 155
Reputation: 3272
The below points will help you solve the issue:
CheckPoint
class there is no constructor defined with input a number, that's why the compiler is giving you the error that "The constructor CheckPoint(int) is undefined".cp.getNum();
.Upvotes: 1