claytoncastro97
claytoncastro97

Reputation: 13

Simple JUnit test

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

Answers (1)

Deepak Patankar
Deepak Patankar

Reputation: 3272

The below points will help you solve the issue:

  1. In your 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".
  2. The getNum() method expects a character as an input, but you haven't given a character input while calling it cp.getNum();.

Upvotes: 1

Related Questions