mercredi 10 juin 2020

junit main metohd testing failed

My junit main method testing giving me error although i could not find why. i only implemented when 3 sides meet the criteria for a triangle. expected output and actual outputs does not match.but in code input and output are same.

**Main method:**

    public class Demo {

        public static void main(String[] args) {
            // Reading from System.in
            Scanner reader = new Scanner(System.in);

            System.out.println("Enter side 1: ");
            // Scans the next token of the input as an int.
            int side_1 = reader.nextInt();

            System.out.println("Enter side 2: ");
            // Scans the next token of the input as an int.
            int side_2 = reader.nextInt();

            System.out.println("Enter side 3: ");
            // Scans the next token of the input as an int.
            int side_3 = reader.nextInt();

            if (isTriangle(side_1, side_2, side_3)) {
                System.out.println("This is a triangle.");
            }
            else {
                System.out.println("This is not a triangle.");
            }

            reader.close();

        }

junit test_main_program:

 @Test
    public void test_main_program() {
        ByteArrayInputStream in=new ByteArrayInputStream("5\n6\n9\n".getBytes());
        System.setIn(in);
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
        //invoke
        String[] args= {};
        Demo.main(args);

        String consoleOutput="Enter side 1:\n";
        consoleOutput+="Enter side 2: \n";
        consoleOutput+="Enter side 3:\n";
        consoleOutput+="This is a triangle.\n" ;

        assertEquals(consoleOutput,out.toString());


    }

Aucun commentaire:

Enregistrer un commentaire