简体   繁体   中英

Mockito verify constructor calls method

I'm trying to test when the Game class is instantiated, that the start method is called. However I get the following error:

Wanted but not invoked:
game.start();
Actually, there were zero interactions with this mock.

I have the following class called Game

public class Game {
    private Hand player_hand;
    private Hand dealer_hand;
    public static Boolean isInPlay;

    public Game() {
        player_hand = new Hand();
        dealer_hand = new Hand();
        start();
    }

    public void start() {
        isInPlay = true;
        player_hand.hit();
        player_hand.hit();
        System.out.println("Player hand: ");
        player_hand.printHands();
        instantWinLose();
        dealer_hand.hit();
        dealer_hand.hit();
    }
}

I have a test class called GameTest

@RunWith(MockitoJUnitRunner.StrictStubs.class)

public class GameTest {

@InjectMocks
Game game;

@Mock
Hand hand;

    @Test
    public void testGameConstruction() {
        Game mockedGame = mock(Game.class);
        verify(mockedGame, times(1)).start();
    }
}

I'm new to Mockito. I've tried following examples in Difference between @Mock and @InjectMocks but I still get the same error

Actually, You need to call the start method then only your mocking will be validated with times.

something like :: mockedGames.start()

Then only it call will be verified

When you call Mockito.mock(SomeType.class) , Mockito will dynamically create a subclass for that type but for instantiation it uses certain techniques to avoid calling the super constructor ( read more ).

Try this:

public class Foobar {

    public Foobar () {
        throw new RuntimeException();
    }

}

// Somewhere else ...
Mockito.mock(Foobar.class); // No exception will be thrown because constructor is never called

That makes sense, when you think about it: A fresh mock object should not be doing anything unless it is absolutely required (stubbing). Calling any real logic could have undesired side effects.

And that is why you never mock the class under test itself!

When you mock the class under test itself, your tests have absolutely no meaning at all!

Only mock dependencies.

Your Game class has no dependencies, so you won't be needing any mocks:

    @Test
    public void testGameConstruction() {
        Game game = new Game();
        assertTrue(game.isInGame());
    }

If Game had dependencies, for instance Hand , you could add constructor parameters:

public Game (Hand hand1, Hand hand2) { .... }

and then:

    @Test
    public void testGameConstruction() {

        Hand handMock1 = Mockito.mock(Hand.class);
        Hand handMock2 = Mockito.mock(Hand.class);
        Game game = new Game(handMock1, handMock2);

        verify(handMock1, times(1)).hit();
        verify(handMock2, times(1)).hit();

    }

How I understand, by mocking Game class you also mocking constructor. That's mean that no code from actual class constructer was run. Try to create an instance of Game class without mocking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM