Best Mockito code snippet using org.mockito.ArgumentMatchers.anyInt
Source:TestBHttpConnectionBase.java
...97 Assert.assertTrue(conn.isOpen());98 conn.close();99 Assert.assertFalse(conn.isOpen());100 Mockito.verify(outStream, Mockito.times(1)).write(101 ArgumentMatchers.<byte[]>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());102 Mockito.verify(socket, Mockito.times(1)).shutdownInput();103 Mockito.verify(socket, Mockito.times(1)).shutdownOutput();104 Mockito.verify(socket, Mockito.times(1)).close();105 conn.close();106 Mockito.verify(socket, Mockito.times(1)).close();107 Mockito.verify(outStream, Mockito.times(1)).write(108 ArgumentMatchers.<byte[]>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());109 }110 @Test111 public void testConnectionShutdown() throws Exception {112 final InputStream inStream = Mockito.mock(InputStream.class);113 final OutputStream outStream = Mockito.mock(OutputStream.class);114 Mockito.when(socket.getInputStream()).thenReturn(inStream);115 Mockito.when(socket.getOutputStream()).thenReturn(outStream);116 conn.bind(socket);117 conn.ensureOpen();118 conn.outbuffer.write(0, outStream);119 Assert.assertTrue(conn.isOpen());120 conn.close(CloseMode.GRACEFUL);121 Assert.assertFalse(conn.isOpen());122 Mockito.verify(outStream, Mockito.never()).write(123 ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());124 Mockito.verify(socket, Mockito.never()).shutdownInput();125 Mockito.verify(socket, Mockito.never()).shutdownOutput();126 Mockito.verify(socket, Mockito.times(1)).close();127 conn.close();128 Mockito.verify(socket, Mockito.times(1)).close();129 conn.close(CloseMode.GRACEFUL);130 Mockito.verify(socket, Mockito.times(1)).close();131 }132 @Test133 public void testCreateEntityLengthDelimited() throws Exception {134 final InputStream inStream = Mockito.mock(InputStream.class);135 final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK");136 message.addHeader("Content-Length", "10");137 message.addHeader("Content-Type", "stuff");138 message.addHeader("Content-Encoding", "chunked");139 final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, 10);140 Assert.assertNotNull(entity);141 Assert.assertFalse(entity.isChunked());142 Assert.assertEquals(10, entity.getContentLength());143 Assert.assertEquals("stuff", entity.getContentType());144 Assert.assertEquals("chunked", entity.getContentEncoding());145 final InputStream content = entity.getContent();146 Assert.assertNotNull(content);147 Assert.assertTrue((content instanceof ContentLengthInputStream));148 }149 @Test150 public void testCreateEntityInputChunked() throws Exception {151 final InputStream inStream = Mockito.mock(InputStream.class);152 final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK");153 final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, ContentLengthStrategy.CHUNKED);154 Assert.assertNotNull(entity);155 Assert.assertTrue(entity.isChunked());156 Assert.assertEquals(-1, entity.getContentLength());157 final InputStream content = entity.getContent();158 Assert.assertNotNull(content);159 Assert.assertTrue((content instanceof ChunkedInputStream));160 }161 @Test162 public void testCreateEntityInputUndefined() throws Exception {163 final InputStream inStream = Mockito.mock(InputStream.class);164 final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK");165 final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, ContentLengthStrategy.UNDEFINED);166 Assert.assertNotNull(entity);167 Assert.assertFalse(entity.isChunked());168 Assert.assertEquals(-1, entity.getContentLength());169 final InputStream content = entity.getContent();170 Assert.assertNotNull(content);171 Assert.assertTrue((content instanceof IdentityInputStream));172 }173 @Test174 public void testSetSocketTimeout() throws Exception {175 conn.bind(socket);176 conn.setSocketTimeout(Timeout.ofMilliseconds(123));177 Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123);178 }179 @Test180 public void testSetSocketTimeoutException() throws Exception {181 conn.bind(socket);182 Mockito.doThrow(new SocketException()).when(socket).setSoTimeout(ArgumentMatchers.anyInt());183 conn.setSocketTimeout(Timeout.ofMilliseconds(123));184 Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123);185 }186 @Test187 public void testGetSocketTimeout() throws Exception {188 Assert.assertEquals(Timeout.DISABLED, conn.getSocketTimeout());189 Mockito.when(socket.getSoTimeout()).thenReturn(345);190 conn.bind(socket);191 Assert.assertEquals(Timeout.ofMilliseconds(345), conn.getSocketTimeout());192 }193 @Test194 public void testGetSocketTimeoutException() throws Exception {195 Assert.assertEquals(Timeout.DISABLED, conn.getSocketTimeout());196 Mockito.when(socket.getSoTimeout()).thenThrow(new SocketException());197 conn.bind(socket);198 Assert.assertEquals(Timeout.DISABLED, conn.getSocketTimeout());199 }200 @Test201 public void testAwaitInputInBuffer() throws Exception {202 final ByteArrayInputStream inStream = Mockito.spy(new ByteArrayInputStream(203 new byte[] {1, 2, 3, 4, 5}));204 Mockito.when(socket.getInputStream()).thenReturn(inStream);205 conn.bind(socket);206 conn.ensureOpen();207 conn.inBuffer.read(inStream);208 Assert.assertTrue(conn.awaitInput(Timeout.ofMilliseconds(432)));209 Mockito.verify(socket, Mockito.never()).setSoTimeout(ArgumentMatchers.anyInt());210 Mockito.verify(inStream, Mockito.times(1)).read(211 ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());212 }213 @Test214 public void testAwaitInputInSocket() throws Exception {215 final ByteArrayInputStream inStream = Mockito.spy(new ByteArrayInputStream(216 new byte[] {1, 2, 3, 4, 5}));217 Mockito.when(socket.getInputStream()).thenReturn(inStream);218 Mockito.when(socket.getSoTimeout()).thenReturn(345);219 conn.bind(socket);220 conn.ensureOpen();221 Assert.assertTrue(conn.awaitInput(Timeout.ofMilliseconds(432)));222 Mockito.verify(socket, Mockito.times(1)).setSoTimeout(432);223 Mockito.verify(socket, Mockito.times(1)).setSoTimeout(345);224 Mockito.verify(inStream, Mockito.times(1)).read(225 ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());226 }227 @Test228 public void testAwaitInputNoData() throws Exception {229 final InputStream inStream = Mockito.mock(InputStream.class);230 Mockito.when(socket.getInputStream()).thenReturn(inStream);231 Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()))232 .thenReturn(-1);233 conn.bind(socket);234 conn.ensureOpen();235 Assert.assertFalse(conn.awaitInput(Timeout.ofMilliseconds(432)));236 }237 @Test238 public void testStaleWhenClosed() throws Exception {239 final OutputStream outStream = Mockito.mock(OutputStream.class);240 Mockito.when(socket.getOutputStream()).thenReturn(outStream);241 conn.bind(socket);242 conn.ensureOpen();243 conn.close();244 Assert.assertTrue(conn.isStale());245 }246 @Test247 public void testNotStaleWhenHasData() throws Exception {248 final ByteArrayInputStream inStream = Mockito.spy(new ByteArrayInputStream(249 new byte[] {1, 2, 3, 4, 5}));250 Mockito.when(socket.getInputStream()).thenReturn(inStream);251 conn.bind(socket);252 conn.ensureOpen();253 Assert.assertFalse(conn.isStale());254 }255 @Test256 public void testStaleWhenEndOfStream() throws Exception {257 final InputStream inStream = Mockito.mock(InputStream.class);258 Mockito.when(socket.getInputStream()).thenReturn(inStream);259 Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()))260 .thenReturn(-1);261 conn.bind(socket);262 conn.ensureOpen();263 Assert.assertTrue(conn.isStale());264 }265 @Test266 public void testNotStaleWhenTimeout() throws Exception {267 final InputStream inStream = Mockito.mock(InputStream.class);268 Mockito.when(socket.getInputStream()).thenReturn(inStream);269 Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()))270 .thenThrow(new SocketTimeoutException());271 conn.bind(socket);272 conn.ensureOpen();273 Assert.assertFalse(conn.isStale());274 }275 @Test276 public void testStaleWhenIOError() throws Exception {277 final InputStream inStream = Mockito.mock(InputStream.class);278 Mockito.when(socket.getInputStream()).thenReturn(inStream);279 Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()))280 .thenThrow(new SocketException());281 conn.bind(socket);282 conn.ensureOpen();283 Assert.assertTrue(conn.isStale());284 }285}...
Source:CarServiceImplTest.java
...54 .carStatus(CarStatus.AVAILABLE)55 .model("X7")56 .price(new BigDecimal("177"))57 .build();58 Mockito.doReturn(catalog).when(catalogDao).getById(ArgumentMatchers.anyInt());59 Mockito.doReturn(firm).when(firmDao).getById(ArgumentMatchers.anyInt());60 carService.addCar(car);61 Mockito.verify(carDao).save(car);62 }63 @Test64 void CarServiceImpl_addCar_whenCatalogNotFoundToThrowBusinessException() {65 Catalog catalog = Catalog.builder()66 .id(1)67 .name("Car")68 .build();69 Firm firm = Firm.builder()70 .id(1)71 .name("BMW")72 .build();73 Car car = Car.builder()74 .id(1)75 .catalog(catalog)76 .firm(firm)77 .carStatus(CarStatus.AVAILABLE)78 .model("X7")79 .price(new BigDecimal("177"))80 .build();81 Mockito.doReturn(null).when(catalogDao).getById(ArgumentMatchers.anyInt());82 Assertions.assertThrows(BusinessException.class,83 () -> carService.addCar(car));84 }85 @Test()86 void CarServiceImpl_removeCar() {87 List<Order> orderList = new ArrayList<>();88 List<Request> requestList = new ArrayList<>();89 Integer carId = 1;90 Mockito.doReturn(requestList).when(requestDao).getListByCarId(ArgumentMatchers.anyInt());91 Mockito.doReturn(orderList).when(orderDao).getListByCarId(ArgumentMatchers.anyInt());92 Mockito.doNothing().when(carDao).delete(ArgumentMatchers.any());93 carService.removeCar(carId);94 Mockito.verify(carDao).delete(carId);95 }96 @Test()97 void CarServiceImpl_removeCar_whenCarHasRequestsToThrowBusinessException() {98 List<Order> orderList = new ArrayList<>();99 orderList.add(Order.builder().build());100 Integer carId = 1;101 Mockito.doReturn(orderList).when(orderDao).getListByCarId(ArgumentMatchers.anyInt());102 Assertions.assertThrows(BusinessException.class,103 () -> carService.removeCar(carId));104 }105 @Test()106 void CarServiceImpl_removeCar_whenCarHasOrdersToThrowBusinessException() {107 List<Request> requestList = new ArrayList<>();108 requestList.add(Request.builder().build());109 Integer carId = 1;110 Mockito.doReturn(requestList).when(requestDao).getListByCarId(ArgumentMatchers.anyInt());111 Assertions.assertThrows(BusinessException.class,112 () -> carService.removeCar(carId));113 }114 @Test115 void CarServiceImpl_updateCar() {116 Catalog catalog = Catalog.builder()117 .id(1)118 .name("Car")119 .build();120 Firm firm = Firm.builder()121 .id(1)122 .name("BMW")123 .build();124 Car car = Car.builder()125 .id(1)126 .catalog(catalog)127 .firm(firm)128 .carStatus(CarStatus.AVAILABLE)129 .model("X7")130 .price(new BigDecimal("177"))131 .build();132 Mockito.doReturn(car).when(carDao).getById(ArgumentMatchers.anyInt());133 Mockito.doReturn(catalog).when(catalogDao).getById(ArgumentMatchers.anyInt());134 Mockito.doReturn(firm).when(firmDao).getById(ArgumentMatchers.anyInt());135 carService.updateCar(car);136 Mockito.verify(carDao).update(car);137 }138 @Test139 void CarServiceImpl_updateCar_whenCarNotFoundToThrowBusinessException() {140 Catalog catalog = Catalog.builder()141 .id(1)142 .name("Car")143 .build();144 Firm firm = Firm.builder()145 .id(1)146 .name("BMW")147 .build();148 Car car = Car.builder()149 .id(1)150 .catalog(catalog)151 .firm(firm)152 .carStatus(CarStatus.AVAILABLE)153 .model("X7")154 .price(new BigDecimal("177"))155 .build();156 Mockito.doReturn(null).when(carDao).getById(ArgumentMatchers.anyInt());157 Assertions.assertThrows(BusinessException.class,158 () -> carService.updateCar(car));159 }160 @Test161 void CarServiceImpl_updateCar_whenCatalogNotFoundToThrowBusinessException() {162 Firm firm = Firm.builder()163 .id(1)164 .name("BMW")165 .build();166 Car car = Car.builder()167 .id(1)168 .catalog(Catalog.builder().id(1).name("Car").build())169 .firm(firm)170 .carStatus(CarStatus.AVAILABLE)171 .model("X7")172 .price(new BigDecimal("177"))173 .build();174 Mockito.doReturn(car).when(carDao).getById(ArgumentMatchers.anyInt());175 Mockito.doReturn(null).when(catalogDao).getById(ArgumentMatchers.anyInt());176 Assertions.assertThrows(BusinessException.class,177 () -> carService.updateCar(car));178 }179 @Test180 void CarServiceImpl_updateCar_whenFirmNotFoundToThrowBusinessException() {181 Catalog catalog = Catalog.builder()182 .id(1)183 .name("Car")184 .build();185 Car car = Car.builder()186 .id(1)187 .catalog(catalog)188 .firm( Firm.builder().id(1).name("BMW").build())189 .carStatus(CarStatus.AVAILABLE)190 .model("X7")191 .price(new BigDecimal("177"))192 .build();193 Mockito.doReturn(car).when(carDao).getById(ArgumentMatchers.anyInt());194 Mockito.doReturn(catalog).when(catalogDao).getById(ArgumentMatchers.anyInt());195 Mockito.doReturn(null).when(firmDao).getById(ArgumentMatchers.anyInt());196 Assertions.assertThrows(BusinessException.class,197 () -> carService.updateCar(car));198 }199 @Test200 void CarServiceImpl_changeCarStatus() {201 CarStatus carStatus = CarStatus.AVAILABLE;202 Integer carId = 1;203 Car car = Car.builder()204 .id(1)205 .carStatus(CarStatus.AVAILABLE)206 .model("X7")207 .price(new BigDecimal("177"))208 .build();209 Mockito.doReturn(car).when(carDao).getById(ArgumentMatchers.anyInt());210 carService.changeCarStatus(carId, carStatus);211 Mockito.verify(carDao).update(car);212 Mockito.verify(requestDao).deleteByCarId(car.getId());213 }214 @Test215 void CarServiceImpl_changeCarStatus_whenCarNotFoundToThrowBusinessException() {216 Integer carId = 1;217 Mockito.doReturn(null).when(carDao).getById(ArgumentMatchers.anyInt());218 Assertions.assertThrows(BusinessException.class,219 () -> carService.changeCarStatus(carId, CarStatus.AVAILABLE));220 }221 @Test222 void CarServiceImpl_changeCarStatus_whenStatusNotSetToThrowBusinessException() {223 Integer carId = 1;224 Assertions.assertThrows(BusinessException.class,225 () -> carService.changeCarStatus(carId, null));226 }227 @Test228 void CarServiceImpl_getCar() {229 Integer carId = 1;230 Car car = Car.builder()231 .id(1)232 .carStatus(CarStatus.AVAILABLE)233 .model("X8")234 .price(new BigDecimal("177"))235 .build();236 Mockito.doReturn(car).when(carDao).getById(ArgumentMatchers.anyInt());237 carService.getCar(carId);238 Mockito.verify(carDao).getById(ArgumentMatchers.anyInt());239 }240 @Test241 void CarServiceImpl_getCarList() {242 List<Car> carList = new ArrayList<>();243 carList.add(Car.builder()244 .id(1)245 .carStatus(CarStatus.AVAILABLE)246 .model("X8")247 .price(new BigDecimal("177"))248 .build());249 carList.add(Car.builder()250 .id(2)251 .carStatus(CarStatus.AVAILABLE)252 .model("X7")253 .price(new BigDecimal("137"))254 .build());255 carList.add(Car.builder()256 .id(3)257 .carStatus(CarStatus.AVAILABLE)258 .model("X9")259 .price(new BigDecimal("187"))260 .build());261 Mockito.doReturn(carList).when(carDao).getAll();262 List<Car> cars = carService.getCarList();263 Assertions.assertNotNull(cars);264 for (Car car : cars) {265 Assertions.assertNotNull(car);266 }267 Mockito.verify(carDao).getAll();268 }269 @Test270 void CarServiceImpl_getSortedCarList() {271 List<Car> carList = new ArrayList<>();272 CarSorterType carSorterType = CarSorterType.SORT_CAR_BY_PRICE;273 carList.add(Car.builder()274 .id(1)275 .catalog(Catalog.builder().build())276 .carStatus(CarStatus.AVAILABLE)277 .model("X8")278 .price(new BigDecimal("198"))279 .build());280 carList.add(Car.builder()281 .id(2)282 .catalog(Catalog.builder().build())283 .carStatus(CarStatus.AVAILABLE)284 .model("X7")285 .price(new BigDecimal("144"))286 .build());287 carList.add(Car.builder()288 .id(3)289 .catalog(Catalog.builder().build())290 .carStatus(CarStatus.AVAILABLE)291 .model("X9")292 .price(new BigDecimal("123"))293 .build());294 Mockito.doReturn(carList).when(carDao).getListByOrderByPrice();295 List<Car> cars = carService.getSortedCarList(carSorterType);296 Assertions.assertNotNull(cars);297 for (Car car : cars) {298 Assertions.assertNotNull(car.getCatalog());299 }300 Mockito.verify(carDao).getListByOrderByPrice();301 }302 @Test303 void CarServiceImpl_getCountOrdersByCarId() {304 Integer count = 3;305 List<Order> orderList = new ArrayList<>();306 orderList.add(Order.builder().build());307 orderList.add(Order.builder().build());308 orderList.add(Order.builder().build());309 Mockito.doReturn(orderList).when(orderDao).getListByCarId(ArgumentMatchers.anyInt());310 Integer countOrders = carService.getCountOrdersByCarId(ArgumentMatchers.anyInt());311 Assertions.assertEquals(countOrders, count);312 }313 @Test314 void CarServiceImpl_getCountCars() {315 Integer count = 4;316 List<Car> carList = new ArrayList<>();317 carList.add(Car.builder().build());318 carList.add(Car.builder().build());319 carList.add(Car.builder().build());320 carList.add(Car.builder().build());321 Mockito.doReturn(carList).when(carDao).getAll();322 Integer countCars = carService.getCountCars();323 Assertions.assertEquals(countCars, count);324 }325 @Test326 void CarServiceImpl_getCountCarsByStatus() {327 Integer count = 4;328 CarStatus carStatus = CarStatus.AVAILABLE;329 List<Car> carList = new ArrayList<>();330 carList.add(Car.builder().build());331 carList.add(Car.builder().build());332 carList.add(Car.builder().build());333 carList.add(Car.builder().build());334 Mockito.doReturn(carList).when(carDao).getListByStatus(carStatus);335 Integer countCars = carService.getCountCarsByStatus(carStatus);336 Assertions.assertEquals(countCars, count);337 }338 @Test339 void CarServiceImpl_getOrdersByCarId() {340 List<Order> orderList = new ArrayList<>();341 orderList.add(Order.builder().build());342 orderList.add(Order.builder().build());343 orderList.add(Order.builder().build());344 Mockito.doReturn(orderList).when(orderDao).getListByCarId(ArgumentMatchers.anyInt());345 List<Order> orders = carService.getOrdersByCarId(ArgumentMatchers.anyInt());346 Assertions.assertNotNull(orders);347 for (Order order : orders) {348 Assertions.assertNotNull(order);349 }350 }351}...
Source:FirmServiceImplTest.java
...52 void FirmServiceImpl_removeFirm() {53 List<Car> carList = new ArrayList<>();54 List<ContactPerson> personList = new ArrayList<>();55 Integer firmId = 1;56 Mockito.doReturn(carList).when(carDao).getListByFirmId(ArgumentMatchers.anyInt());57 Mockito.doReturn(personList).when(contactPersonDao).getListByFirmId(ArgumentMatchers.anyInt());58 Mockito.doNothing().when(firmDao).delete(ArgumentMatchers.anyInt());59 firmService.removeFirm(firmId);60 Mockito.verify(firmDao).delete(firmId);61 }62 @Test63 void FirmServiceImpl_removeFirm_whenFirmHasCarsToThrowBusinessException() {64 List<Car> carList = new ArrayList<>();65 carList.add(Car.builder().build());66 Integer firmId = 1;67 Mockito.doReturn(carList).when(carDao).getListByFirmId(ArgumentMatchers.anyInt());68 Assertions.assertThrows(BusinessException.class,69 () -> firmService.removeFirm(firmId));70 }71 @Test72 void FirmServiceImpl_removeFirm_whenFirmHasContactPersonsToThrowBusinessException() {73 List<Car> carList = new ArrayList<>();74 List<ContactPerson> personList = new ArrayList<>();75 personList.add(ContactPerson.builder().build());76 Integer firmId = 1;77 Mockito.doReturn(carList).when(carDao).getListByFirmId(ArgumentMatchers.anyInt());78 Mockito.doReturn(personList).when(contactPersonDao).getListByFirmId(ArgumentMatchers.anyInt());79 Assertions.assertThrows(BusinessException.class,80 () -> firmService.removeFirm(firmId));81 }82 @Test83 void FirmServiceImpl_updateFirm() {84 Firm firm = Firm.builder()85 .id(1)86 .name("BMW")87 .build();88 Mockito.doReturn(firm).when(firmDao).getById(ArgumentMatchers.anyInt());89 Mockito.doNothing().when(firmDao).update(ArgumentMatchers.any());90 firmService.updateFirm(firm);91 Mockito.verify(firmDao).update(firm);92 }93 @Test94 void FirmServiceImpl_updateFirm_whenNoSuchFirmExistsToThrowBusinessException() {95 Firm firm = Firm.builder()96 .id(1)97 .name("BMW")98 .build();99 Mockito.doReturn(null).when(firmDao).getById(ArgumentMatchers.anyInt());100 Assertions.assertThrows(BusinessException.class,101 () -> firmService.updateFirm(firm));102 }103 @Test104 void FirmServiceImpl_updateFirm_whenFirmNameIsMissingToThrowBusinessException() {105 Firm firm = Firm.builder()106 .id(1)107 .build();108 Mockito.doReturn(firm).when(firmDao).getById(ArgumentMatchers.anyInt());109 Assertions.assertThrows(BusinessException.class,110 () -> firmService.updateFirm(firm));111 }112 @Test113 void FirmServiceImpl_getFirm() {114 Integer firmId = 1;115 Firm firm = Firm.builder()116 .id(1)117 .name("BMW")118 .build();119 Mockito.doReturn(firm).when(firmDao).getById(ArgumentMatchers.anyInt());120 firmService.getFirm(firmId);121 Mockito.verify(firmDao).getById(ArgumentMatchers.anyInt());122 }123 @Test124 void FirmServiceImpl_getFirmList() {125 List<Firm> firmList = new ArrayList<>();126 firmList.add(Firm.builder()127 .id(1)128 .name("BMW")129 .build());130 firmList.add(Firm.builder()131 .id(1)132 .name("Audi")133 .build());134 Mockito.doReturn(firmList).when(firmDao).getAll();135 List<Firm> firms = firmService.getFirmList();136 Assertions.assertNotNull(firms);137 for (Firm firm : firms) {138 Assertions.assertNotNull(firm);139 }140 Mockito.verify(firmDao).getAll();141 }142 @Test143 void FirmServiceImpl_getSortedFirmList() {144 FirmSorterType sorterType = FirmSorterType.SORT_FIRM_BY_NAME;145 List<Firm> firmList = new ArrayList<>();146 firmList.add(Firm.builder()147 .id(1)148 .name("Audi")149 .build());150 firmList.add(Firm.builder()151 .id(1)152 .name("BMW")153 .build());154 Mockito.doReturn(firmList).when(firmDao).getListByOrderByName();155 List<Firm> firms = firmService.getSortedFirmList(sorterType);156 Assertions.assertNotNull(firms);157 for (Firm firm : firms) {158 Assertions.assertNotNull(firm);159 }160 Mockito.verify(firmDao).getListByOrderByName();161 }162 @Test163 void FirmServiceImpl_getContactPersonsListByFirmId() {164 Firm firm = Firm.builder()165 .id(1)166 .build();167 List<ContactPerson> contactPersonList = new ArrayList<>();168 contactPersonList.add(ContactPerson.builder()169 .id(1)170 .name("Dima")171 .firm(firm)172 .build());173 contactPersonList.add(ContactPerson.builder()174 .id(1)175 .name("Tima")176 .firm(firm)177 .build());178 Mockito.doReturn(firm).when(firmDao).getById(firm.getId());179 Mockito.doReturn(contactPersonList).when(contactPersonDao).getListByFirmId(ArgumentMatchers.anyInt());180 firmService.getContactPersonsListByFirmId(firm.getId());181 Mockito.verify(contactPersonDao).getListByFirmId(firm.getId());182 }183 @Test184 void FirmServiceImpl_getContactPersonsListByFirmId_whenFirmNotFoundToThrowBusinessException() {185 Integer firmId = 1;186 Mockito.doReturn(null).when(firmDao).getById(firmId);187 Assertions.assertThrows(BusinessException.class,188 () -> firmService.getContactPersonsListByFirmId(firmId));189 }190 @Test191 void FirmServiceImpl_getCarListByFirmId() {192 Integer firmId = 1;193 Firm firm = Firm.builder()194 .id(firmId)195 .build();196 List<Car> carList = new ArrayList<>();197 carList.add(Car.builder()198 .id(1)199 .firm(firm)200 .carStatus(CarStatus.AVAILABLE)201 .model("X8")202 .price(new BigDecimal("198"))203 .build());204 carList.add(Car.builder()205 .id(2)206 .firm(firm)207 .carStatus(CarStatus.AVAILABLE)208 .model("X7")209 .price(new BigDecimal("144"))210 .build());211 carList.add(Car.builder()212 .id(3)213 .firm(firm)214 .carStatus(CarStatus.AVAILABLE)215 .model("X9")216 .price(new BigDecimal("123"))217 .build());218 Mockito.doReturn(firm).when(firmDao).getById(ArgumentMatchers.anyInt());219 Mockito.doReturn(carList).when(carDao).getListByFirmId(ArgumentMatchers.anyInt());220 List<Car> cars = firmService.getCarListByFirmId(firmId);221 Assertions.assertNotNull(cars);222 for (Car car : cars) {223 Assertions.assertNotNull(car.getFirm());224 Assertions.assertEquals(car.getFirm().getId(), firmId);225 }226 Mockito.verify(carDao).getListByFirmId(ArgumentMatchers.anyInt());227 }228 @Test229 void CarServiceImpl_getCarListByFirmId_whenFirmNotFoundToThrowBusinessException() {230 Integer firmId = 1;231 Mockito.doReturn(null).when(firmDao).getById(ArgumentMatchers.anyInt());232 Assertions.assertThrows(BusinessException.class,233 () -> firmService.getCarListByFirmId(firmId));234 }235}...
Source:CatalogServiceImplTest.java
...31 .name("Car")32 .parentCatalog(Catalog.builder().id(2).build())33 .build();34 Mockito.doNothing().when(catalogDao).save(ArgumentMatchers.any());35 Mockito.doReturn(null).when(catalogDao).getById(ArgumentMatchers.anyInt());36 catalogService.addCatalog(catalog);37 Mockito.verify(catalogDao).save(catalog);38 }39 @Test40 void CatalogServiceImpl_addCatalog_whenCatalogHasEmptyNameToThrowBusinessException() {41 Catalog catalog = Catalog.builder()42 .id(1)43 .build();44 Assertions.assertThrows(BusinessException.class,45 () -> catalogService.addCatalog(catalog));46 }47 @Test48 void CatalogServiceImpl_removeCatalog() {49 Catalog catalog = Catalog.builder()50 .id(1)51 .name("Car")52 .build();53 List<Catalog> catalogList = new ArrayList<>();54 List<Car> carList = new ArrayList<>();55 Mockito.doReturn(catalogList).when(catalogDao).getListByParentCatalogId(ArgumentMatchers.anyInt());56 Mockito.doReturn(carList).when(carDao).getListByCatalogId(ArgumentMatchers.anyInt());57 Mockito.doNothing().when(catalogDao).delete(ArgumentMatchers.anyInt());58 catalogService.removeCatalog(catalog.getId());59 Mockito.verify(catalogDao).delete(catalog.getId());60 }61 @Test62 void CatalogServiceImpl_removeCatalog_whenCatalogHasChildCatalogToThrowBusinessException() {63 List<Catalog> catalogList = new ArrayList<>();64 catalogList.add(Catalog.builder().build());65 Mockito.doReturn(catalogList).when(catalogDao).getListByParentCatalogId(ArgumentMatchers.anyInt());66 Assertions.assertThrows(BusinessException.class,67 () -> catalogService.removeCatalog(1));68 }69 @Test70 void CatalogServiceImpl_removeCatalog_whenCatalogHasCarsToThrowBusinessException() {71 List<Catalog> catalogList = new ArrayList<>();72 List<Car> carList = new ArrayList<>();73 carList.add(Car.builder().build());74 Mockito.doReturn(catalogList).when(catalogDao).getListByParentCatalogId(ArgumentMatchers.anyInt());75 Mockito.doReturn(carList).when(carDao).getListByCatalogId(ArgumentMatchers.anyInt());76 Assertions.assertThrows(BusinessException.class,77 () -> catalogService.removeCatalog(1));78 }79 @Test80 void CatalogServiceImpl_updateCatalog() {81 Catalog catalog = Catalog.builder()82 .id(1)83 .name("Car")84 .parentCatalog(Catalog.builder().id(2).build())85 .build();86 Mockito.doNothing().when(catalogDao).update(ArgumentMatchers.any());87 Mockito.doReturn(catalog).when(catalogDao).getById(catalog.getId());88 Mockito.doReturn(null).when(catalogDao).getById(catalog.getParentCatalog().getId());89 catalogService.updateCatalog(catalog);90 Mockito.verify(catalogDao).update(catalog);91 }92 @Test93 void CatalogServiceImpl_updateCatalog_whenCatalogHasEmptyNameToThrowBusinessException() {94 Catalog catalog = Catalog.builder()95 .id(1)96 .build();97 Mockito.doReturn(catalog).when(catalogDao).getById(catalog.getId());98 Assertions.assertThrows(BusinessException.class,99 () -> catalogService.updateCatalog(catalog));100 }101 @Test102 void CatalogServiceImpl_getCatalog() {103 Catalog catalog = Catalog.builder()104 .id(1)105 .name("Car")106 .build();107 Mockito.doReturn(catalog).when(catalogDao).getById(ArgumentMatchers.anyInt());108 catalogService.getCatalog(catalog.getId());109 Mockito.verify(catalogDao).getById(ArgumentMatchers.anyInt());110 }111 @Test112 void CatalogServiceImpl_getList() {113 List<Catalog> catalogList = new ArrayList<>();114 catalogList.add(Catalog.builder()115 .id(1)116 .name("Car")117 .build());118 catalogList.add(Catalog.builder()119 .id(2)120 .name("Car")121 .build());122 catalogList.add(Catalog.builder()123 .id(3)124 .name("Car")125 .build());126 Mockito.doReturn(catalogList).when(catalogDao).getAll();127 List<Catalog> catalogs = catalogService.getList();128 Assertions.assertNotNull(catalogs);129 for (Catalog catalog : catalogs) {130 Assertions.assertNotNull(catalog);131 }132 Mockito.verify(catalogDao).getAll();133 }134 @Test135 void CatalogServiceImpl_getListByParentCatalogId() {136 Catalog parentCatalog = Catalog.builder().id(7).name("Car").build();137 List<Catalog> catalogList = new ArrayList<>();138 catalogList.add(Catalog.builder()139 .id(1)140 .name("Fast car")141 .parentCatalog(parentCatalog)142 .build());143 catalogList.add(Catalog.builder()144 .id(2)145 .name("Small car")146 .parentCatalog(parentCatalog)147 .build());148 catalogList.add(Catalog.builder()149 .id(3)150 .name("Big car")151 .parentCatalog(parentCatalog)152 .build());153 Mockito.doReturn(catalogList).when(catalogDao).getListByParentCatalogId(ArgumentMatchers.anyInt());154 Mockito.doReturn(parentCatalog).when(catalogDao).getById(ArgumentMatchers.anyInt());155 List<Catalog> catalogs = catalogService.getListByParentCatalogId(parentCatalog.getId());156 Assertions.assertNotNull(catalogs);157 for (Catalog catalog : catalogs) {158 Assertions.assertNotNull(catalog);159 Assertions.assertNotNull(catalog.getParentCatalog());160 Assertions.assertEquals(catalog.getParentCatalog(), parentCatalog);161 }162 Mockito.verify(catalogDao).getListByParentCatalogId(parentCatalog.getId());163 }164 @Test165 void CatalogServiceImpl_getListByParentCatalogId_whenParentCatalogNotFoundToThrowBusinessException() {166 Integer parentCatalogId = 1;167 Mockito.doReturn(null).when(catalogDao).getById(ArgumentMatchers.anyInt());168 Assertions.assertThrows(BusinessException.class,169 () -> catalogService.getListByParentCatalogId(parentCatalogId));170 }171 @Test172 void CatalogServiceImpl_getCarListByCatalogId() {173 Integer catalogId = 1;174 Catalog catalog = Catalog.builder()175 .id(catalogId)176 .build();177 List<Car> carList = new ArrayList<>();178 carList.add(Car.builder()179 .id(1)180 .catalog(catalog)181 .carStatus(CarStatus.AVAILABLE)182 .model("X8")183 .price(new BigDecimal("198"))184 .build());185 carList.add(Car.builder()186 .id(2)187 .catalog(catalog)188 .carStatus(CarStatus.AVAILABLE)189 .model("X7")190 .price(new BigDecimal("144"))191 .build());192 carList.add(Car.builder()193 .id(3)194 .catalog(catalog)195 .carStatus(CarStatus.AVAILABLE)196 .model("X9")197 .price(new BigDecimal("123"))198 .build());199 Mockito.doReturn(catalog).when(catalogDao).getById(ArgumentMatchers.anyInt());200 Mockito.doReturn(carList).when(carDao).getListByCatalogId(ArgumentMatchers.anyInt());201 List<Car> cars = catalogService.getCarListByCatalogId(catalogId);202 Assertions.assertNotNull(cars);203 for (Car car : cars) {204 Assertions.assertNotNull(car.getCatalog());205 Assertions.assertEquals(car.getCatalog().getId(), catalogId);206 }207 }208 @Test209 void CatalogServiceImpl_getCarListByCatalogId_whenCatalogNotFoundToThrowBusinessException() {210 Integer catalogId = 1;211 Mockito.doReturn(null).when(catalogDao).getById(ArgumentMatchers.anyInt());212 Assertions.assertThrows(BusinessException.class,213 () -> catalogService.getCarListByCatalogId(catalogId));214 }215}...
Source:CheckoutServiceTest.java
...63 @Test64 public void calculateCost_productId_001_quantity_3() {65 List<String> products = Arrays.asList("001", "001", "001");66 Mockito.when(productCatalogueRepository.findAllByProductIdIn(products)).thenReturn(Arrays.asList(watch1));67 Mockito.when(productDiscountRepository.findApplicableDiscount(ArgumentMatchers.anyInt(),68 ArgumentMatchers.anyInt())).thenReturn(Optional.of(discount1));69 Integer totalCost = checkoutService.calculateCost(products);70 Assertions.assertEquals(Integer.valueOf(200), totalCost);71 }72 @Test73 public void calculateCost_productId_001_quantity_4() {74 List<String> products = Arrays.asList("001", "001", "001", "001");75 Mockito.when(productCatalogueRepository.findAllByProductIdIn(products)).thenReturn(Arrays.asList(watch1));76 Mockito.when(productDiscountRepository.findApplicableDiscount(ArgumentMatchers.anyInt(),77 ArgumentMatchers.anyInt())).thenReturn(Optional.of(discount1));78 Integer totalCost = checkoutService.calculateCost(products);79 Assertions.assertEquals(Integer.valueOf(300), totalCost);80 }81 @Test82 public void calculateCost_productId_001_quantity_6() {83 List<String> products = Arrays.asList("001", "001", "001", "001", "001", "001");84 Mockito.when(productCatalogueRepository.findAllByProductIdIn(products)).thenReturn(Arrays.asList(watch1));85 Mockito.when(productDiscountRepository.findApplicableDiscount(ArgumentMatchers.anyInt(),86 ArgumentMatchers.anyInt())).thenReturn(Optional.of(discount1));87 Integer totalCost = checkoutService.calculateCost(products);88 Assertions.assertEquals(Integer.valueOf(400), totalCost);89 }90 @Test91 public void calculateCost_productId_001_quantity_8() {92 List<String> products = Arrays.asList("001", "001", "001", "001", "001", "001", "001", "001");93 Mockito.when(productCatalogueRepository.findAllByProductIdIn(products)).thenReturn(Arrays.asList(watch1));94 Mockito.when(productDiscountRepository.findApplicableDiscount(ArgumentMatchers.anyInt(),95 ArgumentMatchers.anyInt())).thenReturn(Optional.of(discount1));96 Integer totalCost = checkoutService.calculateCost(products);97 Assertions.assertEquals(Integer.valueOf(600), totalCost);98 }99 @Test100 public void calculateCost_productId_002_quantity_1() {101 List<String> products = Arrays.asList("002");102 Mockito.when(productCatalogueRepository.findAllByProductIdIn(products)).thenReturn(Arrays.asList(watch2));103 Integer totalCost = checkoutService.calculateCost(products);104 Assertions.assertEquals(Integer.valueOf(80), totalCost);105 }106 @Test107 public void calculateCost_productId_002_quantity_2() {108 List<String> products = Arrays.asList("002", "002");109 Mockito.when(productCatalogueRepository.findAllByProductIdIn(products)).thenReturn(Arrays.asList(watch2));110 Mockito.when(productDiscountRepository.findApplicableDiscount(ArgumentMatchers.anyInt(),111 ArgumentMatchers.anyInt())).thenReturn(Optional.of(discount2));112 Integer totalCost = checkoutService.calculateCost(products);113 Assertions.assertEquals(Integer.valueOf(120), totalCost);114 }115 @Test116 public void calculateCost_productId_002_quantity_4() {117 List<String> products = Arrays.asList("002", "002", "002", "002");118 Mockito.when(productCatalogueRepository.findAllByProductIdIn(products)).thenReturn(Arrays.asList(watch2));119 Mockito.when(productDiscountRepository.findApplicableDiscount(ArgumentMatchers.anyInt(),120 ArgumentMatchers.anyInt())).thenReturn(Optional.of(discount2));121 Integer totalCost = checkoutService.calculateCost(products);122 Assertions.assertEquals(Integer.valueOf(240), totalCost);123 }124 @Test125 public void calculateCost_productId_002_quantity5() {126 List<String> products = Arrays.asList("002", "002", "002", "002", "002");127 Mockito.when(productCatalogueRepository.findAllByProductIdIn(products)).thenReturn(Arrays.asList(watch2));128 Mockito.when(productDiscountRepository.findApplicableDiscount(ArgumentMatchers.anyInt(),129 ArgumentMatchers.anyInt())).thenReturn(Optional.of(discount2));130 Integer totalCost = checkoutService.calculateCost(products);131 Assertions.assertEquals(Integer.valueOf(320), totalCost);132 }133 @Test134 public void calculateCost_multiProduct() {135 List<String> products = Arrays.asList("001", "002", "001", "004", "003");136 Mockito.when(productCatalogueRepository.findAllByProductIdIn(products))137 .thenReturn(Arrays.asList(watch1, watch2, watch3, watch4));138 Mockito.when(productDiscountRepository.findApplicableDiscount(ArgumentMatchers.eq(1),139 ArgumentMatchers.anyInt())).thenReturn(Optional.of(discount1));140 Mockito.when(productDiscountRepository.findApplicableDiscount(ArgumentMatchers.eq(2),141 ArgumentMatchers.anyInt())).thenReturn(Optional.of(discount2));142 Integer totalCost = checkoutService.calculateCost(products);143 Assertions.assertEquals(Integer.valueOf(360), totalCost);144 }145}...
Source:MockitoAnyIncorrectPrimitiveTypeTest.java
...32 .addSourceLines(33 "Test.java",34 "package org.mockito;",35 "import static org.mockito.ArgumentMatchers.any;",36 "import static org.mockito.ArgumentMatchers.anyInt;",37 "import static org.mockito.Mockito.mock;",38 "import static org.mockito.Mockito.when;",39 "class Test {",40 " public void test() {",41 " Foo foo = mock(Foo.class);",42 " // BUG: Diagnostic contains:",43 " when(foo.run(anyInt())).thenReturn(5);",44 " // BUG: Diagnostic contains:",45 " when(foo.runWithBoth(any(String.class), anyInt())).thenReturn(5);",46 " }",47 " static class Foo {",48 " int run(long arg) {",49 " return 42;",50 " }",51 " int runWithBoth(String arg1, long arg2) {",52 " return 42;",53 " }",54 " }",55 "}")56 .doTest();57 }58 @Test59 public void testNegativeCases() {60 compilationHelper61 .addSourceLines(62 "Test.java",63 "package org.mockito;",64 "import static org.mockito.ArgumentMatchers.any;",65 "import static org.mockito.ArgumentMatchers.anyFloat;",66 "import static org.mockito.ArgumentMatchers.anyLong;",67 "import static org.mockito.Mockito.mock;",68 "import static org.mockito.Mockito.when;",69 "class Test {",70 " public void test() {",71 " Foo foo = mock(Foo.class);",72 " when(foo.runWithInt(anyFloat())).thenReturn(5);",73 " when(foo.runWithBoth(any(String.class), anyLong())).thenReturn(5);",74 " }",75 " static class Foo {",76 " int run(String arg) {",77 " return 42;",78 " }",79 " int runWithInt(float arg) {",80 " return 42;",81 " }",82 " int runWithBoth(String arg1, long arg2) {",83 " return 42;",84 " }",85 " }",86 "}")87 .doTest();88 }89 @Test90 public void testPositivesSubclass() {91 compilationHelper92 .addSourceLines(93 "Test.java",94 "package org.mockito;",95 "import static org.mockito.Mockito.mock;",96 "import static org.mockito.Mockito.when;",97 "import static org.mockito.Mockito.any;",98 "import static org.mockito.Mockito.anyInt;",99 "class Test {",100 " public void test() {",101 " Foo foo = mock(Foo.class);",102 " // BUG: Diagnostic contains:",103 " when(foo.run(anyInt())).thenReturn(5);",104 " // BUG: Diagnostic contains:",105 " when(foo.runWithBoth(any(String.class), anyInt())).thenReturn(5);",106 " }",107 " static class Foo {",108 " int run(long arg) {",109 " return 42;",110 " }",111 " int runWithBoth(String arg1, float arg2) {",112 " return 42;",113 " }",114 " }",115 "}")116 .doTest();117 }118 @Test119 public void testRefactoring() {120 refactoringHelper121 .addInputLines(122 "Test.java",123 "import static org.mockito.ArgumentMatchers.any;",124 "import static org.mockito.ArgumentMatchers.anyInt;",125 "import static org.mockito.Mockito.mock;",126 "import static org.mockito.Mockito.when;",127 "class Test {",128 " public void test() {",129 " Foo foo = mock(Foo.class);",130 " when(foo.run(anyInt())).thenReturn(5);",131 " when(foo.runWithBoth(any(String.class), anyInt())).thenReturn(5);",132 " }",133 " static class Foo {",134 " int run(float arg) {",135 " return 42;",136 " }",137 " int runWithBoth(String arg1, long arg2) {",138 " return 42;",139 " }",140 " }",141 "}")142 .addOutputLines(143 "Test.java",144 "import static org.mockito.ArgumentMatchers.any;",145 "import static org.mockito.ArgumentMatchers.anyFloat;",146 "import static org.mockito.ArgumentMatchers.anyInt;",147 "import static org.mockito.ArgumentMatchers.anyLong;",148 "import static org.mockito.Mockito.mock;",149 "import static org.mockito.Mockito.when;",150 "class Test {",151 " public void test() {",152 " Foo foo = mock(Foo.class);",153 " when(foo.run(anyFloat())).thenReturn(5);",154 " when(foo.runWithBoth(any(String.class), anyLong())).thenReturn(5);",155 " }",156 " static class Foo {",157 " int run(float arg) {",158 " return 42;",159 " }",160 " int runWithBoth(String arg1, long arg2) {",...
Source:MainPanelTest.java
...86 mainPanel.setSortVisualiser(spy);87 // Act88 mainPanel.paintComponent(g);89 // Assert90 Mockito.verify(spy, Mockito.times(1)).paint(ArgumentMatchers.eq(g), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt(), ArgumentMatchers.any());91 }92 @Test93 public void paintComponentPaintsAllBarsMagentaWhenNotSortingAndSortingAlgorithmJustRan() {94 // Arrange95 mainPanel = new MainPanel(optionPane, 10, 1, 100);96 mainPanel.setSortingAlgorithmJustRan(true);97 Mockito.when(g.create()).thenReturn(g);98 // Act99 mainPanel.paintComponent(g);100 // Assert101 Mockito.verify(g, Mockito.times(1)).setColor(Color.MAGENTA);102 int expected = mainPanel.getData().size() + 1; // The 'plus 1' accounts for the super.paintComponent() call103 Mockito.verify(g, Mockito.times(expected)).fillRect(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(0), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());104 }105 @Test106 public void paintComponentPaintsAllBarsBlackWhenNotSortingAndSortingAlgorithmDidNotJustRun() {107 // Arrange108 mainPanel = new MainPanel(optionPane, 10, 1, 100);109 Mockito.when(g.create()).thenReturn(g);110 // Act111 mainPanel.paintComponent(g);112 // Assert113 Mockito.verify(g, Mockito.times(1)).setColor(Color.BLACK);114 int expected = mainPanel.getData().size() + 1; // The 'plus 1' accounts for the super.paintComponent() call115 Mockito.verify(g, Mockito.times(expected)).fillRect(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(0), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());116 }117}...
Source:BinaryFileWriterUnitTest.java
...6import java.io.InputStream;7import java.io.OutputStream;8import static org.junit.Assert.assertEquals;9import static org.mockito.ArgumentMatchers.any;10import static org.mockito.ArgumentMatchers.anyInt;11import static org.mockito.ArgumentMatchers.eq;12import static org.mockito.Mockito.mock;13import static org.mockito.Mockito.times;14import static org.mockito.Mockito.verify;15import static org.mockito.Mockito.when;16@RunWith(MockitoJUnitRunner.class)17public class BinaryFileWriterUnitTest {18 @Mock19 private OutputStream outputStream;20 @Test21 public void givenInputStream_whenWrite_thenExpectWritten() throws Exception {22 InputStream inputStream = mock(InputStream.class);23 when(inputStream.read(any(), anyInt(), anyInt())).thenReturn(10, -1);24 try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress, .0))) {25 long result = tested.write(inputStream, 10);26 assertEquals(10, result);27 verify(outputStream).write(any(), eq(0), eq(10));28 verify(inputStream).close();29 }30 verify(outputStream).close();31 }32 @Test33 public void givenInputStreamEmpty_whenWrite_thenExpectNotWritten() throws Exception {34 InputStream inputStream = mock(InputStream.class);35 try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress, .0))) {36 long result = tested.write(inputStream, 1);37 assertEquals(0, result);38 verify(outputStream, times(0)).write(any(), anyInt(), anyInt());39 verify(inputStream).close();40 }41 verify(outputStream).close();42 }43}...
anyInt
Using AI Code Generation
1import org.junit.Test;2import org.junit.runner.RunWith;3import org.mockito.junit.MockitoJUnitRunner;4import static org.mockito.ArgumentMatchers.anyInt;5import static org.mockito.Mockito.mock;6import static org.mockito.Mockito.when;7@RunWith(MockitoJUnitRunner.class)8public class anyIntMethod {9 public void test() {10 MyList list = mock(MyList.class);11 when(list.get(anyInt())).thenReturn("element");12 System.out.println(list.get(1));13 System.out.println(list.get(2));14 }15}16Example 2: Using anyInt() method with argument matcher17import org.junit.Test;18import org.junit.runner.RunWith;19import org.mockito.junit.MockitoJUnitRunner;20import static org.mockito.ArgumentMatchers.anyInt;21import static org.mockito.Mockito.mock;22import static org.mockito.Mockito.when;23@RunWith(MockitoJUnitRunner.class)24public class anyIntMethod {25 public void test() {26 MyList list = mock(MyList.class);27 when(list.get(anyInt())).thenReturn("element");28 System.out.println(list.get(1));29 System.out.println(list.get(2));30 }31}32Example 3: Using anyInt() method with verify method33import org.junit.Test;34import org.junit.runner.RunWith;35import org.mockito.junit.MockitoJUnitRunner;36import static org.mockito.ArgumentMatchers.anyInt;37import static org.mockito.Mockito.mock;38import static org.mockito.Mockito.verify;39@RunWith(MockitoJUnitRunner.class)40public class anyIntMethod {41 public void test() {42 MyList list = mock(MyList.class);43 list.get(1);44 verify(list).get(anyInt());45 }46}47Example 4: Using anyInt() method with verify method and argument matcher48import org.junit.Test;49import org.junit.runner.RunWith;50import org.mockito.junit.MockitoJUnitRunner;51import static org.mockito.ArgumentMatchers.anyInt;52import static org.mockito.Mockito.mock;53import static org.mockito.Mockito.verify;54@RunWith(MockitoJUnitRunner.class)55public class anyIntMethod {56 public void test() {
anyInt
Using AI Code Generation
1import static org.mockito.ArgumentMatchers.anyInt;2import static org.mockito.Mockito.mock;3import static org.mockito.Mockito.when;4public class Test {5 public static void main(String[] args) {6 ArrayList mockList = mock(ArrayList.class);7 when(mockList.size()).thenReturn(10);8 when(mockList.get(0)).thenReturn("Mockito");9 when(mockList.get(anyInt())).thenReturn("Mockito");10 System.out.println(mockList.get(0));11 System.out.println(mockList.get(1));12 System.out.println(mockList.get(2));13 }14}
anyInt
Using AI Code Generation
1import org.mockito.ArgumentMatchers;2import org.mockito.Mockito;3import java.util.List;4public class MockitoAnyIntMethod {5 public static void main(String[] args) {6 List list = Mockito.mock(List.class);7 Mockito.when(list.get(ArgumentMatchers.anyInt())).thenReturn("Hello");8 System.out.println(list.get(10));9 }10}11import org.mockito.Matchers;12import org.mockito.Mockito;13import java.util.List;14public class MockitoAnyIntMethod {15 public static void main(String[] args) {16 List list = Mockito.mock(List.class);17 Mockito.when(list.get(Matchers.anyInt())).thenReturn("Hello");18 System.out.println(list.get(10));19 }20}21import org.mockito.Matchers;22import org.mockito.Mockito;23import java.util.List;24public class MockitoAnyIntMethod {25 public static void main(String[] args) {26 List list = Mockito.mock(List.class);27 Mockito.when(list.get(Matchers.anyInt())).thenReturn("Hello");28 System.out.println(list.get(10));29 }30}31import org.mockito.Matchers;32import org.mockito.Mockito;33import java.util.List;34public class MockitoAnyIntMethod {35 public static void main(String[] args) {36 List list = Mockito.mock(List.class);37 Mockito.when(list.get(Matchers.anyInt())).thenReturn("Hello");38 System.out.println(list.get(10));39 }40}41import org.mockito.Matchers;42import org.mockito.Mockito;43import java.util.List;44public class MockitoAnyIntMethod {45 public static void main(String[] args) {46 List list = Mockito.mock(List.class);47 Mockito.when(list.get(Matchers.anyInt())).thenReturn("Hello");48 System.out.println(list.get(10));49 }50}51import org.mockito.Matchers;52import org.mockito.Mockito;53import java.util.List;
anyInt
Using AI Code Generation
1import static org.mockito.ArgumentMatchers.anyInt;2import static org.mockito.Mockito.doReturn;3import static org.mockito.Mockito.spy;4import static org.mockito.Mockito.verify;5import java.util.List;6import org.junit.Test;7public class AnyIntTest {8 public void test() {9 List<String> list = spy(List.class);10 doReturn("foo").when(list).get(anyInt());11 list.get(1);12 verify(list).get(anyInt());13 }14}15BUILD SUCCESSFUL (total time: 0 seconds)
anyInt
Using AI Code Generation
1public class MockitoExample {2 public void test() {3 List mock = mock(List.class);4 when(mock.get(anyInt())).thenReturn("Element");5 assertEquals("Element", mock.get(0));6 assertEquals("Element", mock.get(1));7 }8}9import static org.mockito.Matchers.anyInt;10public class MockitoExample {11 public void test() {12 List mock = mock(List.class);13 when(mock.get(anyInt())).thenReturn("Element");14 assertEquals("Element", mock.get(0));15 assertEquals("Element", mock.get(1));16 }17}18import static org.mockito.Matchers.*;19public class MockitoExample {20 public void test() {21 List mock = mock(List.class);22 when(mock.get(anyInt())).thenReturn("Element");23 assertEquals("Element", mock.get(0));24 assertEquals("Element", mock.get(1));25 }26}27import static org.mockito.Matchers.anyInt;28public class MockitoExample {29 public void test() {30 List mock = mock(List.class);31 when(mock.get(anyInt())).thenReturn("Element");32 assertEquals("Element", mock.get(0));33 assertEquals("Element", mock.get(1));34 }35}36import static org.mockito.Matchers.*;37public class MockitoExample {38 public void test() {39 List mock = mock(List.class);40 when(mock.get(anyInt())).thenReturn("Element");41 assertEquals("Element", mock.get(0));42 assertEquals("Element", mock.get(1));43 }44}45import static org.mockito.Matchers.anyInt;46public class MockitoExample {47 public void test() {48 List mock = mock(List.class);49 when(mock.get(anyInt())).thenReturn("Element");50 assertEquals("Element", mock.get(0));51 assertEquals("Element", mock.get(1));52 }53}
anyInt
Using AI Code Generation
1import static org.mockito.ArgumentMatchers.anyInt;2import static org.mockito.Mockito.mock;3import static org.mockito.Mockito.when;4class Test {5 public static void main(String[] args) {6 List list = mock(List.class);7 when(list.get(anyInt())).thenReturn("element");8 System.out.println(list.get(1));9 System.out.println(list.get(2));10 }11}
anyInt
Using AI Code Generation
1public class anyIntClass {2public static void main(String[] args) {3 List mockedList = mock(List.class);4 when(mockedList.get(anyInt())).thenReturn("element");5 System.out.println(mockedList.get(999));6 verify(mockedList).get(anyInt());7}8}9public class anyStringClass {10public static void main(String[] args) {11 List mockedList = mock(List.class);12 when(mockedList.get(anyString())).thenReturn("element");13 System.out.println(mockedList.get("Hello"));14 verify(mockedList).get(anyString());15}16}17public class anyDoubleClass {18public static void main(String[] args) {19 List mockedList = mock(List.class);20 when(mockedList.get(anyDouble())).thenReturn("element");21 System.out.println(mockedList.get(0.0001));22 verify(mockedList).get(anyDouble());23}24}25public class anyFloatClass {26public static void main(String[] args) {27 List mockedList = mock(List.class);28 when(mockedList.get(anyFloat())).thenReturn("element");29 System.out.println(mockedList.get(0.1f));30 verify(mockedList).get(anyFloat());31}32}33public class anyLongClass {34public static void main(String[] args) {35 List mockedList = mock(List.class);36 when(mockedList.get(anyLong())).thenReturn("element");37 System.out.println(mockedList.get(0L));38 verify(mockedList).get(anyLong());39}40}
anyInt
Using AI Code Generation
1import org.mockito.ArgumentMatchers;2import org.mockito.Mockito;3import static org.mockito.Mockito.*;4public class anyIntMethod {5 public static void main(String[] args) {6 List mockedList = Mockito.mock(List.class);7 when(mockedList.get(anyInt())).thenReturn("element");8 System.out.println(mockedList.get(999));9 verify(mockedList).get(anyInt());10 }11}12import org.mockito.ArgumentMatchers;13import org.mockito.Mockito;14import static org.mockito.Mockito.*;15public class anyIntMethod {16 public static void main(String[] args) {17 List mockedList = Mockito.mock(List.class);18 when(mockedList.get(anyInt())).thenThrow(new RuntimeException());19 mockedList.get(999);20 verify(mockedList).get(anyInt());21 }22}23-> at anyIntMethod.main(anyIntMethod.java:19)24 when(mock.isOk()).thenReturn(true);25 when(mock.isOk()).thenThrow(exception);26 doThrow(exception).when(mock).someVoidMethod();27-> at anyIntMethod.main(anyIntMethod.java:19)28import org.mockito.ArgumentMatchers;29import org.mockito.Mockito;30import static org.mockito.Mockito.*;31public class anyIntMethod {32 public static void main(String[] args) {33 List mockedList = Mockito.mock(List.class);34 when(mockedList.get(anyInt())).thenThrow(new RuntimeException()).thenReturn("element");35 mockedList.get(999);36 System.out.println(mockedList.get(999));37 verify(mockedList, times(2
anyInt
Using AI Code Generation
1import org.mockito.ArgumentMatchers;2import org.mockito.Mockito;3import static org.mockito.Mockito.when;4import static org.mockito.Mockito.anyInt;5public class Test1 {6 public static void main(String[] args) {7 MyClass obj = Mockito.mock(MyClass.class);8 when(obj.myMethod(ArgumentMatchers.anyInt())).thenReturn("Hello");9 System.out.println(obj.myMethod(10));10 }11}12import org.mockito.Mockito;13import static org.mockito.Mockito.when;14import static org.mockito.Matchers.anyInt;15public class Test2 {16 public static void main(String[] args) {17 MyClass obj = Mockito.mock(MyClass.class);18 when(obj.myMethod(anyInt())).thenReturn("Hello");19 System.out.println(obj.myMethod(10));20 }21}22How to use anyInt() method in Mockito?23How to use anyString() method in Mockito?24How to use anyList() method in Mockito?25How to use anySet() method in Mockito?26How to use anyMap() method in Mockito?27How to use anyCollection() method in Mockito?28How to use anyLong() method in Mockito?29How to use anyDouble() method in Mockito?30How to use anyBoolean() method in Mockito?31How to use any() method in Mockito?32How to use anyObject() method in Mockito?33How to use anyVararg() method in Mockito?34How to use anyIterable() method in Mockito?35How to use anyChar() method in Mockito?36How to use anyByte() method in Mockito?37How to use anyFloat() method in Mockito?38How to use anyShort() method in Mockito?39How to use any() method in Mockito?40How to use anyVararg() method in Mockito?41How to use anyIterable() method in Mockito?42How to use anyChar() method in Mockito?43How to use anyByte() method in Mockito?44How to use anyFloat() method in Mockito?45How to use anyShort() method in Mockito?46How to use any() method in Mockito?47How to use anyVararg() method in Mockito?48How to use anyIterable() method in Mockito?49How to use anyChar() method in Mockito?50How to use anyByte() method in Mockito?51How to use anyFloat() method in Mockito?52How to use anyShort() method in Mockito?53How to use any()
anyInt
Using AI Code Generation
1import static org.mockito.ArgumentMatchers.*;2public class AnyInt {3 public static void main(String[] args) {4 List mockedList = mock(List.class);5 mockedList.add(anyInt(), "anyInt");6 verify(mockedList).add(anyInt(), "anyInt");7 }8}92. anyLong()10anyLong()11import static org.mockito.ArgumentMatchers.*;12public class AnyLong {13 public static void main(String[] args) {14 List mockedList = mock(List.class);15 mockedList.add(anyLong(), "anyLong");16 verify(mockedList).add(anyLong(), "anyLong");17 }18}193. anyDouble()20anyDouble()21import static org.mockito.ArgumentMatchers.*;22public class AnyDouble {23 public static void main(String[] args) {24 List mockedList = mock(List.class);25 mockedList.add(anyDouble(), "anyDouble");26 verify(mockedList).add(anyDouble(), "anyDouble");27 }28}29In the above example, we have created a mock object of List interface and added anyDouble() method to it. We have verified
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!!