Call all static methods including main from another class in setUp() before performing tests in test() function

StackOverflow https://stackoverflow.com/questions/23074480

Question

I am working on a application where lets say class A has main function, class B has getter & setter methods, class C has some calculations.
class A has user validations static methods to make sure user has typed correctly
Object Instance of class B is created in A's main function & userinputs for those validations in 'A' are set using getter & setter methods of B.

I am testing my program using Junit. The problem while testing the calculation function in class C is, the user validations in A arent getting called. I dont want to call them one by one but I want to know how can I call a certain class & all its static methods including main from another class so that all I got to do while testing is call the calculation function in @Test and in @setUp, call the class A's static methods so that my calc function is properly validated before running.

    import code.B;
    import code.C;

    class A
    {
        private static int number1=5;
        private static int number1=50;
        public static final MSG_ERROR="input is null";
        private static String error="";
        private static boolean result=true;
        p s v m(String[] args)
        {
            B b = new B();
            int num1 = b.setValNum1(number1);
            int num2 = b.setValNum2(number2);

            if(!validateInput(num1,num2))
            {
                System.exit(1);
            }
            else
            {
                C.calc(num1,num2);
            }
        }
        /* There are lot of validations just like this */
        public static boolean validateInput(int num1,int num2)
        {
            if(number1 == null || number2 == null)
            {
                error = MSG_ERROR;
                result = false;
            }
            return result;
        }
    }

    class B
    {
        private static int num1;
        private static int num2;

        private getValNum1()
        {
            return num1;
        }

        private getValNum2()
        {
            return num2;
        }

        private setValNum1(int num1)
        {
            this.num1 = num1;
        }
        private setValNum2()
        {
            this.num2 = num2;
        }
    }


    class C
    {
        public static int calc(int a,int b)
        {
            Sopln("add:"+(a+b));

            return (a+b);
        }
    }

    class Ctest
    {
        @Before
        public void setUp() throws Exception {
        }

        @After
        public void tearDown() throws Exception {
        }

        @Test
        public void testCalc() 
        {
            int num1 = 4;
            int num2 = 1;
            int expected = 5;
            /* Here if I provide num1 as null 
                               then it will throw a error 
                               when I have already validated
               in class A for the same.
            */
            int actual = C.calc(num1, num2);
            assertEquals(expected,actual);
        }
    }
Was it helpful?

Solution

You need to refactor your code to make it more testable. That's the primary benefit of TDD - if the code's hard to test, then it's probably hard to maintain, understand, etc.

Regarding your example code, for testing class C, the unit test should just test methods on C, like your example shows. Don't worry about invalid input to C - that's not a responsibility of C so it isn't tested in the unit test for C.

To test the validations, you'll write a unit test for class A and call the validation method with different inputs and check the result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top