0

I have the next JUnit test, but when I execute it throws "Object is not an instance of declaring method". What could it be?

@Test
    public void testCopiarByteArray() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, InstantiationException, UnsupportedEncodingException{
        String expected = "prueba";
        String mensaje = "prueba";

        DataReader datareader = new DataReader(null, null, 100, "=");

        Method copiarByteArray = datareader.getClass().getDeclaredMethod("copiarByteArray", byte[].class, int.class);
        copiarByteArray.setAccessible(true);

        byte[] copia = (byte[]) copiarByteArray.invoke(mensaje.getBytes(), mensaje.getBytes().length);

        String actual = new String(copia, "UTF-8");

        assertEquals("failure - encription not correctly encript", expected, actual);
    }
0

2 Answers 2

1

The first parameter of .invoke should be an instance of the object to invoke the method on.

So instead of:

    byte[] copia = (byte[]) copiarByteArray.invoke(mensaje.getBytes(), mensaje.getBytes().length);

You need to add datareader as first parameter:

    byte[] copia = (byte[]) copiarByteArray.invoke(datareader, mensaje.getBytes(), mensaje.getBytes().length);
Sign up to request clarification or add additional context in comments.

Comments

0

Check the javadoc on reflection APIs.

You have to pass the object to call your method on as first parameter; and then followed by the "actual" method parameters.

Your code tries to execute a method on mensaje.getBytes(); which of course will not work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.