Question

Je reçois un IllegalArgumentException, mais je ne peux pas comprendre pourquoi.

La fonction que je suis en train d'accéder à:

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

Le code problématique:

@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
}

L'échec:

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

Qu'est-ce que je fais mal?

Était-ce utile?

La solution

Le premier argument de invoke doit être un objet sur lequel appeler la méthode:

checkEvents.invoke(g, p, gm)

Étant donné que votre méthode est static, vous pouvez également utiliser null au lieu de la référence d'objet g.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top