vendredi 6 novembre 2020

How to pass null on private functions at Junit test [code.getDeclaredMethod] [method.invoke(serviceTest, "aaa",new Object[]{null}) happens an error]

I want to test the behaviour of method when an argument is null . I tried like below but arugment type miss match error happens . What can I do to test it? I installed Jmockit. but I don't know how to use it.

java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

ACTUAL CODE

    @Test
    public void testLogic() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
        Service serviceTest = new ServiceTest();

        int expected = false;
        Method method = Service.class.getDeclaredMethod("testMethod", String.class, String.class)  ;
        method.setAccessible(true);
        boolean actual = (boolean)method.invoke(serviceTest, "aaa",new Object[]{null});
        assertEquals(false, actual);

    }


TEST CODE

    private boolean testMethod(String test,String testNull) throws IOException
    {
        if(testNull== null)
        {

            return false;
        }

            return true;

    }

Aucun commentaire:

Enregistrer un commentaire