Best Easymock code snippet using org.easymock.internal.MockBuilder.createNiceMock
Source:DefaultSapOaaCommerceStockServiceTest.java
...6566 final List<ATPAvailability> availabilityList = getDefaultATPAvailabilityList();6768 // Mock ATPService69 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);70 EasyMock71 .expect(72 atpServiceMock.callRestAvailabilityServiceForProduct(EasyMock.anyObject(String.class),73 EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class))).andReturn(availabilityList)74 .anyTimes();75 EasyMock.replay(atpServiceMock);76 classUnderTest.setAtpService(atpServiceMock);77 Assert.assertNotNull(classUnderTest.getAtpService());7879 //Mock ATPStrategy80 final ATPAggregationStrategy atpStrategyMock = EasyMock.createNiceMock(ATPAggregationStrategy.class);81 EasyMock82 .expect(83 atpStrategyMock.aggregateAvailability(EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class),84 EasyMock.anyObject(PointOfServiceModel.class), EasyMock.anyObject(List.class)))85 .andReturn(Long.valueOf(300)).anyTimes();86 EasyMock.replay(atpStrategyMock);87 classUnderTest.setAtpAggregationStrategy(atpStrategyMock);88 Assert.assertNotNull(classUnderTest.getAtpAggregationStrategy());8990 final List<CartEntryModel> cartEntryModel = new ArrayList<>();91 final CartEntryModel cartEntry = new CartEntryModel();92 cartEntryModel.add(cartEntry);939495 final CartService cartServiceMock = EasyMock.createNiceMock(CartService.class);96 EasyMock97 .expect(98 cartServiceMock.getEntriesForProduct(EasyMock.anyObject(CartModel.class),99 EasyMock.anyObject(ProductModel.class))).andReturn(cartEntryModel).anyTimes();100 EasyMock.replay(cartServiceMock);101102103 final CartModel cartModel = new CartModel();104105 final Long actualStockAvailability = classUnderTest.getAvailableStockLevel(cartModel.getGuid(), "4711", new ProductModel(),106 null);107108 Assert.assertEquals(ATP_QTY_1.longValue(), actualStockAvailability.longValue());109 }110111 @Test112 public void getStockLevelForProductAndPointOfServiceOfflineTest()113 {114 final PointOfServiceModel pos = new PointOfServiceModel();115 pos.setName("test");116117 // Mock ATPService118 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);119 atpServiceMock.callRestAvailabilityServiceForProductAndSource(EasyMock.anyObject(ProductModel.class),120 EasyMock.anyObject(String.class));121 EasyMock.expectLastCall().andThrow(new BackendDownException()).anyTimes();122 EasyMock.replay(atpServiceMock);123124 classUnderTest.setAtpService(atpServiceMock);125126 Assert.assertEquals(0, classUnderTest.getStockLevelForProductAndPointOfService(new ProductModel(), pos).longValue());127 }128129 @Test130 public void getPosAndStockLevelStatusForProductOfflineTest()131 {132 final BaseStoreModel baseStore = new BaseStoreModel();133 final PointOfServiceModel pos = new PointOfServiceModel();134 pos.setName("test");135 pos.setType(PointOfServiceTypeEnum.STORE);136137 final List<PointOfServiceModel> posList = new ArrayList();138 posList.add(pos);139140 baseStore.setPointsOfService(posList);141142 // Mock ATPService143 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);144 atpServiceMock.callRestAvailabilityServiceForProductAndSources(EasyMock.anyObject(String.class),145 EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class), EasyMock.anyObject(List.class));146 EasyMock.expectLastCall().andThrow(new BackendDownException()).anyTimes();147 EasyMock.replay(atpServiceMock);148149 classUnderTest.setAtpService(atpServiceMock);150151 Assert.assertEquals(0, classUnderTest.getPosAndStockLevelStatusForProduct(new ProductModel(), baseStore).size());152 }153154155 @Test156 public void getAvailableStockLevelNoATPServiceAvailableTest()157 {158 final long qtyToAdd = 1000000;159160 // Mock ATPService161 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);162 EasyMock.expect(163 atpServiceMock.callRestAvailabilityServiceForProduct(EasyMock.anyObject(String.class),164 EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class))).andThrow(new ATPException());165 EasyMock.replay(atpServiceMock);166 classUnderTest.setAtpService(atpServiceMock);167168 // if the ATP Service is unavailable we expect requested value = stock value169 final CartModel cartModel = new CartModel();170 boolean reached = false;171 try172 {173 final Long actualStockAvailability = classUnderTest.getAvailableStockLevel(cartModel.getGuid(), "4711",174 new ProductModel(), null);175 Assert.assertEquals(qtyToAdd, actualStockAvailability.longValue());176 }177 catch (final ATPException e)178 {179 reached = true;180 }181 Assert.assertTrue(reached);182183 }184185 @Test186 public void getAvailableStockForPointOfServiceNoATPServiceAvailableTest()187 {188 // Mock ATPService189 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);190 EasyMock.expect(191 atpServiceMock.callRestAvailabilityServiceForProductAndSource(EasyMock.anyObject(ProductModel.class),192 EasyMock.anyObject(String.class))).andThrow(new ATPException());193 EasyMock.replay(atpServiceMock);194 classUnderTest.setAtpService(atpServiceMock);195196 //Mock ATPStrategy197 final ATPAggregationStrategy atpStrategyMock = EasyMock.createNiceMock(ATPAggregationStrategy.class);198 EasyMock199 .expect(200 atpStrategyMock.aggregateAvailability(EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class),201 EasyMock.anyObject(PointOfServiceModel.class), EasyMock.anyObject(List.class)))202 .andReturn(Long.valueOf(0)).anyTimes();203 EasyMock.replay(atpStrategyMock);204 classUnderTest.setAtpAggregationStrategy(atpStrategyMock);205206 final Long stock = classUnderTest.getStockLevelForProductAndPointOfService(new ProductModel(), new PointOfServiceModel());207208 Assert.assertEquals(0, stock.longValue());209 }210211 @Test212 public void getAvailableStockForPointOfServiceTest()213 {214 final List<ATPAvailability> availabilityList = getDefaultATPAvailabilityList();215216 // Mock ATPService217 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);218 EasyMock219 .expect(220 atpServiceMock.callRestAvailabilityServiceForProductAndSource(EasyMock.anyObject(ProductModel.class),221 EasyMock.anyObject(String.class))).andReturn(availabilityList).anyTimes();222 EasyMock.replay(atpServiceMock);223 classUnderTest.setAtpService(atpServiceMock);224225226 //Mock ATPStrategy227 final ATPAggregationStrategy atpStrategyMock = EasyMock.createNiceMock(ATPAggregationStrategy.class);228 EasyMock229 .expect(230 atpStrategyMock.aggregateAvailability(EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class),231 EasyMock.anyObject(PointOfServiceModel.class), EasyMock.anyObject(List.class)))232 .andReturn(Long.valueOf(0)).anyTimes();233 EasyMock.replay(atpStrategyMock);234 classUnderTest.setAtpAggregationStrategy(atpStrategyMock);235236 final Long actualStockAvailability = classUnderTest.getStockLevelForProductAndPointOfService(new ProductModel(),237 new PointOfServiceModel());238239 Assert.assertEquals(0, actualStockAvailability.longValue());240241 }242243 @Test244 public void getAvailabilityForProductTest()245 {246 final ProductModel product = new ProductModel();247 final List<ATPAvailability> availabilityList = getDefaultATPAvailabilityList();248249 // Mock ATPService250 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);251 EasyMock252 .expect(253 atpServiceMock.callRestAvailabilityServiceForProduct(EasyMock.anyObject(String.class),254 EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class))).andReturn(availabilityList)255 .anyTimes();256 EasyMock.replay(atpServiceMock);257 classUnderTest.setAtpService(atpServiceMock);258259 final List<ATPAvailability> atpResultList = classUnderTest.getAvailabilityForProduct(CART_GUID, "4711", product);260 Assert.assertNotNull(atpResultList);261 Assert.assertEquals(availabilityList, atpResultList);262 }263264 @Test265 public void getAvailabilityForProductAndSourcTest()266 {267268 final ProductModel product = new ProductModel();269 final PointOfServiceModel pointOfServiceModel = createStore();270271 final List<ATPAvailability> availabilityList = getDefaultATPAvailabilityList();272273 // Mock ATPService274 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);275 EasyMock276 .expect(277 atpServiceMock.callRestAvailabilityServiceForProductAndSource(EasyMock.anyObject(String.class),278 EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class),279 EasyMock.anyObject(String.class))).andReturn(availabilityList).anyTimes();280 EasyMock.replay(atpServiceMock);281 classUnderTest.setAtpService(atpServiceMock);282283 final List<ATPAvailability> atpResultList = classUnderTest.getAvailabilityForProductAndSource(CART_GUID, "4711", product,284 pointOfServiceModel.getName());285 Assert.assertNotNull(atpResultList);286 Assert.assertEquals(availabilityList, atpResultList);287 }288289290 @Test291 public void getAvailabilityForProductsTest()292 {293 final List<ProductModel> productList = new ArrayList<>();294 final ProductModel product1 = new ProductModel();295 product1.setCode(ARTICLE_1);296 final ProductModel product2 = new ProductModel();297 product1.setCode(ARTICLE_2);298 productList.add(product1);299 productList.add(product2);300301 final String productUnit = "PCE";302303 final List<ATPProductAvailability> atpProductAvailabilityList = new ArrayList<>();304305 final List<ATPAvailability> availabilityList = getDefaultATPAvailabilityList();306307 final ATPProductAvailability atpProductAvailability1 = new ATPProductAvailability();308 atpProductAvailability1.setAvailabilityList(availabilityList);309 atpProductAvailability1.setArticleId(ARTICLE_1);310 atpProductAvailabilityList.add(atpProductAvailability1);311312 final ATPProductAvailability atpProductAvailability2 = new ATPProductAvailability();313 atpProductAvailability2.setAvailabilityList(availabilityList);314 atpProductAvailability2.setArticleId(ARTICLE_2);315 atpProductAvailabilityList.add(atpProductAvailability2);316317 // Mock ATPService318 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);319 EasyMock320 .expect(321 atpServiceMock.callRestAvailabilityServiceForProducts(EasyMock.anyObject(String.class),322 EasyMock.anyObject(List.class), EasyMock.anyObject(String.class), EasyMock.anyObject(List.class)))323 .andReturn(atpProductAvailabilityList).anyTimes();324 EasyMock.replay(atpServiceMock);325 classUnderTest.setAtpService(atpServiceMock);326327 final List<ATPProductAvailability> atpResultList = classUnderTest.getAvailabilityForProducts(CART_GUID, "4711",328 productUnit, productList);329 Assert.assertNotNull(atpResultList);330 Assert.assertEquals(ARTICLE_1, atpResultList.get(0).getArticleId());331 Assert.assertEquals(availabilityList, atpResultList.get(0).getAvailabilityList());332 Assert.assertEquals(ARTICLE_2, atpResultList.get(1).getArticleId());333 Assert.assertEquals(availabilityList, atpResultList.get(1).getAvailabilityList());334 }335336337 @Test338 public void getAvailabilityForProductAndSourcesTest()339 {340 final List<String> sourcesList = new ArrayList<>();341 sourcesList.add(STORE_1);342 sourcesList.add(STORE_2);343344 final ProductModel product = new ProductModel();345 product.setCode(ARTICLE_1);346347 final List<ATPProductAvailability> atpProductAvailabilityList = new ArrayList<>();348349 final List<ATPAvailability> availabilityList = getDefaultATPAvailabilityList();350351 final ATPProductAvailability atpProductAvailability1 = new ATPProductAvailability();352 atpProductAvailability1.setAvailabilityList(availabilityList);353 atpProductAvailability1.setArticleId(ARTICLE_1);354 atpProductAvailability1.setSourceId(STORE_1);355 atpProductAvailabilityList.add(atpProductAvailability1);356357 final ATPProductAvailability atpProductAvailability2 = new ATPProductAvailability();358 atpProductAvailability2.setAvailabilityList(availabilityList);359 atpProductAvailability2.setArticleId(ARTICLE_1);360 atpProductAvailability2.setSourceId(STORE_2);361 atpProductAvailabilityList.add(atpProductAvailability2);362363 // Mock ATPService364 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);365 EasyMock366 .expect(367 atpServiceMock.callRestAvailabilityServiceForProductAndSources(EasyMock.anyObject(String.class),368 EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class), EasyMock.anyObject(List.class)))369 .andReturn(atpProductAvailabilityList).anyTimes();370 EasyMock.replay(atpServiceMock);371 classUnderTest.setAtpService(atpServiceMock);372373 final List<ATPProductAvailability> atpResultList = classUnderTest.getAvailabilityForProductAndSources(CART_GUID, "4711",374 product, sourcesList);375 Assert.assertNotNull(atpResultList);376 Assert.assertEquals(ARTICLE_1, atpResultList.get(0).getArticleId());377 Assert.assertEquals(STORE_1, atpResultList.get(0).getSourceId());378 Assert.assertEquals(availabilityList, atpResultList.get(0).getAvailabilityList());379 Assert.assertEquals(ARTICLE_1, atpResultList.get(1).getArticleId());380 Assert.assertEquals(STORE_2, atpResultList.get(1).getSourceId());381 Assert.assertEquals(availabilityList, atpResultList.get(1).getAvailabilityList());382 }383384 @Test385 @SuppressWarnings("PMD.MethodNamingConventions")386 public void getStockLevelStatusForProductAndBaseStoreTest_NoOfStock()387 {388 final BaseStoreModel baseStore = null;389 final ProductModel product = null;390391 final StockService stockServiceMock = EasyMock.createNiceMock(StockService.class);392 final Collection<StockLevelModel> stockCollection = new ArrayList<StockLevelModel>();393 EasyMock394 .expect(stockServiceMock.getStockLevels(EasyMock.anyObject(ProductModel.class), EasyMock.anyObject(Collection.class)))395 .andReturn(stockCollection).anyTimes();396 EasyMock.replay(stockServiceMock);397398 final WarehouseSelectionStrategy warehouseSelectionStrategyMock = createWarehouseSelectionStrategyMock(baseStore);399400 classUnderTest.setWarehouseSelectionStrategy(warehouseSelectionStrategyMock);401 classUnderTest.setStockService(stockServiceMock);402403 Assert.assertEquals(StockLevelStatus.OUTOFSTOCK,404 classUnderTest.getStockLevelStatusForProductAndBaseStore(product, baseStore));405 }406407 @Test408 @SuppressWarnings("PMD.MethodNamingConventions")409 public void getStockLevelStatusForProductAndBaseStoreTest_LowStock()410 {411 final BaseStoreModel baseStore = null;412 final ProductModel product = null;413414 final Collection<StockLevelModel> stockCollection = createStockCollection("Y", 150);415 final StockService stockServiceMock = createStockServiceMock(stockCollection);416 final WarehouseSelectionStrategy warehouseSelectionStrategyMock = createWarehouseSelectionStrategyMock(baseStore);417418 classUnderTest.setWarehouseSelectionStrategy(warehouseSelectionStrategyMock);419 classUnderTest.setStockService(stockServiceMock);420421 Assert.assertEquals(StockLevelStatus.LOWSTOCK, classUnderTest.getStockLevelStatusForProductAndBaseStore(product, baseStore));422 }423424 @Test425 @SuppressWarnings("PMD.MethodNamingConventions")426 public void getStockLevelStatusForProductAndBaseStoreTest_InStock()427 {428 final BaseStoreModel baseStore = null;429 final ProductModel product = null;430431 final Collection<StockLevelModel> stockCollection = createStockCollection("G", 150);432 final StockService stockServiceMock = createStockServiceMock(stockCollection);433 final WarehouseSelectionStrategy warehouseSelectionStrategyMock = createWarehouseSelectionStrategyMock(baseStore);434435 classUnderTest.setWarehouseSelectionStrategy(warehouseSelectionStrategyMock);436 classUnderTest.setStockService(stockServiceMock);437438 Assert.assertEquals(StockLevelStatus.INSTOCK, classUnderTest.getStockLevelStatusForProductAndBaseStore(product, baseStore));439440 }441442 @Test443 @SuppressWarnings("PMD.MethodNamingConventions")444 public void getStockLevelStatusForProductAndBaseStoreTest_OutOftock()445 {446 final BaseStoreModel baseStore = null;447 final ProductModel product = null;448449 final Collection<StockLevelModel> stockCollection = createStockCollection("R", 150);450 final StockService stockServiceMock = createStockServiceMock(stockCollection);451 final WarehouseSelectionStrategy warehouseSelectionStrategyMock = createWarehouseSelectionStrategyMock(baseStore);452453 classUnderTest.setWarehouseSelectionStrategy(warehouseSelectionStrategyMock);454 classUnderTest.setStockService(stockServiceMock);455456 Assert.assertEquals(StockLevelStatus.OUTOFSTOCK,457 classUnderTest.getStockLevelStatusForProductAndBaseStore(product, baseStore));458 }459460 @Test461 @SuppressWarnings("PMD.MethodNamingConventions")462 public void getStockLevelForProductAndBaseStoreTest_Null()463 {464 final BaseStoreModel baseStore = null;465 final ProductModel product = null;466467 // create partial mock to mock the internal method calls468 classUnderTest = EasyMock.createMockBuilder(DefaultSapOaaCommerceStockService.class)469 .addMockedMethod("getStockLevelStatusForProductAndBaseStore").createMock();470471 EasyMock.expect(classUnderTest.getStockLevelStatusForProductAndBaseStore(product, baseStore))472 .andReturn(StockLevelStatus.INSTOCK).anyTimes();473 EasyMock.replay(classUnderTest);474475476 Assert.assertNull(classUnderTest.getStockLevelForProductAndBaseStore(product, baseStore));477478 }479480 @Test481 @SuppressWarnings("PMD.MethodNamingConventions")482 public void getStockLevelForProductAndBaseStoreTest_OutOfStock()483 {484 final BaseStoreModel baseStore = null;485 final ProductModel product = null;486487 // create partial mock to mock the internal method calls488 classUnderTest = EasyMock.createMockBuilder(DefaultSapOaaCommerceStockService.class)489 .addMockedMethod("getStockLevelStatusForProductAndBaseStore").createMock();490491 EasyMock.expect(classUnderTest.getStockLevelStatusForProductAndBaseStore(product, baseStore))492 .andReturn(StockLevelStatus.OUTOFSTOCK).anyTimes();493 EasyMock.replay(classUnderTest);494495496 Assert.assertEquals(classUnderTest.getStockLevelForProductAndBaseStore(product, baseStore).longValue(), 0);497498 }499500 @Test501 @SuppressWarnings("PMD.MethodNamingConventions")502 public void getStockLevelForProductAndBaseStoreTest_LowStock()503 {504 final BaseStoreModel baseStore = null;505506 final ProductModel product = null;507508 // create partial mock to mock the internal method calls509 final IMockBuilder<DefaultSapOaaCommerceStockService> mockBuilder = EasyMock510 .createMockBuilder(DefaultSapOaaCommerceStockService.class);511 mockBuilder.addMockedMethod("getStockLevelStatusForProductAndBaseStore");512 classUnderTest = mockBuilder.createMock();513514 EasyMock.expect(classUnderTest.getStockLevelStatusForProductAndBaseStore(product, baseStore))515 .andReturn(StockLevelStatus.LOWSTOCK).anyTimes();516517518519520 final Collection<StockLevelModel> stockCollection = createStockCollection("Y", 5);521 final StockService stockServiceMock = createStockServiceMock(stockCollection);522 final WarehouseSelectionStrategy warehouseSelectionStrategyMock = createWarehouseSelectionStrategyMock(baseStore);523524 classUnderTest.setWarehouseSelectionStrategy(warehouseSelectionStrategyMock);525 classUnderTest.setStockService(stockServiceMock);526527 EasyMock.replay(classUnderTest);528529 Assert.assertEquals(classUnderTest.getStockLevelForProductAndBaseStore(product, baseStore).longValue(), 5);530531 }532533534 @Test535 @SuppressWarnings("PMD.MethodNamingConventions")536 public void getStockLevelForProductAndBaseStoreTest_NoStockLevel()537 {538539540 final BaseStoreModel baseStore = null;541542 final ProductModel product = null;543544 // create partial mock to mock the internal method calls545 final IMockBuilder<DefaultSapOaaCommerceStockService> mockBuilder = EasyMock546 .createMockBuilder(DefaultSapOaaCommerceStockService.class);547 mockBuilder.addMockedMethod("getStockLevelStatusForProductAndBaseStore");548 classUnderTest = mockBuilder.createMock();549550 EasyMock.expect(classUnderTest.getStockLevelStatusForProductAndBaseStore(product, baseStore))551 .andReturn(StockLevelStatus.LOWSTOCK).anyTimes();552553554555556 final Collection<StockLevelModel> stockCollection = createEmptyStockCollection();557 final StockService stockServiceMock = createStockServiceMock(stockCollection);558 final WarehouseSelectionStrategy warehouseSelectionStrategyMock = createWarehouseSelectionStrategyMock(baseStore);559560 classUnderTest.setWarehouseSelectionStrategy(warehouseSelectionStrategyMock);561 classUnderTest.setStockService(stockServiceMock);562563 EasyMock.replay(classUnderTest);564565 Assert.assertEquals(classUnderTest.getStockLevelForProductAndBaseStore(product, baseStore).longValue(), 0);566567 }568569570 @Test571 @SuppressWarnings("PMD.MethodNamingConventions")572 public void getPosAndStockLevelStatusForProductTest_InStock()573 {574575 final ProductModel product = null;576577578579 final PointOfServiceModel pointOfService = createStore();580 final List<PointOfServiceModel> pointsOfService = new ArrayList<PointOfServiceModel>();581 pointsOfService.add(pointOfService);582583584 final BaseStoreModel baseStoreMock = createBaseStoreMock(pointsOfService);585586587 final PointOfServiceDao pointOfServiceDaoMock = createPointOfServiceDaoMock(pointOfService);588 classUnderTest.setPointOfServiceDao(pointOfServiceDaoMock);589 Assert.assertNotNull(classUnderTest.getPointOfServiceDao());590591 //Mock ATPStrategy592 final ATPAggregationStrategy atpStrategyMock = EasyMock.createNiceMock(ATPAggregationStrategy.class);593 EasyMock594 .expect(595 atpStrategyMock.aggregateAvailability(EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class),596 EasyMock.anyObject(PointOfServiceModel.class), EasyMock.anyObject(List.class)))597 .andReturn(Long.valueOf(50)).anyTimes();598 EasyMock.replay(atpStrategyMock);599 classUnderTest.setAtpAggregationStrategy(atpStrategyMock);600601602 final List<ATPProductAvailability> atpProductAvailabilityList = createAtpProductAvailabilityList(new Double(50));603604 final ATPService atpServiceMock = createAtpServiceMock(atpProductAvailabilityList);605 classUnderTest.setAtpService(atpServiceMock);606607 final Map<PointOfServiceModel, StockLevelStatus> posAndStockLevelStatusForProduct = classUnderTest608 .getPosAndStockLevelStatusForProduct(product, baseStoreMock);609 Assert.assertNotNull(posAndStockLevelStatusForProduct);610 Assert.assertEquals(posAndStockLevelStatusForProduct.get(pointOfService), StockLevelStatus.INSTOCK);611612 }613614 @Test615 @SuppressWarnings("PMD.MethodNamingConventions")616 public void getPosAndStockLevelStatusForProductTest_OutOfStock()617 {618619 final ProductModel product = null;620621 final PointOfServiceModel pointOfService = createStore();622 final List<PointOfServiceModel> pointsOfService = new ArrayList<PointOfServiceModel>();623 pointsOfService.add(pointOfService);624625626 final BaseStoreModel baseStoreMock = createBaseStoreMock(pointsOfService);627628629 final PointOfServiceDao pointOfServiceDaoMock = createPointOfServiceDaoMock(pointOfService);630 classUnderTest.setPointOfServiceDao(pointOfServiceDaoMock);631632633 final List<ATPProductAvailability> atpProductAvailabilityList = createAtpProductAvailabilityList(new Double(0));634635 final ATPService atpServiceMock = createAtpServiceMock(atpProductAvailabilityList);636 classUnderTest.setAtpService(atpServiceMock);637638 //Mock ATPStrategy639 final ATPAggregationStrategy atpStrategyMock = EasyMock.createNiceMock(ATPAggregationStrategy.class);640 EasyMock641 .expect(642 atpStrategyMock.aggregateAvailability(EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class),643 EasyMock.anyObject(PointOfServiceModel.class), EasyMock.anyObject(List.class)))644 .andReturn(Long.valueOf(0)).anyTimes();645 EasyMock.replay(atpStrategyMock);646 classUnderTest.setAtpAggregationStrategy(atpStrategyMock);647648 final Map<PointOfServiceModel, StockLevelStatus> posAndStockLevelStatusForProduct = classUnderTest649 .getPosAndStockLevelStatusForProduct(product, baseStoreMock);650 Assert.assertNotNull(posAndStockLevelStatusForProduct);651 Assert.assertEquals(posAndStockLevelStatusForProduct.get(pointOfService), StockLevelStatus.OUTOFSTOCK);652653 }654655 @Test656 @SuppressWarnings("PMD.MethodNamingConventions")657 public void getPosAndStockLevelStatusForProductTest_ATPException()658 {659660 final ProductModel product = null;661662 final PointOfServiceModel pointOfService = createStore();663 final List<PointOfServiceModel> pointsOfService = new ArrayList<PointOfServiceModel>();664 pointsOfService.add(pointOfService);665666667 final BaseStoreModel baseStoreMock = createBaseStoreMock(pointsOfService);668669670 final PointOfServiceDao pointOfServiceDaoMock = createPointOfServiceDaoMock(pointOfService);671 classUnderTest.setPointOfServiceDao(pointOfServiceDaoMock);672673 final List<ATPProductAvailability> atpProductAvailabilityList = createAtpProductAvailabilityList(new Double(0));674675 final ATPService atpServiceMock = createAtpServiceMockException(atpProductAvailabilityList);676 classUnderTest.setAtpService(atpServiceMock);677678679 final Map<PointOfServiceModel, StockLevelStatus> posAndStockLevelStatusForProduct = classUnderTest680 .getPosAndStockLevelStatusForProduct(product, baseStoreMock);681 Assert.assertNotNull(posAndStockLevelStatusForProduct);682 Assert.assertTrue(posAndStockLevelStatusForProduct.isEmpty());683684 }685686687688 /**689 * @param atpProductAvailabilityList690 * @return atpServiceMock691 */692 private ATPService createAtpServiceMockException(final List<ATPProductAvailability> atpProductAvailabilityList)693 {694 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);695 EasyMock.expect(696 atpServiceMock.callRestAvailabilityServiceForProductAndSources(EasyMock.anyObject(String.class),697 EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class), EasyMock.anyObject(List.class)))698 .andThrow(new ATPException());699 EasyMock.replay(atpServiceMock);700 return atpServiceMock;701 }702703 /**704 * @param atpProductAvailabilityList705 * @return atpServiceMock706 */707 private ATPService createAtpServiceMock(final List<ATPProductAvailability> atpProductAvailabilityList)708 {709 final ATPService atpServiceMock = EasyMock.createNiceMock(ATPService.class);710 EasyMock.expect(711 atpServiceMock.callRestAvailabilityServiceForProductAndSources(EasyMock.anyObject(String.class),712 EasyMock.anyObject(String.class), EasyMock.anyObject(ProductModel.class), EasyMock.anyObject(List.class)))713 .andReturn(atpProductAvailabilityList);714 EasyMock.replay(atpServiceMock);715 return atpServiceMock;716 }717718 /**719 * @param quantity720 * @return atpProductAvailabilityList721 */722 private List<ATPProductAvailability> createAtpProductAvailabilityList(final Double quantity)723 {724 final List<ATPProductAvailability> atpProductAvailabilityList = new ArrayList<ATPProductAvailability>();725 final ATPProductAvailability atpProductAvailability = new ATPProductAvailability();726 atpProductAvailability.setArticleId("Article1");727 atpProductAvailability.setSourceId("OAS1");728 final List<ATPAvailability> availabilityList = new ArrayList<>();729 final ATPAvailability availability = new ATPAvailability();730 availability.setQuantity(quantity);731 availabilityList.add(availability);732 atpProductAvailability.setAvailabilityList(availabilityList);733 atpProductAvailabilityList.add(atpProductAvailability);734 return atpProductAvailabilityList;735 }736737 /**738 * @param pointOfService739 * @return pointOfServiceDaoMock740 */741 private PointOfServiceDao createPointOfServiceDaoMock(final PointOfServiceModel pointOfService)742 {743 final PointOfServiceDao pointOfServiceDaoMock = EasyMock.createNiceMock(PointOfServiceDao.class);744 EasyMock.expect(pointOfServiceDaoMock.getPosByName(EasyMock.anyObject(String.class))).andReturn(pointOfService);745 EasyMock.replay(pointOfServiceDaoMock);746 return pointOfServiceDaoMock;747 }748749 /**750 * @param pointsOfService751 * @return baseStoreMock752 */753 private BaseStoreModel createBaseStoreMock(final List<PointOfServiceModel> pointsOfService)754 {755 final BaseStoreModel baseStoreMock = EasyMock.createNiceMock(BaseStoreModel.class);756 EasyMock.expect(baseStoreMock.getPointsOfService()).andReturn(pointsOfService).anyTimes();757 EasyMock.replay(baseStoreMock);758 return baseStoreMock;759 }760761 /**762 * @return store763 */764 private PointOfServiceModel createStore()765 {766 final PointOfServiceModel pointOfService = new PointOfServiceModel();767 pointOfService.setName(STORE_1);768 pointOfService.setType(PointOfServiceTypeEnum.STORE);769 return pointOfService;770 }771772 private List<ATPAvailability> getDefaultATPAvailabilityList()773 {774 final List<ATPAvailability> availabilityList = new ArrayList<>();775 availabilityList.add(getDefaultATPAvailability_1());776 return availabilityList;777 }778779 @SuppressWarnings("PMD.MethodNamingConventions")780 private ATPAvailability getDefaultATPAvailability_1()781 {782 final ATPAvailability availModel = new ATPAvailability();783 availModel.setAtpDate(ATP_DATE);784 availModel.setQuantity(ATP_QTY_1);785 return availModel;786 }787788 /**789 * @param roughStockStatus790 * @param availableQuantity791 * @return stockCollection792 */793 private Collection<StockLevelModel> createStockCollection(final String roughStockStatus, final int availableQuantity)794 {795 final Collection<StockLevelModel> stockCollection = new ArrayList<StockLevelModel>();796 final StockLevelModel stockLevel = new StockLevelModel();797 stockLevel.setAvailable(availableQuantity);798 stockLevel.setSapoaa_roughStockIndicator(roughStockStatus);799 stockCollection.add(stockLevel);800 return stockCollection;801 }802803804 /**805 * @return stockCollection806 */807 private Collection<StockLevelModel> createEmptyStockCollection()808 {809 final Collection<StockLevelModel> stockCollection = new ArrayList<StockLevelModel>();810 return stockCollection;811 }812813814 /**815 * @param baseStore816 * @return warehouseSelectionStrategyMock817 */818 private WarehouseSelectionStrategy createWarehouseSelectionStrategyMock(final BaseStoreModel baseStore)819 {820 final WarehouseSelectionStrategy warehouseSelectionStrategyMock = EasyMock.createNiceMock(WarehouseSelectionStrategy.class);821 EasyMock.expect(warehouseSelectionStrategyMock.getWarehousesForBaseStore(baseStore)).andReturn(822 new ArrayList<WarehouseModel>());823 EasyMock.replay(warehouseSelectionStrategyMock);824 return warehouseSelectionStrategyMock;825 }826827 /**828 * @param stockCollection829 * @return stockServiceMock830 */831 private StockService createStockServiceMock(final Collection<StockLevelModel> stockCollection)832 {833 final StockService stockServiceMock = EasyMock.createNiceMock(StockService.class);834 EasyMock835 .expect(stockServiceMock.getStockLevels(EasyMock.anyObject(ProductModel.class), EasyMock.anyObject(Collection.class)))836 .andReturn(stockCollection).anyTimes();837 EasyMock.replay(stockServiceMock);838 return stockServiceMock;839 }840}
...
Source:MockBuilderTest.java
...220 }221 }222 @Test223 public void testCreateNiceMock() {224 mock = builder.addMockedMethod("size").addMockedMethod("toString").createNiceMock();225 replay(mock);226 assertEquals(0, mock.size());227 verify(mock);228 }229 @Test230 public void testCreateStrictMock() {231 mock = builder.addMockedMethod("size").addMockedMethod("clear").addMockedMethod("toString")232 .createStrictMock();233 expect(mock.size()).andReturn(1);234 mock.clear();235 replay(mock);236 try {237 mock.clear();238 fail("Unexpected call");239 } catch (final AssertionError e) {240 }241 }242 @Test243 public void testCreateMockStringIMocksControl() {244 final IMocksControl ctrl = createControl();245 mock = builder.addMockedMethod("toString").createMock("myName", ctrl);246 assertSame(MocksControl.getControl(mock), ctrl);247 assertTrue(mock.toString().contains("myName"));248 }249 @Test250 public void testCreateMockString() {251 mock = builder.addMockedMethod("size").addMockedMethod("toString").createMock("myName");252 replay(mock);253 try {254 mock.size();255 fail("Unexpected call");256 } catch (final AssertionError e) {257 assertTrue(e.getMessage().contains("myName"));258 }259 }260 @Test261 public void testCreateNiceMockString() {262 mock = builder.addMockedMethod("size").addMockedMethod("toString").createNiceMock("myName");263 replay(mock);264 assertEquals(0, mock.size());265 verify(mock);266 assertTrue(mock.toString().contains("myName"));267 }268 @Test269 public void testCreateStrictMockString() throws Throwable {270 mock = builder.addMockedMethod("size").addMockedMethod("clear").addMockedMethod("toString")271 .createStrictMock("myName");272 expect(mock.size()).andReturn(1);273 mock.clear();274 replay(mock);275 try {276 mock.clear();...
Source:MockBuilder.java
...187 public T createMock() {188 return createMock((String) null);189 }190191 public T createNiceMock() {192 return createNiceMock(null);193 }194195 public T createStrictMock() {196 return createStrictMock(null);197 }198199 @SuppressWarnings("deprecation")200 public T createMock(final String name, final IMocksControl control) {201 final Method[] mockedMethodArray = (mockedMethods == null ? new Method[0] : mockedMethods202 .toArray(new Method[mockedMethods.size()]));203204 // Create a mock with no default {@code withConstructor} was not called.205 if (constructor == null) {206 return control.createMock(name, toMock, mockedMethodArray);207 }208209 // If the constructor is defined, so must be its arguments210 if (constructorArgs == null) {211 throw new IllegalStateException("Picked a constructor but didn't pass arguments to it");212 }213214 return control.createMock(name, toMock, constructorArgs, mockedMethodArray);215 }216217 public T createMock(final String name) {218 final IMocksControl control = (support == null ? EasyMock.createControl() : support.createControl());219 return createMock(name, control);220 }221222 public T createNiceMock(final String name) {223 final IMocksControl control = (support == null ? EasyMock.createNiceControl() : support224 .createNiceControl());225 return createMock(name, control);226 }227228 public T createStrictMock(final String name) {229 final IMocksControl control = (support == null ? EasyMock.createStrictControl() : support230 .createStrictControl());231 return createMock(name, control);232 }233234 private void checkConstructorNotInitialized() {235 if (constructor != null) {236 throw new IllegalStateException("Trying to define the constructor call more than once.");
...
createNiceMock
Using AI Code Generation
1import static org.easymock.EasyMock.createNiceMock;2import org.easymock.internal.MockBuilder;3public class 1 {4 public static void main(String[] args) {5 MockBuilder mockBuilder = new MockBuilder();6 mockBuilder.createNiceMock();7 }8}9Exception in thread "main" java.lang.IllegalAccessError: class org.easymock.internal.MockBuilder (in unnamed module @0x5c0f5a) cannot access class org.easymock.internal.MocksControl (in module org.easymock) because module org.easymock does not export org.easymock.internal to unnamed module @0x5c0f5a
createNiceMock
Using AI Code Generation
1import org.easymock.EasyMock;2import org.easymock.internal.MockBuilder;3import org.easymock.internal.MocksControl;4public class TestClass {5 public static void main(String[] args) {6 MocksControl control = new MocksControl();7 MockBuilder mockBuilder = new MockBuilder(control);8 Object mock = mockBuilder.createNiceMock(Object.class);9 System.out.println(mock.getClass());10 }11}12import org.easymock.EasyMock;13import org.easymock.internal.MocksControl;14public class TestClass {15 public static void main(String[] args) {16 MocksControl control = new MocksControl();17 Object mock = EasyMock.createNiceMock(control, Object.class);18 System.out.println(mock.getClass());19 }20}
createNiceMock
Using AI Code Generation
1import org.easymock.EasyMock;2import org.easymock.IAnswer;3import org.easymock.IMocksControl;4import org.easymock.internal.MockBuilder;5import org.easymock.internal.MocksControl;6public class MockBuilderTest {7 public static void main(String[] args) {8 IMocksControl control = new MocksControl();9 MockBuilder builder = new MockBuilder();10 builder.setControl(control);11 builder.setMockType(Comparable.class);12 builder.setMockName("mock");13 builder.setMethodsToMock(new String[] { "compareTo" });14 builder.setAnswer(new IAnswer<Object>() {15 public Object answer() throws Throwable {16 return 0;17 }18 });19 Comparable<?> mock = (Comparable<?>) builder.createNiceMock();20 EasyMock.expect(mock.compareTo("a")).andReturn(1);21 EasyMock.expect(mock.compareTo("b")).andReturn(2);22 control.replay();23 System.out.println(mock.compareTo("a"));24 System.out.println(mock.compareTo("b"));25 control.verify();26 }27}
createNiceMock
Using AI Code Generation
1import org.easymock.EasyMock;2import org.easymock.EasyMockSupport;3import org.easymock.internal.MockBuilder;4public class MockBuilderDemo {5 public static void main(String[] args) {6 MockBuilder builder = new MockBuilder();7 builder.setMockType(EasyMockSupport.NICE);8 builder.setMockedType(Interface.class);9 Interface mock = (Interface) builder.createMock();10 System.out.println("Nice mock object created");11 }12}
createNiceMock
Using AI Code Generation
1package org.easymock.internal;2import org.easymock.IExpectationSetters;3import org.easymock.internal.MockBuilder;4{5 public static void main(String[] args)6 {7 MockBuilder mockBuilder = new MockBuilder();8 IExpectationSetters<String> iExpectationSetters = mockBuilder.createNiceMock(String.class);9 }10}
createNiceMock
Using AI Code Generation
1import java.util.List;2import org.easymock.internal.MockBuilder;3public class 1 {4 public static void main(String[] args) {5 MockBuilder builder = new MockBuilder(List.class);6 List mock = (List) builder.createNiceMock();7 System.out.println("Nice Mock of the List interface created.");8 }9}
createNiceMock
Using AI Code Generation
1package org.easymock;2import org.easymock.internal.MockBuilder;3import org.junit.Test;4import static org.easymock.EasyMock.*;5public class ExampleTest {6 public interface Foo {7 void doSomething();8 }9 public void test() {10 Foo mock = MockBuilder.createNiceMock(Foo.class);11 mock.doSomething();12 replay(mock);13 verify(mock);14 }15}16package org.easymock;17import org.junit.Test;18import static org.easymock.EasyMock.*;19public class ExampleTest {20 public interface Foo {21 void doSomething();22 }23 public void test() {24 Foo mock = createNiceMock(Foo.class);25 mock.doSomething();26 replay(mock);27 verify(mock);28 }29}30package org.easymock;31import org.junit.Test;32import static org.easymock.EasyMock.*;33public class ExampleTest {34 public interface Foo {35 void doSomething();36 }37 public void test() {38 Foo mock = createNiceMock(Foo.class);39 mock.doSomething();40 replay(mock);41 verify(mock);42 }43}44package org.easymock;45import org.junit.Test;46import static org.easymock.EasyMock.*;47public class ExampleTest {48 public interface Foo {49 void doSomething();50 }51 public void test() {52 Foo mock = createNiceMock(Foo.class);53 mock.doSomething();54 replay(mock);55 verify(mock);56 }57}58package org.easymock;59import org.junit.Test;60import static org.easymock.EasyMock.*;61public class ExampleTest {62 public interface Foo {63 void doSomething();64 }65 public void test() {66 Foo mock = createNiceMock(Foo.class);67 mock.doSomething();68 replay(mock);69 verify(mock);
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!