-5

I'm new to java, unit-testing and mockito but I want to test something from my class.

Class I want to test:

abstract class Irc {
    Command received(String data) {
        // parsing

        receivedCommand(prefix, command, parameters, trailing, tags);

        return new Command(prefix, command, parameters, trailing, tags);
    }

    private void receivedCommand(String prefix, String command, String[] parameters, String trailing, Map<String, String> tags) {
        String nick = getNickFromPrefix(prefix);

        parsed(prefix, command, parameters, trailing);

        if (command.equals("MODE")) {
            if (parameters.length == 3) {
                String chan = parameters[0];
                String mode = parameters[1];
                String name = parameters[2];

                if (mode.length() == 2) {
                    String modeChar = mode.substring(1, 2);
                    if (mode.startsWith("+")) {
                        onModeChange(chan, name, true, modeChar, prefix);
                    } else if (mode.startsWith("-")) {
                        onModeChange(chan, name, false, modeChar, prefix);
                    }
                }
            }

            return;
        }
    }

    void onModeChange(String channel, String nick, boolean modeAdded, String mode, String prefix) {
    }
}

EDIT: I want to make sure that onModeChange is called after received was called.

What I have so far:

@Test
public void modeChangeReceivedRightSyntax() {
    try {
        irc = new Irc("Test") {
            @Override
            public void debug(String line) {
                System.err.println(line);
            }

            @Override
            void onModeChange(String channel, String nick, boolean modeAdded, String mode, String prefix) {
                System.out.println("Mode Change: " + channel + " " + nick + " " + mode + " " + prefix + " " + modeAdded);
            }
        };

        ircMock = spy(irc);

        when(
                ircMock.received(":jtv MODE #channel +o user")
        ).thenReturn(
                new Command("jtv", "MODE", new String[]{"#channel", "+o", "user"}, "", null)
        );

        verify(ircMock).onModeChange("#channel", "user", true, "o", "jtv");
    } catch (Exception ex) {
        fail("Exception: " + ex.toString());
    }
}

The when is working but the verify fails with Actually, there were zero interactions with this mock.

iPaat
  • 792
  • 4
  • 16
  • 1
    If you want to test abstract class maybe you should look to this post https://stackoverflow.com/questions/1087339/using-mockito-to-test-abstract-classes – Piotr0123456 Jun 28 '16 at 10:25
  • I had some trouble with the formating, I updated my post. – iPaat Jun 28 '16 at 10:43

1 Answers1

2

Your code is not calling any business methods. It only mocks how the received method should behave but that does not actually call the method. Since your Irc class is abstract, your approach to use spy is good but you should not mock the received method but tell the spy to delegate to the real method. Then you can verify that onModeChange is called.

Irc irc = spy(Irc.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));

// Business method
irc.received(":jtv MODE #channel +o user");

// Asserts
verify(irc).onModeChange("#channel", "user", true, "o", "jtv");