Created
August 23, 2018 07:13
-
-
Save viveknaskar/f11b7882b465f3587074ce1b4ad6e34d to your computer and use it in GitHub Desktop.
Difference between @mock and @Injectmocks!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sample code on how @Mock and @InjectMocks works. | |
Say we have Game and Player class. | |
class Game { | |
private Player player; | |
public Game(Player player) { | |
this.player = player; | |
} | |
public String attack() { | |
return "Player attack with: " + player.getWeapon(); | |
} | |
} | |
class Player { | |
private String weapon; | |
public Player(String weapon) { | |
this.weapon = weapon; | |
} | |
String getWeapon() { | |
return weapon; | |
} | |
} | |
As you see, Game class need Player to perform an attack. | |
@RunWith(MockitoJUnitRunner.class) | |
class GameTest { | |
@Mock | |
Player player; | |
@InjectMocks | |
Game game; | |
@Test | |
public void attackWithSwordTest() throws Exception { | |
Mockito.when(player.getWeapon()).thenReturn("Sword"); | |
assertEquals("Player attack with: Sword", game.attack()); | |
} | |
} | |
Mockito will mock a Player class and it's behaviour using when and thenReturn method. Lastly, using @InjectMocks Mockito will put that Player into Game. | |
Notice that you don't even have to create a new Game object. Mockito will inject it for you. | |
// you don't have to do this | |
Game game = new Game(player); | |
We will also get same behaviour using @Spy annotation. Even if the attribute name is different. | |
@RunWith(MockitoJUnitRunner.class) | |
public class GameTest { | |
@Mock Player player; | |
@Spy List<String> enemies = new ArrayList<>(); | |
@InjectMocks Game game; | |
@Test public void attackWithSwordTest() throws Exception { | |
Mockito.when(player.getWeapon()).thenReturn("Sword"); | |
enemies.add("Dragon"); | |
enemies.add("Orc"); | |
assertEquals(2, game.numberOfEnemies()); | |
assertEquals("Player attack with: Sword", game.attack()); | |
} | |
} | |
class Game { | |
private Player player; | |
private List<String> opponents; | |
public Game(Player player, List<String> opponents) { | |
this.player = player; | |
this.opponents = opponents; | |
} | |
public int numberOfEnemies() { | |
return opponents.size(); | |
} | |
// ... | |
That's because Mockito will check the Type Signature of Game class, which is Player and List<String>. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment