How to use returnsFirstArg method of org.mockito.AdditionalAnswers class

Best Mockito code snippet using org.mockito.AdditionalAnswers.returnsFirstArg

Source:GameServiceTest.java Github

copy

Full Screen

...29 @InjectMocks30 private GameServiceImpl gameService;31 @Test32 public void create_valid_initial_game_id_is_UUID() {33 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());34 Game game = gameService.createNewGame();35 Pattern pattern = Pattern.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$");36 assertTrue(pattern.asPredicate().test(game.getGameId().toString()));37 }38 @Test39 public void create_valid_initial_game_firstPlayerId_is_UUID() {40 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());41 Game game = gameService.createNewGame();42 Pattern pattern = Pattern.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$");43 assertTrue(pattern.asPredicate().test(game.getPlayerOneId().toString()));44 }45 @Test46 public void creates_valid_initial_game_secondPlayerId_is_null() {47 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());48 Game game = gameService.createNewGame();49 assertNull(game.getPlayerTwoId());50 }51 @Test52 public void creates_valid_initial_game_moves_is_empty() {53 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());54 Game game = gameService.createNewGame();55 assertTrue(game.getMovesHistory().isEmpty());56 }57 @Test58 public void addPlayer_adds_palyer_to_valid_game() throws TicTacToeException {59 UUID gameId = UUID.randomUUID();60 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(new Game(gameId, UUID.randomUUID())));61 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());62 Game game = gameService.addPlayerToGame(gameId);63 Pattern pattern = Pattern.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$");64 assertNotNull(game.getPlayerTwoId());65 assertTrue(pattern.asPredicate().test(game.getPlayerTwoId().toString()));66 }67 @Test68 public void addPlayer_throws_exception_player_already_added() throws TicTacToeException {69 UUID gameId = UUID.randomUUID();70 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(new Game(gameId, UUID.randomUUID())));71 gameService.addPlayerToGame(gameId);72 Exception exception = assertThrows(TicTacToeException.class, () -> {73 gameService.addPlayerToGame(gameId);74 });75 assertTrue(exception.getMessage().contains("Given gameId: " + gameId + " has already a player2 defined:"));76 }77 @Test78 public void addPlayer_throws_exception_no_game_found_given_id() {79 UUID gameId = UUID.randomUUID();80 Mockito.when(gameDao.getById(gameId)).thenReturn(Optional.empty());81 Exception exception = assertThrows(TicTacToeException.class, () -> {82 gameService.addPlayerToGame(gameId);83 });84 assertTrue(exception.getMessage().contains("No game associated with given gameId: " + gameId));85 }86 @Test87 public void isNextMoveValid_firstMove_game_exists_valid_positions() throws TicTacToeException {88 UUID gameId = UUID.randomUUID();89 UUID p1Id = UUID.randomUUID();90 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(new Game(gameId, p1Id)));91 GameMove gameMove = new GameMove(p1Id, 0, 0);92 assertTrue(gameService.isNextMoveValid(gameId, gameMove));93 }94 @Test95 public void isNextMoveValid_firstMove_game_doesnt_exist() {96 UUID gameId = UUID.randomUUID();97 UUID p1Id = UUID.randomUUID();98 Mockito.when(gameDao.getById(any())).thenReturn(Optional.empty());99 GameMove gameMove = new GameMove(p1Id, 0, 0);100 Exception exception = assertThrows(TicTacToeException.class, () -> {101 gameService.isNextMoveValid(gameId, gameMove);102 });103 assertTrue(exception.getMessage().contains("No game associated with given gameId: " + gameId));104 }105 @Test106 public void isNextMoveValid_wrong_player_firstMove() throws TicTacToeException {107 UUID gameId = UUID.randomUUID();108 UUID p1Id = UUID.randomUUID();109 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(new Game(gameId, p1Id)));110 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());111 Game game = gameService.addPlayerToGame(gameId);112 GameMove gameMove = new GameMove(game.getPlayerTwoId(), 0, 0);113 assertFalse(gameService.isNextMoveValid(gameId, gameMove));114 }115 @Test116 public void isNextMoveValid_invalid_x_position_too_high() throws TicTacToeException {117 GameMove gameMove = new GameMove(UUID.randomUUID(), 3, 0);118 assertFalse(gameService.isNextMoveValid(UUID.randomUUID(), gameMove));119 }120 @Test121 public void isNextMoveValid_invalid_x_position_too_low() throws TicTacToeException {122 GameMove gameMove = new GameMove(UUID.randomUUID(), -1, 0);123 assertFalse(gameService.isNextMoveValid(UUID.randomUUID(), gameMove));124 }125 @Test126 public void isNextMoveValid_invalid_y_position_too_high() throws TicTacToeException {127 GameMove gameMove = new GameMove(UUID.randomUUID(), 0, 3);128 assertFalse(gameService.isNextMoveValid(UUID.randomUUID(), gameMove));129 }130 @Test131 public void isNextMoveValid_invalid_y_position_too_low() throws TicTacToeException {132 GameMove gameMove = new GameMove(UUID.randomUUID(), 0, -1);133 assertFalse(gameService.isNextMoveValid(UUID.randomUUID(), gameMove));134 }135 @Test136 public void play_game_move_added() throws TicTacToeException {137 UUID gameId = UUID.randomUUID();138 UUID p1Id = UUID.randomUUID();139 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(new Game(gameId, p1Id)));140 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());141 gameService.addPlayerToGame(gameId);142 GameMove gameMove = new GameMove(p1Id, 0, 0);143 List<GameMove> moves = gameService.play(gameId, gameMove).getMovesHistory();144 assertEquals(moves.get(moves.size() - 1), gameMove);145 }146 @Test147 public void play_game_not_existant() {148 UUID gameId = UUID.randomUUID();149 UUID p1Id = UUID.randomUUID();150 Mockito.when(gameDao.getById(any())).thenReturn(Optional.empty());151 GameMove gameMove = new GameMove(p1Id, 0, 0);152 Exception exception = assertThrows(TicTacToeException.class, () -> {153 gameService.play(gameId, gameMove).getMovesHistory();154 });155 assertTrue(exception.getMessage().contains("No game associated with given gameId: " + gameId));156 }157 @Test158 public void play_game_no_secondPlayerYet() {159 UUID gameId = UUID.randomUUID();160 UUID p1Id = UUID.randomUUID();161 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(new Game(gameId, p1Id)));162 GameMove gameMove = new GameMove(p1Id, 0, 0);163 Exception exception = assertThrows(TicTacToeException.class, () -> {164 gameService.play(gameId, gameMove);165 });166 assertTrue(exception.getMessage().contains("Still awaiting second player."));167 }168 @Test169 public void isNextMoveValid_second_move() throws TicTacToeException {170 UUID gameId = UUID.randomUUID();171 UUID p1Id = UUID.randomUUID();172 Game game = new Game(gameId, p1Id);173 game.playMove(new GameMove(p1Id, 0, 0));174 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));175 GameMove gameMove = new GameMove(game.getPlayerTwoId(), 1, 0);176 assertTrue(gameService.isNextMoveValid(gameId, gameMove));177 }178 @Test179 public void isNextMoveValid_second_move_wrong_player() throws TicTacToeException {180 UUID gameId = UUID.randomUUID();181 UUID p1Id = UUID.randomUUID();182 Game game = new Game(gameId, p1Id);183 game.playMove(new GameMove(p1Id, 0, 0));184 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));185 GameMove gameMove = new GameMove(p1Id, 1, 0);186 assertFalse(gameService.isNextMoveValid(gameId, gameMove));187 }188 @Test189 public void isNextMoveValid_second_move_position_already_taken() throws TicTacToeException {190 UUID gameId = UUID.randomUUID();191 UUID p1Id = UUID.randomUUID();192 Game game = new Game(gameId, p1Id);193 game.playMove(new GameMove(p1Id, 0, 0));194 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));195 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());196 game = gameService.addPlayerToGame(gameId);197 GameMove gameMove = new GameMove(game.getPlayerTwoId(), 0, 0);198 assertFalse(gameService.isNextMoveValid(gameId, gameMove));199 }200 @Test201 public void getStatus_awaiting_second_player() {202 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());203 Game game = gameService.createNewGame();204 assertEquals(GameStatus.awaitingSecondPlayer, game.getStatus());205 }206 @Test207 public void getStatus_is_running_after_player2_joined() throws TicTacToeException {208 UUID gameId = UUID.randomUUID();209 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(new Game(gameId, UUID.randomUUID())));210 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());211 Game game = gameService.addPlayerToGame(gameId);212 assertEquals(GameStatus.running, game.getStatus());213 }214 @Test215 public void getStatus_is_running_after_move_played() throws TicTacToeException {216 UUID gameId = UUID.randomUUID();217 UUID p1Id = UUID.randomUUID();218 Game game = new Game(gameId, p1Id);219 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));220 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());221 game = gameService.addPlayerToGame(gameId);222 GameMove gameMove = new GameMove(p1Id, 0, 0);223 game = gameService.play(gameId, gameMove);224 assertEquals(GameStatus.running, game.getStatus());225 }226 @Test227 public void getStatus_is_won_by_player1_horizontal() throws TicTacToeException {228 UUID gameId = UUID.randomUUID();229 UUID p1Id = UUID.randomUUID();230 Game game = new Game(gameId, p1Id);231 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));232 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());233 game = gameService.addPlayerToGame(gameId);234 var p2Id = game.getPlayerTwoId();235 GameMove p1m1 = new GameMove(p1Id, 0, 0);236 GameMove p1m2 = new GameMove(p1Id, 1, 0);237 GameMove p1m3 = new GameMove(p1Id, 2, 0);238 GameMove p2m1 = new GameMove(p2Id, 0, 1);239 GameMove p2m2 = new GameMove(p2Id, 1, 1);240 game = gameService.play(gameId, p1m1);241 game = gameService.play(gameId, p2m1);242 game = gameService.play(gameId, p1m2);243 game = gameService.play(gameId, p2m2);244 game = gameService.play(gameId, p1m3);245 assertEquals(GameStatus.p1Won, game.getStatus());246 }247 @Test248 public void getStatus_is_won_by_player1_vertical() throws TicTacToeException {249 UUID gameId = UUID.randomUUID();250 UUID p1Id = UUID.randomUUID();251 Game game = new Game(gameId, p1Id);252 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));253 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());254 game = gameService.addPlayerToGame(gameId);255 var p2Id = game.getPlayerTwoId();256 GameMove p1m1 = new GameMove(p1Id, 1, 0);257 GameMove p1m2 = new GameMove(p1Id, 1, 1);258 GameMove p1m3 = new GameMove(p1Id, 1, 2);259 GameMove p2m1 = new GameMove(p2Id, 0, 1);260 GameMove p2m2 = new GameMove(p2Id, 0, 2);261 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));262 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());263 game = gameService.play(gameId, p1m1);264 game = gameService.play(gameId, p2m1);265 game = gameService.play(gameId, p1m2);266 game = gameService.play(gameId, p2m2);267 game = gameService.play(gameId, p1m3);268 assertEquals(GameStatus.p1Won, game.getStatus());269 }270 @Test271 public void gettStatus_is_won_by_player1_oblique_positive_slope() throws TicTacToeException {272 UUID gameId = UUID.randomUUID();273 UUID p1Id = UUID.randomUUID();274 Game game = new Game(gameId, p1Id);275 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));276 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());277 game = gameService.addPlayerToGame(gameId);278 var p2Id = game.getPlayerTwoId();279 GameMove p1m1 = new GameMove(p1Id, 0, 0);280 GameMove p1m2 = new GameMove(p1Id, 1, 1);281 GameMove p1m3 = new GameMove(p1Id, 2, 2);282 GameMove p2m1 = new GameMove(p2Id, 0, 1);283 GameMove p2m2 = new GameMove(p2Id, 0, 2);284 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));285 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());286 game = gameService.play(gameId, p1m1);287 game = gameService.play(gameId, p2m1);288 game = gameService.play(gameId, p1m2);289 game = gameService.play(gameId, p2m2);290 game = gameService.play(gameId, p1m3);291 assertEquals(GameStatus.p1Won, game.getStatus());292 }293 @Test294 public void gettStatus_is_won_by_player1_oblique_negative_slope() throws TicTacToeException {295 UUID gameId = UUID.randomUUID();296 UUID p1Id = UUID.randomUUID();297 Game game = new Game(gameId, p1Id);298 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));299 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());300 game = gameService.addPlayerToGame(gameId);301 var p2Id = game.getPlayerTwoId();302 GameMove p1m1 = new GameMove(p1Id, 0, 2);303 GameMove p1m2 = new GameMove(p1Id, 1, 1);304 GameMove p1m3 = new GameMove(p1Id, 2, 0);305 GameMove p2m1 = new GameMove(p2Id, 0, 1);306 GameMove p2m2 = new GameMove(p2Id, 0, 0);307 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));308 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());309 game = gameService.play(gameId, p1m1);310 game = gameService.play(gameId, p2m1);311 game = gameService.play(gameId, p1m2);312 game = gameService.play(gameId, p2m2);313 game = gameService.play(gameId, p1m3);314 assertEquals(GameStatus.p1Won, game.getStatus());315 }316 @Test317 public void getStatus_is_draw() throws TicTacToeException {318 UUID gameId = UUID.randomUUID();319 UUID p1Id = UUID.randomUUID();320 Game game = new Game(gameId, p1Id);321 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));322 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());323 game = gameService.addPlayerToGame(gameId);324 var p2Id = game.getPlayerTwoId();325 GameMove p1m1 = new GameMove(p1Id, 0, 0);326 GameMove p2m1 = new GameMove(p2Id, 1, 0);327 GameMove p1m2 = new GameMove(p1Id, 2, 0);328 GameMove p2m2 = new GameMove(p2Id, 0, 1);329 GameMove p1m3 = new GameMove(p1Id, 1, 1);330 GameMove p2m3 = new GameMove(p2Id, 0, 2);331 GameMove p1m4 = new GameMove(p1Id, 2, 1);332 GameMove p2m4 = new GameMove(p2Id, 2, 2);333 GameMove p1m5 = new GameMove(p1Id, 1, 2);334 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));335 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());336 game = gameService.play(gameId, p1m1);337 game = gameService.play(gameId, p2m1);338 game = gameService.play(gameId, p1m2);339 game = gameService.play(gameId, p2m2);340 game = gameService.play(gameId, p1m3);341 game = gameService.play(gameId, p2m3);342 game = gameService.play(gameId, p1m4);343 game = gameService.play(gameId, p2m4);344 game = gameService.play(gameId, p1m5);345 assertEquals(GameStatus.draw, game.getStatus());346 }347 @Test348 public void getGamestate_next_player_is_first_when_game_ready() throws TicTacToeException {349 UUID gameId = UUID.randomUUID();350 UUID p1 = UUID.randomUUID();351 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(new Game(gameId, p1)));352 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());353 gameService.addPlayerToGame(gameId);354 assertEquals(p1, gameService.getGameState(gameId).getNextPlayer());355 }356 @Test357 public void getGamestate_next_player_is_second_after_firstMove() throws TicTacToeException {358 UUID gameId = UUID.randomUUID();359 UUID p1 = UUID.randomUUID();360 Game game = new Game(gameId, p1);361 Mockito.when(gameDao.getById(any())).thenReturn(Optional.of(game));362 Mockito.when(gameDao.save(any())).then(AdditionalAnswers.returnsFirstArg());363 game = gameService.addPlayerToGame(gameId);364 var p2 = game.getPlayerTwoId();365 GameMove p1m1 = new GameMove(p1, 0, 0);366 game = gameService.play(gameId, p1m1);367 assertEquals(p2, gameService.getGameState(gameId).getNextPlayer());368 }369}...

Full Screen

Full Screen

Source:InstanceServiceTest.java Github

copy

Full Screen

...76 final long CONT_IMAGE_ID = 0;77 when(containerImageRepository.findById(CONT_IMAGE_ID))78 .thenReturn(Optional.of(new ContainerImage("foo Image", "bravo", "desc")));79 when(serverRepository.findAll()).thenReturn(new LinkedList<>(Collections.singletonList(mockServer)));80 when(instanceRepository.save(any())).then(AdditionalAnswers.returnsFirstArg());81 when(instanceGpuRepository.saveAll(anyList())).then(AdditionalAnswers.returnsFirstArg());82 when(serverRepository.findAll()).thenReturn(getNewServerList());83 when(serverPortRepository.fetchFreeInternalPortOfServer(mockServer)).thenReturn(Collections.emptyList());84 when(serverPortRepository.fetchFreeExternalPortOfServer(mockServer)).thenReturn(Collections.emptyList());85 when(serverPortRepository.saveAll(anyList())).then(AdditionalAnswers.returnsFirstArg());86 when(sshExecutor.createNewInstance(any(), any(), anyList(), any(), any(), anyString()))87 .thenReturn(88 new SshResult<>(null, new InstanceCreationDto("FOO_CONTAINER_ID", "FOO_HASH")));89 DCloudError error = instanceService.createInstance(90 UUID.randomUUID().toString(),91 new InstanceDto.Post("foo", mockUser, "sudoerId", "sudoerPwd",92 "purpose", CONT_IMAGE_ID, 1,93 false, 8, true,94 new ArrayList<>(), new ArrayList<>()), LocalDateTime.MAX);95 assertThat(error, is(nullValue()));96 }97 @Test98 public void createInstance_는서버에충분한자원이있다면_요청받은_갯수만큼의_InstanceGPU를_할당하여_새로운_인스턴스를_생성한다() throws Exception {99 final int REQUESTED_GPU = 8;100 User mockUser = User.of("loginId", "password", "John Doe", "organization", "010-0000-0000", "abc@dgu.ac.kr");101 Server mockServer = new Server(0, "192.168.1.11", 22, 10000, new ServerResource());102 final long CONT_IMAGE_ID = 0;103 when(containerImageRepository.findById(CONT_IMAGE_ID))104 .thenReturn(Optional.of(new ContainerImage("foo Image", "bravo", "desc")));105 when(serverRepository.findAll()).thenReturn(new LinkedList<>(Collections.singletonList(mockServer)));106 when(instanceRepository.save(any())).then(AdditionalAnswers.returnsFirstArg());107 when(instanceGpuRepository.saveAll(anyList())).then(AdditionalAnswers.returnsFirstArg());108 when(serverRepository.findAll()).thenReturn(getNewServerList());109 when(serverPortRepository.fetchFreeInternalPortOfServer(mockServer)).thenReturn(Collections.emptyList());110 when(serverPortRepository.fetchFreeExternalPortOfServer(mockServer)).thenReturn(Collections.emptyList());111 when(serverPortRepository.saveAll(anyList())).then(AdditionalAnswers.returnsFirstArg());112 when(sshExecutor.createNewInstance(any(), any(), anyList(), any(), any(), anyString()))113 .thenReturn(114 new SshResult<>(null, new InstanceCreationDto("FOO_CONTAINER_ID", "FOO_HASH")));115 DCloudError error = instanceService.createInstance(116 UUID.randomUUID().toString(),117 new InstanceDto.Post("foo", mockUser, "sudoerId", "sudoerPwd",118 "purpose", CONT_IMAGE_ID, 1,119 false, REQUESTED_GPU, true,120 new ArrayList<>(), new ArrayList<>()), LocalDateTime.MAX);121 assertThat(error, is(nullValue()));122 ArgumentCaptor<List<InstanceGpu>> captor = ArgumentCaptor.forClass((Class) List.class);123 verify(instanceGpuRepository, Mockito.times(1)).saveAll(captor.capture());124 List<InstanceGpu> SAVED_INSTANCE_GPUS = captor.getValue();125 assertThat(SAVED_INSTANCE_GPUS.size(), is(REQUESTED_GPU));...

Full Screen

Full Screen

Source:ServiceTests.java Github

copy

Full Screen

...42 @DisplayName("Empty basket should be created")43 void shouldCreateEmptyBasket() {44 Mockito.when(basketRepositoryMock45 .save(Mockito.any(BasketEntity.class)))46 .then(AdditionalAnswers.returnsFirstArg());47 Basket basket = basketService.createBasket();48 assertThat(basket.getProducts()).isEmpty();49 }50 @Test51 @DisplayName("Get basket method should return the correct basket")52 void shouldGetCorrectBasket() {53 Mockito.when(basketRepositoryMock54 .save(Mockito.any(BasketEntity.class)))55 .then(AdditionalAnswers.returnsFirstArg());56 Basket basket = basketService.createBasket();57 Mockito.when(basketRepositoryMock58 .findById(basket.getId()))59 .thenReturn(Optional.of(BasketMapper.basketToEntity(basket)));60 Basket storedBasket = basketService.getBasket(basket.getId());61 assertThat(storedBasket).isEqualTo(basket);62 }63 @Test64 void shouldGetExistingProduct() {65 Product apple = Product.valueOf(0, "Apple", Money.valueOfSEK(9.24));66 Product expected = productService.findById(apple.getId());67 assertThat(expected).isEqualTo(apple);68 }69 @Test70 void shouldThrowForNonExistingBasket() {71 UUID id = UUID.randomUUID();72 Mockito.when(basketRepositoryMock.findById(id))73 .thenReturn(Optional.empty());74 assertThrows(NotFoundException.class, () -> basketService.getBasket(id));75 }76 @Test77 void shouldThrowForNonExistingProduct() {78 Product faultyProduct = Product.valueOf(-1, "Fail", Money.ZERO("SEK"));79 assertThrows(NotFoundException.class, () -> productService.findById(faultyProduct.getId()));80 }81 @Test82 void shouldThrowForAddNonExistingProductToBasket() {83 Mockito.when(basketRepositoryMock.save(Mockito.any(BasketEntity.class)))84 .then(AdditionalAnswers.returnsFirstArg());85 Basket basket = basketService.createBasket();86 assertThrows(NotFoundException.class, () -> basketService.addProductToBasket(basket.getId(), -1));87 }88 @Test89 void shouldThrowForAddProductToNonExistingBasket() {90 Product apple = Product.valueOf(0, "Apple", Money.valueOfSEK(24.2));91 assertThrows(NotFoundException.class, () -> basketService.addProductToBasket(null, apple.getId()));92 }93 //ShouldAddProductToBasket94}...

Full Screen

Full Screen

Source:PropostaServiceTest.java Github

copy

Full Screen

...57 verify(propostaRepository, times(1)).findById(1L);58 }59 @Test60 void criarProposta() throws TransicaoDeStatusNaoSuportadaException {61 Mockito.when(propostaRepository.save(ArgumentMatchers.any(Proposta.class))).then(AdditionalAnswers.returnsFirstArg());62 Mockito.when(clienteRepository.save(ArgumentMatchers.any(Cliente.class))).then(AdditionalAnswers.returnsFirstArg());63 Proposta novaProposta = propostaService.criarProposta(cliente);64 verify(clienteRepository, times(1)).save(cliente);65 assertThat(novaProposta.getPropostaStatus(), equalTo(PropostaStatusEnum.DADOS_INICIAIS_INFORMADOS));66 }67 @Test68 void cadastrarEndereco() throws TransicaoDeStatusNaoSuportadaException {69 Mockito.when(enderecoRepository.save(ArgumentMatchers.any(Endereco.class))).then(AdditionalAnswers.returnsFirstArg());70 propostaService.cadastrarEndereco(proposta, endereco);71 verify(enderecoRepository, times(1)).save(endereco);72 assertThat(proposta.getPropostaStatus(), equalTo(PropostaStatusEnum.ENDERECO_INFORMADO));73 }74 @Test75 void cadastrarDocumento() throws TransicaoDeStatusNaoSuportadaException {76 Mockito.when(propostaRepository.save(ArgumentMatchers.any(Proposta.class))).then(AdditionalAnswers.returnsFirstArg());77 Proposta novaProposta = propostaService.cadastrarDocumento(proposta, new MockMultipartFile(78 "cpf",79 "cpf.pdf",80 MediaType.APPLICATION_PDF_VALUE,81 "<<cpf pdf data>>".getBytes(StandardCharsets.UTF_8)));82 verify(propostaRepository, times(1)).save(proposta);83 assertThat(novaProposta.getPropostaStatus(), equalTo(PropostaStatusEnum.AGUARDANDO_ACEITE));84 }85 @Test86 void aceitarProposta() throws TransicaoDeStatusNaoSuportadaException {87 Mockito.when(propostaRepository.save(ArgumentMatchers.any(Proposta.class))).then(AdditionalAnswers.returnsFirstArg());88 Proposta novaProposta = propostaService.aceitarProposta(proposta);89 verify(propostaRepository, times(1)).save(proposta);90 assertThat(novaProposta.getPropostaStatus(), equalTo(PropostaStatusEnum.ACEITE_POSITIVO));91 }92}...

Full Screen

Full Screen

Source:TextAnalyzerTest.java Github

copy

Full Screen

...46 );47 }48 @Test49 public void shouldProcessEntireWordFeedOnAllAnalyzers() {50 doAnswer( AdditionalAnswers.returnsFirstArg() ).when( qualityControl1 ).applyQualityMeasures( anyString() );51 doAnswer( AdditionalAnswers.returnsFirstArg() ).when( qualityControl2 ).applyQualityMeasures( anyString() );52 List< String > words = asList( "first", "second", "third", "fourth" );53 WordFeed wordFeed = new ListWordFeed( words );54 systemUnderTest.process( wordFeed );55 InOrder order = inOrder( analyzer1, analyzer2 );56 for ( String word : words ) {57 order.verify( analyzer1 ).analyze( word );58 order.verify( analyzer2 ).analyze( word );59 }60 }61 @Test62 public void shouldReportMultipleAnalyzers() {63 Reporter reporter = mock( Reporter.class );64 systemUnderTest.report( reporter );65 InOrder order = inOrder( analyzer1, analyzer2 );...

Full Screen

Full Screen

Source:StubbingWithAdditionalAnswers.java Github

copy

Full Screen

...9import org.mockito.runners.MockitoJUnitRunner;10import org.mockitousage.IMethods;11import static org.fest.assertions.Assertions.assertThat;12import static org.mockito.AdditionalAnswers.returnsArgAt;13import static org.mockito.AdditionalAnswers.returnsFirstArg;14import static org.mockito.AdditionalAnswers.returnsLastArg;15import static org.mockito.AdditionalAnswers.returnsSecondArg;16import static org.mockito.BDDMockito.given;17import static org.mockito.Matchers.anyInt;18import static org.mockito.Matchers.anyObject;19import static org.mockito.Matchers.anyString;20import static org.mockito.Matchers.anyVararg;21import static org.mockito.Matchers.eq;22@RunWith(MockitoJUnitRunner.class)23public class StubbingWithAdditionalAnswers {24 @Mock IMethods iMethods;25 @Test26 public void can_return_arguments_of_invocation() throws Exception {27 given(iMethods.objectArgMethod(anyObject())).will(returnsFirstArg());28 given(iMethods.threeArgumentMethod(eq(0), anyObject(), anyString())).will(returnsSecondArg());29 given(iMethods.threeArgumentMethod(eq(1), anyObject(), anyString())).will(returnsLastArg());30 assertThat(iMethods.objectArgMethod("first")).isEqualTo("first");31 assertThat(iMethods.threeArgumentMethod(0, "second", "whatever")).isEqualTo("second");32 assertThat(iMethods.threeArgumentMethod(1, "whatever", "last")).isEqualTo("last");33 }34 @Test35 public void can_return_expanded_arguments_of_invocation() throws Exception {36 given(iMethods.varargsObject(eq(1), anyVararg())).will(returnsArgAt(3));37 assertThat(iMethods.varargsObject(1, "bob", "alexander", "alice", "carl")).isEqualTo("alice");38 }39 @Test40 public void can_return_primitives_or_wrappers() throws Exception {41 given(iMethods.toIntPrimitive(anyInt())).will(returnsFirstArg());42 given(iMethods.toIntWrapper(anyInt())).will(returnsFirstArg());43 assertThat(iMethods.toIntPrimitive(1)).isEqualTo(1);44 assertThat(iMethods.toIntWrapper(1)).isEqualTo(1);45 }46}...

Full Screen

Full Screen

Source:PlayerServiceTests.java Github

copy

Full Screen

...35 MockitoAnnotations.initMocks(this);36 game = new Game(); 37 Mockito.when(gameRepository.findById(1L)).thenReturn(Optional.of(game));38 Mockito.when(gameRepository.findById(2L)).thenReturn(Optional.empty());39 Mockito.when(gameRepository.save(Mockito.any(Game.class))).then(AdditionalAnswers.returnsFirstArg());40 41 Mockito.when(repository.findById(1L)).thenReturn(Optional.of(new Player("test")));42 Mockito.when(repository.findById(2L)).thenReturn(Optional.empty());43 Mockito.when(repository.save(Mockito.any(Player.class))).then(AdditionalAnswers.returnsFirstArg());44 }45 46 @Test47 public void addPlayerToGameWhenThereIsNoGame() {48 Exception exception = assertThrows(49 GameNotFoundException.class,50 () -> service.addPlayer("", 3L));51 assertTrue(exception.getMessage().contains("Cannot find a game with ID"));52 }53 54 @Test55 public void addPlayerToGame() {56 service.addPlayer("", 1L);57 assertTrue(game.getPlayers().size() > 0);...

Full Screen

Full Screen

Source:PlayerServiceTest.java Github

copy

Full Screen

...15 }16 @Test17 public void create() throws Exception {18 PlayerDao playerDao = Mockito.mock(PlayerDao.class);19 Mockito.when(playerDao.create(Mockito.any(PlayerRecord.class))).then(AdditionalAnswers.returnsFirstArg());20 Player toCreate = Player.builder()21 .userId(UUID.randomUUID().toString())22 .build();23 PlayerService instance = new PlayerService(playerDao);24 Player created = instance.create(toCreate);25 Assert.assertEquals("user id is saved", toCreate.getUserId(), created.getUserId());26 Assert.assertNotNull("id is generated", created.getId());27 }28 @Test29 public void update() throws Exception {30 // data31 String id = UUID.randomUUID().toString();32 int ratingAdjustment = 32;33 int newGames = 1;34 int newWins = 1;35 int newLosses = 0;36 PlayerRecord existingRecord = PlayerRecord.builder()37 .id(id)38 .currentRating(1200)39 .gamesPlayed(1)40 .wins(1)41 .losses(0)42 .build();43 // mocks44 PlayerDao playerDao = Mockito.mock(PlayerDao.class);45 Mockito.when(playerDao.update(Mockito.any(PlayerRecord.class))).then(AdditionalAnswers.returnsFirstArg());46 Mockito.when(playerDao.findOne(id)).thenReturn(existingRecord);47 PlayerService instance = new PlayerService(playerDao);48 Player updated = instance.update(id, ratingAdjustment, newGames, newWins, newLosses);49 Assert.assertEquals(existingRecord.getCurrentRating() + ratingAdjustment, updated.getCurrentRating());50 }51 @Test52 public void reset() throws Exception {53 }54 @Test55 public void getPlayer() throws Exception {56 }57}...

Full Screen

Full Screen

returnsFirstArg

Using AI Code Generation

copy

Full Screen

1package org.mockito.examples;2import static org.junit.Assert.assertEquals;3import static org.mockito.Mockito.mock;4import static org.mockito.Mockito.when;5import static org.mockito.AdditionalAnswers.returnsFirstArg;6import java.util.List;7import org.junit.Test;8public class ReturnsFirstArgExample {9 public void shouldReturnFirstArg() {10 List mock = mock(List.class);11 when(mock.add(returnsFirstArg())).thenReturn(true);12 assertEquals(true, mock.add("one"));13 }14}15package org.mockito.examples;16import static org.junit.Assert.assertEquals;17import static org.mockito.Mockito.mock;18import static org.mockito.Mockito.when;19import static org.mockito.AdditionalAnswers.returnsFirstArg;20import java.util.List;21import org.junit.Test;22public class ReturnsFirstArgExample {23 public void shouldReturnFirstArg() {24 List mock = mock(List.class);25 when(mock.add(returnsFirstArg())).thenReturn(true);26 assertEquals(true, mock.add("one"));27 }28}29package org.mockito.examples;30import static org.junit.Assert.assertEquals;31import static org.mockito.Mockito.mock;32import static org.mockito.Mockito.when;33import static org.mockito.AdditionalAnswers.returnsFirstArg;34import java.util.List;35import org.junit.Test;36public class ReturnsFirstArgExample {37 public void shouldReturnFirstArg() {38 List mock = mock(List.class);39 when(mock.add(returnsFirstArg())).thenReturn(true);40 assertEquals(true, mock.add("one"));41 }42}43package org.mockito.examples;44import static org.junit.Assert.assertEquals;45import static org.mockito.Mockito.mock;46import static org.mockito.Mockito.when;47import static org.mockito.AdditionalAnswers.returnsFirstArg;48import java.util.List;49import org.junit.Test;50public class ReturnsFirstArgExample {51 public void shouldReturnFirstArg() {52 List mock = mock(List.class);53 when(mock.add(returnsFirstArg())).thenReturn(true);54 assertEquals(true, mock.add("one"));55 }56}57package org.mockito.examples;58import

Full Screen

Full Screen

returnsFirstArg

Using AI Code Generation

copy

Full Screen

1import org.mockito.AdditionalAnswers;2import static org.mockito.Mockito.*;3public class returnsFirstArg {4 public static void main(String[] args) {5 List list = mock(List.class);6 when(list.get(anyInt())).then(AdditionalAnswers.returnsFirstArg());7 list.get(1);8 verify(list).get(1);9 }10}11import org.mockito.AdditionalAnswers;12import static org.mockito.Mockito.*;13public class returnsFirstArg {14 public static void main(String[] args) {15 List list = mock(List.class);16 when(list.get(anyInt())).then(AdditionalAnswers.returnsFirstArg());17 list.get(1);18 verify(list).get(1);19 }20}21import org.mockito.AdditionalAnswers;22import static org.mockito.Mockito.*;23public class returnsFirstArg {24 public static void main(String[] args) {25 List list = mock(List.class);26 when(list.get(anyInt())).then(AdditionalAnswers.returnsFirstArg());27 list.get(1);28 verify(list).get(1);29 }30}31import org.mockito.AdditionalAnswers;32import static org.mockito.Mockito.*;33public class returnsFirstArg {34 public static void main(String[] args) {35 List list = mock(List.class);36 when(list.get(anyInt())).then(AdditionalAnswers.returnsFirstArg());37 list.get(1);38 verify(list).get(1);39 }40}41import org.mockito.AdditionalAnswers;42import static org.mockito.Mockito.*;43public class returnsFirstArg {44 public static void main(String[] args) {45 List list = mock(List.class);46 when(list.get(anyInt())).then(AdditionalAnswers.returnsFirstArg());47 list.get(1);48 verify(list).get(1);49 }50}51import org.mockito.AdditionalAnswers;52import static org.mockito.Mockito.*;53public class returnsFirstArg {54 public static void main(String[] args) {

Full Screen

Full Screen

returnsFirstArg

Using AI Code Generation

copy

Full Screen

1package org.mockito.examples;2import static org.mockito.Mockito.mock;3import static org.mockito.Mockito.when;4import static org.mockito.AdditionalAnswers.returnsFirstArg;5public class ReturnsFirstArg {6 public static void main(String[] args) {7 ReturnsFirstArg returnsFirstArg = mock(ReturnsFirstArg.class);8 when(returnsFirstArg.returnsFirstArg("Hello")).then(returnsFirstArg());9 System.out.println(returnsFirstArg.returnsFirstArg("Hello"));10 }11}12package org.mockito.examples;13import static org.mockito.Mockito.mock;14import static org.mockito.Mockito.when;15import static org.mockito.AdditionalAnswers.returnsFirstArg;16public class ReturnsFirstArg {17 public static void main(String[] args) {18 ReturnsFirstArg returnsFirstArg = mock(ReturnsFirstArg.class);19 when(returnsFirstArg.returnsFirstArg("Hello")).then(returnsFirstArg());20 System.out.println(returnsFirstArg.returnsFirstArg("World"));21 }22}23package org.mockito.examples;24import static org.mockito.Mockito.mock;25import static org.mockito.Mockito.when;26import static org.mockito.AdditionalAnswers.returnsFirstArg;27public class ReturnsFirstArg {28 public static void main(String[] args) {29 ReturnsFirstArg returnsFirstArg = mock(ReturnsFirstArg.class);30 when(returnsFirstArg.returnsFirstArg("Hello")).then(returnsFirstArg());31 System.out.println(returnsFirstArg.returnsFirstArg("World"));32 System.out.println(returnsFirstArg.returnsFirstArg("Hello"));33 }34}35package org.mockito.examples;36import static org.mockito.Mockito.mock;37import static org.mockito.Mockito.when;38import static org.mockito.AdditionalAnswers.returnsFirstArg;39public class ReturnsFirstArg {40 public static void main(String[] args) {41 ReturnsFirstArg returnsFirstArg = mock(ReturnsFirstArg.class);42 when(returnsFirstArg.returnsFirstArg("Hello")).then(returnsFirstArg());43 System.out.println(returnsFirstArg.returnsFirstArg("World"));44 System.out.println(returnsFirst

Full Screen

Full Screen

returnsFirstArg

Using AI Code Generation

copy

Full Screen

1package org.mockito;2import org.mockito.invocation.InvocationOnMock;3import org.mockito.stubbing.Answer;4import org.mockito.stubbing.Stubber;5import org.mockito.stubbing.VoidAnswer2;6import org.mockito.stubbing.VoidAnswer3;7import org.mockito.stubbing.VoidAnswer4;8import org.mockito.stubbing.VoidAnswer5;9import org.mockito.stubbing.VoidAnswer6;10import org.mockito.stubbing.VoidAnswer7;11import org.mockito.stubbing.VoidAnswer8;12import org.mockito.stubbing.VoidAnswer9;13import org.mockito.stubbing.VoidAnswer10;14import org.mockito.stubbing.VoidAnswer11;15import org.mockito.stubbing.VoidAnswer12;16import org.mockito.stubbing.VoidAnswer13;17import org.mockito.stubbing.VoidAnswer14;18import org.mockito.stubbing.VoidAnswer15;19import org.mockito.stubbing.VoidAnswer16;20import org.mockito.stubbing.VoidAnswer17;21import org.mockito.stubbing.VoidAnswer18;22import org.mockito.stubbing.VoidAnswer19;23import org.mockito.stubbing.VoidAnswer20;24import org.mockito.stubbing.VoidAnswer21;25import org.mockito.stubbing.VoidAnswer22;26import org.mockito.stubbing.VoidAnswer23;27import org.mockito.stubbing.VoidAnswer24;28import org.mockito.stubbing.VoidAnswer25;29import org.mockito.stubbing.VoidAnswer26;30import org.mockito.stubbing.VoidAnswer27;31import org.mockito.stubbing.VoidAnswer28;32import org.mockito.stubbing.VoidAnswer29;33import org.mockito.stubbing.VoidAnswer30;34import org.mockito.stubbing.VoidAnswer31;35import org.mockito.stubbing.VoidAnswer32;36import org.mockito.stubbing.VoidAnswer33;37import org.mockito.stubbing.VoidAnswer34;38import org.mockito.stubbing.VoidAnswer35;39import org.mockito.stubbing.VoidAnswer36;40import org.mockito.stubbing.VoidAnswer37;41import org.mockito.stubbing.VoidAnswer38;42import org.mockito.stubbing.VoidAnswer39;43import org.mockito.stubbing.VoidAnswer40;44import org.mockito.stubbing.VoidAnswer41;45import org.mockito.stubbing.VoidAnswer42;46import org.mockito.stubbing.VoidAnswer43;47import org.mockito.stubbing.VoidAnswer44;48import org.mockito.stubbing.VoidAnswer45;49import org.mockito.stubbing.VoidAnswer46;50import org.mockito.stubbing.VoidAnswer47;51import org.mockito.stubbing.VoidAnswer48;52import org.mockito.stubbing.VoidAnswer49;53import org.mockito.stubbing.VoidAnswer50;54import org.mockito.stubbing.VoidAnswer51;55import org.mockito.stubbing.VoidAnswer52;56import org.mockito.stubbing.VoidAnswer53;

Full Screen

Full Screen

returnsFirstArg

Using AI Code Generation

copy

Full Screen

1public class Test {2 public void test() {3 List list = mock(List.class);4 when(list.get(anyInt())).then(returnsFirstArg());5 assertEquals(0, list.get(0));6 assertEquals(1, list.get(1));7 assertEquals(2, list.get(2));8 }9}10org.junit.ComparisonFailure: expected:<[0]> but was:<[null]> at org.junit.Assert.assertEquals(Assert.java:115) at org.junit.Assert.assertEquals(Assert.java:144) at Test.test(Test.java:10)11import static org.mockito.Mockito.*;12import org.junit.Test;13import java.util.List;14public class Test {15 public void test() {16 List list = mock(List.class);17 when(list.get(anyInt())).then(returnsFirstArg());18 assertEquals(0, list.get(0));19 assertEquals(1, list.get(1));20 assertEquals(2, list.get(2));21 }22}23org.junit.ComparisonFailure: expected:<[0]> but was:<[null]> at org.junit.Assert.assertEquals(Assert.java:115) at org.junit.Assert.assertEquals(Assert.java:144) at Test.test(Test.java:10)24Related posts: Mockito – How to use thenThrow() method of Mockito class? Mockito – How to use then() method of Mockito class? Mockito – How to use thenAnswer() method of Mockito class? Mockito – How to use thenCallRealMethod() method of Mockito class? Mockito – How to use thenReturn() method of Mockito class? Mockito – How to use thenReturn() method of Mockito class? Mockito – How to use doReturn() method of Mockito class? Mockito – How to use doAnswer() method of Mockito class? Mockito – How to use doThrow() method of Mockito class? Mockito – How to use doNothing() method of Mockito class? Mockito – How to use doCallRealMethod() method of Mockito class? Mockito – How to use verify() method of Mockito class? Mockito – How to use verifyNoMoreInteractions() method of Mockito class? Mockito – How to use verifyZeroInteractions() method of

Full Screen

Full Screen

returnsFirstArg

Using AI Code Generation

copy

Full Screen

1package org.mockito.examples;2import static org.mockito.Mockito.*;3import org.mockito.AdditionalAnswers;4import org.mockito.Mock;5import org.mockito.MockitoAnnotations;6import org.mockito.exceptions.base.MockitoException;7import org.mockito.internal.matchers.Any;8import static org.junit.Assert.*;9import org.junit.Before;10import org.junit.Test;11public class ReturnsFirstArgTest {12 private Collaborator collaborator;13 public void setUp() {14 MockitoAnnotations.initMocks(this);15 }16 public void test() {17 when(collaborator.doSomething(anyString())).then(AdditionalAnswers.returnsFirstArg());18 assertEquals("foo", collaborator.doSomething("foo"));19 assertEquals("bar", collaborator.doSomething("bar"));20 Collaborator real = new Collaborator();21 try {22 real.doSomething("foo");23 fail();24 } catch (MockitoException e) {25 }26 }27 private class Collaborator {28 public String doSomething(String str) {29 throw new MockitoException("should not be called");30 }31 }32}33package org.mockito.examples;34import static org.mockito.Mockito.*;35import org.mockito.AdditionalAnswers;36import org.mockito.Mock;37import org.mockito.MockitoAnnotations;38import org.mockito.exceptions.base.MockitoException;39import org.mockito.internal.matchers.Any;40import static org.junit.Assert.*;41import org.junit.Before;42import org.junit.Test;43public class ReturnsFirstArgTest {44 private Collaborator collaborator;45 public void setUp() {46 MockitoAnnotations.initMocks(this);47 }48 public void test() {49 when(collaborator.doSomething(anyString())).then(AdditionalAnswers.returnsFirstArg());50 assertEquals("foo", collaborator.doSomething("foo"));51 assertEquals("bar", collaborator.doSomething("bar"));52 Collaborator real = new Collaborator();53 try {54 real.doSomething("foo");55 fail();56 } catch (MockitoException e) {57 }58 }59 private class Collaborator {60 public String doSomething(String str) {

Full Screen

Full Screen

returnsFirstArg

Using AI Code Generation

copy

Full Screen

1import static org.mockito.Mockito.*;2class FirstArgTest {3 void testFirstArg() {4 List<String> mockedList = mock(List.class);5 when(mockedList.get(anyInt())).then(returnsFirstArg());6 assertEquals("first", mockedList.get(0));7 }8}

Full Screen

Full Screen

returnsFirstArg

Using AI Code Generation

copy

Full Screen

1import org.mockito.AdditionalAnswers;2import org.mockito.Mockito;3public class returnsFirstArgExample {4public static void main(String[] args) {5Object mock = Mockito.mock(Object.class);6Mockito.when(mock.toString()).then(AdditionalAnswers.returnsFirstArg());7System.out.println(mock.toString("Hello"));8System.out.println(mock.toString("World"));9}10}11Related posts: Mockito – returnsSecondArg() method example Mockito – returnsArgAt() me

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful