Search code examples
javajunitcompiler-errorsvoid

TriangleTest using Junit: void type is not allowed


I have this code and I tried to create unit tests using Junit. When I try to test the MAIN as following;

@Test
public void testMain(){
    assertEquals(determineType(0,1,1),"ABCDEF");
}

It keeps telling me that void type is not allowed here. What I have to do?


package triangle;

public class Triangle {


    public static void determineType(int a, int b, int c) {
        if (a >= (b + c) || c >= (b + a) || b >= (a + c)) {
            System.out.println("Not a Triangle");
        } else if (a == b && b == c) {
            System.out.println("Equilateral Triangle");
        } else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
            System.out.println("Right Triangle");
        } else if (a != b && b != c && c != a) {
            System.out.println("Scalene Triangle");
        } else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) {
            System.out.println("Isosceles Triangle");
        }
    }

    public static void main(String[] args) {
        determineType(1, 1, 9);
    }
}

Solution

  • Try this code, I have fixed the errors

    TestCode

        @Test
        public void testMain(){
            assertEquals(determineType(0,1,1),"Not a Triangle");
        }
    

    SourceCode

    package triangle;
    
    public class Triangle {
    
    
        public static String determineType(int a, int b, int c) {
    
            if (a >= (b + c) || c >= (b + a) || b >= (a + c)) {
                return "Not a Triangle";
            } else if (a == b && b == c) {
                return "Equilateral Triangle";
            } else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
                return "Right Triangle";
            } else if (a != b && b != c && c != a) {
                return "Scalene Triangle";
            } else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) {
                return "Isosceles Triangle";
            }
            // other case
            return null;
        }
    
        public static void main(String[] args) {
            System.out.println(determineType(1, 1, 9));
        }
    }