Pergunta

Estou recebendo um IllegalArgumentException, mas não consigo descobrir o porquê.

A função que estou tentando acessar:

private static Player checkEvents(Player[] players, GameMaster bananas)

O código problemático:

@Test
public void testCheckEvents() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Game g = new Game();
    GameMaster gm = new GameMaster(4);
    Player[] p = new Player[] {new Player(gm), new Player(gm), new Player(gm), new Player(gm)};

    Method checkEvents = g.getClass().getDeclaredMethod("checkEvents", new Class[] {p.getClass(), GameMaster.class});
    checkEvents.setAccessible(true);

    checkEvents.invoke(p, gm); // fails here
}

A falha:

testCheckEvents(nth.bananas.GameTest)
java.lang.IllegalArgumentException: wrong number of arguments

O que estou fazendo errado?

Foi útil?

Solução

O primeiro argumento para invoke Deve ser um objeto para invocar o método:

checkEvents.invoke(g, p, gm)

Como o seu método é static, você também pode usar null em vez da referência do objeto g.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top