How to use createPartialMockAndInvokeDefaultConstructor method of org.powermock.api.easymock.PowerMock class

Best Powermock code snippet using org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor

Source:TestBitstampExchangeAdapter.java Github

copy

Full Screen

...155 final Map<String, String> requestParamMap = PowerMock.createMock(Map.class);156 expect(requestParamMap.put("id", ORDER_ID_TO_CANCEL)).andStubReturn(null);157 // Partial mock so we do not send stuff down the wire158 final BitstampExchangeAdapter exchangeAdapter =159 PowerMock.createPartialMockAndInvokeDefaultConstructor(160 BitstampExchangeAdapter.class,161 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,162 MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD);163 PowerMock.expectPrivate(exchangeAdapter, MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD)164 .andReturn(requestParamMap);165 PowerMock.expectPrivate(166 exchangeAdapter,167 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,168 eq(CANCEL_ORDER),169 eq(requestParamMap))170 .andReturn(exchangeResponse);171 PowerMock.replayAll();172 exchangeAdapter.init(exchangeConfig);173 // marketId arg not needed for cancelling orders on this exchange.174 final boolean success = exchangeAdapter.cancelOrder(ORDER_ID_TO_CANCEL, null);175 assertTrue(success);176 PowerMock.verifyAll();177 }178 @Test(expected = ExchangeNetworkException.class)179 public void testCancelOrderHandlesExchangeNetworkException() throws Exception {180 final BitstampExchangeAdapter exchangeAdapter =181 PowerMock.createPartialMockAndInvokeDefaultConstructor(182 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);183 PowerMock.expectPrivate(184 exchangeAdapter,185 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,186 eq(CANCEL_ORDER),187 anyObject(Map.class))188 .andThrow(189 new ExchangeNetworkException(190 "Traveling through hyperspace ain't like dusting crops, boy!"191 + " Without precise calculations we could fly right through a star, or bounce "192 + "too close to a supernova and that'd end your trip real quick, wouldn't "193 + "it?"));194 PowerMock.replayAll();195 exchangeAdapter.init(exchangeConfig);196 // marketId arg not needed for cancelling orders on this exchange.197 exchangeAdapter.cancelOrder(ORDER_ID_TO_CANCEL, null);198 PowerMock.verifyAll();199 }200 @Test(expected = TradingApiException.class)201 public void testCancelOrderHandlesUnexpectedException() throws Exception {202 final BitstampExchangeAdapter exchangeAdapter =203 PowerMock.createPartialMockAndInvokeDefaultConstructor(204 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);205 PowerMock.expectPrivate(206 exchangeAdapter,207 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,208 eq(CANCEL_ORDER),209 anyObject(Map.class))210 .andThrow(new IllegalStateException("The Force is strong with this one."));211 PowerMock.replayAll();212 exchangeAdapter.init(exchangeConfig);213 // marketId arg not needed for cancelling orders on this exchange.214 exchangeAdapter.cancelOrder(ORDER_ID_TO_CANCEL, null);215 PowerMock.verifyAll();216 }217 // --------------------------------------------------------------------------218 // Create Orders tests219 // --------------------------------------------------------------------------220 @Test221 @SuppressWarnings("unchecked")222 public void testCreateOrderToBuyIsSuccessful() throws Exception {223 final byte[] encoded = Files.readAllBytes(Paths.get(BUY_JSON_RESPONSE));224 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =225 new AbstractExchangeAdapter.ExchangeHttpResponse(226 200, "OK", new String(encoded, StandardCharsets.UTF_8));227 final Map<String, String> requestParamMap = PowerMock.createMock(Map.class);228 expect(229 requestParamMap.put(230 "price",231 new DecimalFormat("#.##", getDecimalFormatSymbols()).format(BUY_ORDER_PRICE)))232 .andStubReturn(null);233 expect(234 requestParamMap.put(235 "amount",236 new DecimalFormat("#.########", getDecimalFormatSymbols())237 .format(BUY_ORDER_QUANTITY)))238 .andStubReturn(null);239 expect(requestParamMap.put("type", "buy")).andStubReturn(null);240 final BitstampExchangeAdapter exchangeAdapter =241 PowerMock.createPartialMockAndInvokeDefaultConstructor(242 BitstampExchangeAdapter.class,243 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,244 MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD);245 PowerMock.expectPrivate(exchangeAdapter, MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD)246 .andReturn(requestParamMap);247 PowerMock.expectPrivate(248 exchangeAdapter,249 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,250 eq(BUY + MARKET_ID),251 eq(requestParamMap))252 .andReturn(exchangeResponse);253 PowerMock.replayAll();254 exchangeAdapter.init(exchangeConfig);255 final String orderId =256 exchangeAdapter.createOrder(MARKET_ID, OrderType.BUY, BUY_ORDER_QUANTITY, BUY_ORDER_PRICE);257 assertEquals("80890994", orderId);258 PowerMock.verifyAll();259 }260 @Test261 @SuppressWarnings("unchecked")262 public void testCreateOrderToSellIsSuccessful() throws Exception {263 final byte[] encoded = Files.readAllBytes(Paths.get(SELL_JSON_RESPONSE));264 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =265 new AbstractExchangeAdapter.ExchangeHttpResponse(266 200, "OK", new String(encoded, StandardCharsets.UTF_8));267 final Map<String, String> requestParamMap = PowerMock.createMock(Map.class);268 expect(269 requestParamMap.put(270 "price",271 new DecimalFormat("#.##", getDecimalFormatSymbols()).format(SELL_ORDER_PRICE)))272 .andStubReturn(null);273 expect(274 requestParamMap.put(275 "amount",276 new DecimalFormat("#.########", getDecimalFormatSymbols())277 .format(SELL_ORDER_QUANTITY)))278 .andStubReturn(null);279 expect(requestParamMap.put("type", "sell")).andStubReturn(null);280 final BitstampExchangeAdapter exchangeAdapter =281 PowerMock.createPartialMockAndInvokeDefaultConstructor(282 BitstampExchangeAdapter.class,283 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,284 MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD);285 PowerMock.expectPrivate(exchangeAdapter, MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD)286 .andReturn(requestParamMap);287 PowerMock.expectPrivate(288 exchangeAdapter,289 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,290 eq(SELL + MARKET_ID),291 eq(requestParamMap))292 .andReturn(exchangeResponse);293 PowerMock.replayAll();294 exchangeAdapter.init(exchangeConfig);295 final String orderId =296 exchangeAdapter.createOrder(297 MARKET_ID, OrderType.SELL, SELL_ORDER_QUANTITY, SELL_ORDER_PRICE);298 assertEquals("80890993", orderId);299 PowerMock.verifyAll();300 }301 @Test(expected = ExchangeNetworkException.class)302 public void testCreateOrderHandlesExchangeNetworkException() throws Exception {303 final BitstampExchangeAdapter exchangeAdapter =304 PowerMock.createPartialMockAndInvokeDefaultConstructor(305 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);306 PowerMock.expectPrivate(307 exchangeAdapter,308 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,309 eq(SELL + MARKET_ID),310 anyObject(Map.class))311 .andThrow(new ExchangeNetworkException("Aaaaaaaaaaaaaaaarrrgh!"));312 PowerMock.replayAll();313 exchangeAdapter.init(exchangeConfig);314 exchangeAdapter.createOrder(MARKET_ID, OrderType.SELL, SELL_ORDER_QUANTITY, SELL_ORDER_PRICE);315 PowerMock.verifyAll();316 }317 @Test(expected = TradingApiException.class)318 public void testCreateOrderHandlesUnexpectedException() throws Exception {319 final BitstampExchangeAdapter exchangeAdapter =320 PowerMock.createPartialMockAndInvokeDefaultConstructor(321 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);322 PowerMock.expectPrivate(323 exchangeAdapter,324 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,325 eq(BUY + MARKET_ID),326 anyObject(Map.class))327 .andThrow(328 new IllegalArgumentException(329 "That's 'cause droids don't pull people's arms out of their "330 + "sockets when they lose. Wookiees are known to do that."));331 PowerMock.replayAll();332 exchangeAdapter.init(exchangeConfig);333 exchangeAdapter.createOrder(MARKET_ID, OrderType.BUY, BUY_ORDER_QUANTITY, BUY_ORDER_PRICE);334 PowerMock.verifyAll();335 }336 // --------------------------------------------------------------------------337 // Get Market Orders tests338 // --------------------------------------------------------------------------339 @Test340 public void testGettingMarketOrdersSuccessfully() throws Exception {341 final byte[] encoded = Files.readAllBytes(Paths.get(ORDER_BOOK_JSON_RESPONSE));342 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =343 new AbstractExchangeAdapter.ExchangeHttpResponse(344 200, "OK", new String(encoded, StandardCharsets.UTF_8));345 final BitstampExchangeAdapter exchangeAdapter =346 PowerMock.createPartialMockAndInvokeDefaultConstructor(347 BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD);348 PowerMock.expectPrivate(349 exchangeAdapter,350 MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD,351 eq(ORDER_BOOK + MARKET_ID))352 .andReturn(exchangeResponse);353 PowerMock.replayAll();354 exchangeAdapter.init(exchangeConfig);355 final MarketOrderBook marketOrderBook = exchangeAdapter.getMarketOrders(MARKET_ID);356 // assert some key stuff; we're not testing GSON here.357 assertEquals(MARKET_ID, marketOrderBook.getMarketId());358 final BigDecimal buyPrice = new BigDecimal("230.34");359 final BigDecimal buyQuantity = new BigDecimal("7.22860000");360 final BigDecimal buyTotal = buyPrice.multiply(buyQuantity);361 assertEquals(1268, marketOrderBook.getBuyOrders().size()); // stamp send them all back!362 assertSame(OrderType.BUY, marketOrderBook.getBuyOrders().get(0).getType());363 assertEquals(0, marketOrderBook.getBuyOrders().get(0).getPrice().compareTo(buyPrice));364 assertEquals(0, marketOrderBook.getBuyOrders().get(0).getQuantity().compareTo(buyQuantity));365 assertEquals(0, marketOrderBook.getBuyOrders().get(0).getTotal().compareTo(buyTotal));366 final BigDecimal sellPrice = new BigDecimal("230.90");367 final BigDecimal sellQuantity = new BigDecimal("0.62263188");368 final BigDecimal sellTotal = sellPrice.multiply(sellQuantity);369 assertEquals(1957, marketOrderBook.getSellOrders().size()); // stamp send them all back!370 assertSame(OrderType.SELL, marketOrderBook.getSellOrders().get(0).getType());371 assertEquals(0, marketOrderBook.getSellOrders().get(0).getPrice().compareTo(sellPrice));372 assertEquals(0, marketOrderBook.getSellOrders().get(0).getQuantity().compareTo(sellQuantity));373 assertEquals(0, marketOrderBook.getSellOrders().get(0).getTotal().compareTo(sellTotal));374 PowerMock.verifyAll();375 }376 @Test(expected = ExchangeNetworkException.class)377 public void testGettingMarketOrdersHandlesExchangeNetworkException() throws Exception {378 final BitstampExchangeAdapter exchangeAdapter =379 PowerMock.createPartialMockAndInvokeDefaultConstructor(380 BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD);381 PowerMock.expectPrivate(382 exchangeAdapter,383 MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD,384 eq(ORDER_BOOK + MARKET_ID))385 .andThrow(386 new ExchangeNetworkException(387 "Traveling through hyperspace ain’t like dusting crops, farm boy."));388 PowerMock.replayAll();389 exchangeAdapter.init(exchangeConfig);390 exchangeAdapter.getMarketOrders(MARKET_ID);391 PowerMock.verifyAll();392 }393 @Test(expected = TradingApiException.class)394 public void testGettingMarketOrdersHandlesUnexpectedException() throws Exception {395 final BitstampExchangeAdapter exchangeAdapter =396 PowerMock.createPartialMockAndInvokeDefaultConstructor(397 BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD);398 PowerMock.expectPrivate(399 exchangeAdapter,400 MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD,401 eq(ORDER_BOOK + MARKET_ID))402 .andThrow(403 new IllegalArgumentException(404 "Uh, we had a slight weapons malfunction, but uh... "405 + "everything's perfectly all right now. We're fine. We're all fine here now, "406 + "thank you. How are you?"));407 PowerMock.replayAll();408 exchangeAdapter.init(exchangeConfig);409 exchangeAdapter.getMarketOrders(MARKET_ID);410 PowerMock.verifyAll();411 }412 // --------------------------------------------------------------------------413 // Get Your Open Orders tests414 // --------------------------------------------------------------------------415 @Test416 public void testGettingYourOpenOrdersSuccessfully() throws Exception {417 final byte[] encoded = Files.readAllBytes(Paths.get(OPEN_ORDERS_JSON_RESPONSE));418 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =419 new AbstractExchangeAdapter.ExchangeHttpResponse(420 200, "OK", new String(encoded, StandardCharsets.UTF_8));421 final BitstampExchangeAdapter exchangeAdapter =422 PowerMock.createPartialMockAndInvokeDefaultConstructor(423 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);424 PowerMock.expectPrivate(425 exchangeAdapter,426 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,427 eq(OPEN_ORDERS + MARKET_ID),428 eq(null))429 .andReturn(exchangeResponse);430 PowerMock.replayAll();431 exchangeAdapter.init(exchangeConfig);432 final List<OpenOrder> openOrders = exchangeAdapter.getYourOpenOrders(MARKET_ID);433 // assert some key stuff; we're not testing GSON here.434 assertEquals(2, openOrders.size());435 assertEquals(MARKET_ID, openOrders.get(0).getMarketId());436 assertEquals("52603560", openOrders.get(0).getId());437 assertSame(OrderType.SELL, openOrders.get(0).getType());438 assertEquals(439 openOrders.get(0).getCreationDate().getTime(),440 bitstampExchangeDateFormat.parse("2015-01-09 21:14:50").getTime());441 assertEquals(0, openOrders.get(0).getPrice().compareTo(new BigDecimal("350.00")));442 assertEquals(0, openOrders.get(0).getQuantity().compareTo(new BigDecimal("0.20000000")));443 assertEquals(444 0,445 openOrders446 .get(0)447 .getTotal()448 .compareTo(openOrders.get(0).getPrice().multiply(openOrders.get(0).getQuantity())));449 // the values below are not provided by Bitstamp450 assertNull(openOrders.get(0).getOriginalQuantity());451 PowerMock.verifyAll();452 }453 @Test(expected = ExchangeNetworkException.class)454 public void testGettingYourOpenOrdersHandlesExchangeNetworkException() throws Exception {455 final BitstampExchangeAdapter exchangeAdapter =456 PowerMock.createPartialMockAndInvokeDefaultConstructor(457 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);458 PowerMock.expectPrivate(459 exchangeAdapter,460 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,461 eq(OPEN_ORDERS + MARKET_ID),462 eq(null))463 .andThrow(new ExchangeNetworkException("The board is green!"));464 PowerMock.replayAll();465 exchangeAdapter.init(exchangeConfig);466 exchangeAdapter.getYourOpenOrders(MARKET_ID);467 PowerMock.verifyAll();468 }469 @Test(expected = TradingApiException.class)470 public void testGettingYourOpenOrdersHandlesUnexpectedException() throws Exception {471 final BitstampExchangeAdapter exchangeAdapter =472 PowerMock.createPartialMockAndInvokeDefaultConstructor(473 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);474 PowerMock.expectPrivate(475 exchangeAdapter,476 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,477 eq(OPEN_ORDERS + MARKET_ID),478 eq(null))479 .andThrow(480 new IllegalStateException(481 "You may dispense with the pleasantries, Commander. "482 + "I am here to put you back on schedule."));483 PowerMock.replayAll();484 exchangeAdapter.init(exchangeConfig);485 exchangeAdapter.getYourOpenOrders(MARKET_ID);486 PowerMock.verifyAll();487 }488 // --------------------------------------------------------------------------489 // Get Latest Market Price tests490 // --------------------------------------------------------------------------491 @Test492 public void testGettingLatestMarketPriceSuccessfully() throws Exception {493 final byte[] encoded = Files.readAllBytes(Paths.get(TICKER_JSON_RESPONSE));494 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =495 new AbstractExchangeAdapter.ExchangeHttpResponse(496 200, "OK", new String(encoded, StandardCharsets.UTF_8));497 final BitstampExchangeAdapter exchangeAdapter =498 PowerMock.createPartialMockAndInvokeDefaultConstructor(499 BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD);500 PowerMock.expectPrivate(501 exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, eq(TICKER + MARKET_ID))502 .andReturn(exchangeResponse);503 PowerMock.replayAll();504 exchangeAdapter.init(exchangeConfig);505 final BigDecimal latestMarketPrice =506 exchangeAdapter.getLatestMarketPrice(MARKET_ID).setScale(8, RoundingMode.HALF_UP);507 assertEquals(0, latestMarketPrice.compareTo(new BigDecimal("230.33")));508 PowerMock.verifyAll();509 }510 @Test(expected = ExchangeNetworkException.class)511 public void testGettingLatestMarketPriceHandlesExchangeNetworkException() throws Exception {512 final BitstampExchangeAdapter exchangeAdapter =513 PowerMock.createPartialMockAndInvokeDefaultConstructor(514 BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD);515 PowerMock.expectPrivate(516 exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, eq(TICKER + MARKET_ID))517 .andThrow(new ExchangeNetworkException("Jumping in 5... 4... 3... 2... 1... Jump!"));518 PowerMock.replayAll();519 exchangeAdapter.init(exchangeConfig);520 exchangeAdapter.getLatestMarketPrice(MARKET_ID);521 PowerMock.verifyAll();522 }523 @Test(expected = TradingApiException.class)524 public void testGettingLatestMarketPriceHandlesUnexpectedException() throws Exception {525 final BitstampExchangeAdapter exchangeAdapter =526 PowerMock.createPartialMockAndInvokeDefaultConstructor(527 BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD);528 PowerMock.expectPrivate(529 exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, eq(TICKER + MARKET_ID))530 .andThrow(531 new IllegalArgumentException(532 "Sir, the possibility of successfully navigating an asteroid field"533 + " is approximately 3,720 to 1."));534 PowerMock.replayAll();535 exchangeAdapter.init(exchangeConfig);536 exchangeAdapter.getLatestMarketPrice(MARKET_ID);537 PowerMock.verifyAll();538 }539 // --------------------------------------------------------------------------540 // Get Balance Info tests541 // --------------------------------------------------------------------------542 @Test543 public void testGettingBalanceInfoSuccessfully() throws Exception {544 final byte[] encoded = Files.readAllBytes(Paths.get(BALANCE_JSON_RESPONSE));545 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =546 new AbstractExchangeAdapter.ExchangeHttpResponse(547 200, "OK", new String(encoded, StandardCharsets.UTF_8));548 final BitstampExchangeAdapter exchangeAdapter =549 PowerMock.createPartialMockAndInvokeDefaultConstructor(550 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);551 PowerMock.expectPrivate(552 exchangeAdapter,553 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,554 eq(BALANCE),555 eq(null))556 .andReturn(exchangeResponse);557 PowerMock.replayAll();558 exchangeAdapter.init(exchangeConfig);559 final BalanceInfo balanceInfo = exchangeAdapter.getBalanceInfo();560 assertEquals(561 0, balanceInfo.getBalancesAvailable().get("BTC").compareTo(new BigDecimal("0.00760854")));562 assertEquals(563 0, balanceInfo.getBalancesAvailable().get("USD").compareTo(new BigDecimal("57.03")));564 assertEquals(565 0, balanceInfo.getBalancesAvailable().get("EUR").compareTo(new BigDecimal("16.01")));566 assertEquals(567 0, balanceInfo.getBalancesAvailable().get("LTC").compareTo(new BigDecimal("50.01")));568 assertEquals(569 0, balanceInfo.getBalancesAvailable().get("XRP").compareTo(new BigDecimal("10.01")));570 assertEquals(571 0, balanceInfo.getBalancesOnHold().get("BTC").compareTo(new BigDecimal("0.01918917")));572 assertEquals(0, balanceInfo.getBalancesOnHold().get("USD").compareTo(new BigDecimal("62.23")));573 assertEquals(0, balanceInfo.getBalancesOnHold().get("EUR").compareTo(new BigDecimal("12.01")));574 assertEquals(0, balanceInfo.getBalancesOnHold().get("LTC").compareTo(new BigDecimal("40.01")));575 assertEquals(0, balanceInfo.getBalancesOnHold().get("XRP").compareTo(new BigDecimal("5.01")));576 PowerMock.verifyAll();577 }578 @Test(expected = ExchangeNetworkException.class)579 public void testGettingBalanceInfoHandlesExchangeNetworkException() throws Exception {580 final BitstampExchangeAdapter exchangeAdapter =581 PowerMock.createPartialMockAndInvokeDefaultConstructor(582 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);583 PowerMock.expectPrivate(584 exchangeAdapter,585 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,586 eq(BALANCE),587 eq(null))588 .andThrow(589 new ExchangeNetworkException(590 "I’ve been waiting for you, Obi-Wan. We meet again, at last. "591 + "The circle is now complete. When I left you, I was but the learner; "592 + "now I am the master."));593 PowerMock.replayAll();594 exchangeAdapter.init(exchangeConfig);595 exchangeAdapter.getBalanceInfo();596 PowerMock.verifyAll();597 }598 @Test(expected = TradingApiException.class)599 public void testGettingBalanceInfoHandlesUnexpectedException() throws Exception {600 final BitstampExchangeAdapter exchangeAdapter =601 PowerMock.createPartialMockAndInvokeDefaultConstructor(602 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);603 PowerMock.expectPrivate(604 exchangeAdapter,605 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,606 eq(BALANCE),607 eq(null))608 .andThrow(new IllegalStateException("Get me some more frakking birds in the air!"));609 PowerMock.replayAll();610 exchangeAdapter.init(exchangeConfig);611 exchangeAdapter.getBalanceInfo();612 PowerMock.verifyAll();613 }614 // --------------------------------------------------------------------------615 // Get Exchange Fees for Buy orders tests616 // --------------------------------------------------------------------------617 @Test618 public void testGettingExchangeBuyingFeeSuccessfully() throws Exception {619 final byte[] encoded = Files.readAllBytes(Paths.get(BALANCE_JSON_RESPONSE));620 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =621 new AbstractExchangeAdapter.ExchangeHttpResponse(622 200, "OK", new String(encoded, StandardCharsets.UTF_8));623 final BitstampExchangeAdapter exchangeAdapter =624 PowerMock.createPartialMockAndInvokeDefaultConstructor(625 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);626 PowerMock.expectPrivate(627 exchangeAdapter,628 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,629 eq(BALANCE),630 eq(null))631 .andReturn(exchangeResponse);632 PowerMock.replayAll();633 exchangeAdapter.init(exchangeConfig);634 final BigDecimal buyPercentageFee =635 exchangeAdapter.getPercentageOfBuyOrderTakenForExchangeFee(MARKET_ID);636 assertEquals(0, buyPercentageFee.compareTo(new BigDecimal("0.0025")));637 PowerMock.verifyAll();638 }639 @Test(expected = ExchangeNetworkException.class)640 public void testGettingExchangeBuyingFeeHandlesTimeoutException() throws Exception {641 final BitstampExchangeAdapter exchangeAdapter =642 PowerMock.createPartialMockAndInvokeDefaultConstructor(643 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);644 PowerMock.expectPrivate(645 exchangeAdapter,646 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,647 eq(BALANCE),648 eq(null))649 .andThrow(new ExchangeNetworkException("Aren't you a little short for a stormtrooper?"));650 PowerMock.replayAll();651 exchangeAdapter.init(exchangeConfig);652 exchangeAdapter.getPercentageOfBuyOrderTakenForExchangeFee(MARKET_ID);653 PowerMock.verifyAll();654 }655 @Test(expected = TradingApiException.class)656 public void testGettingExchangeBuyingFeeHandlesUnexpectedException() throws Exception {657 final BitstampExchangeAdapter exchangeAdapter =658 PowerMock.createPartialMockAndInvokeDefaultConstructor(659 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);660 PowerMock.expectPrivate(661 exchangeAdapter,662 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,663 eq(BALANCE),664 eq(null))665 .andThrow(666 new IllegalStateException(667 "I felt a great disturbance in the Force, as if millions of voices"668 + " suddenly cried out in terror and were suddenly silenced. "669 + "I fear something terrible has happened."));670 PowerMock.replayAll();671 exchangeAdapter.init(exchangeConfig);672 exchangeAdapter.getPercentageOfBuyOrderTakenForExchangeFee(MARKET_ID);673 PowerMock.verifyAll();674 }675 // --------------------------------------------------------------------------676 // Get Exchange Fees for Sell orders tests677 // --------------------------------------------------------------------------678 @Test679 public void testGettingExchangeSellingFeeSuccessfully() throws Exception {680 final byte[] encoded = Files.readAllBytes(Paths.get(BALANCE_JSON_RESPONSE));681 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =682 new AbstractExchangeAdapter.ExchangeHttpResponse(683 200, "OK", new String(encoded, StandardCharsets.UTF_8));684 final BitstampExchangeAdapter exchangeAdapter =685 PowerMock.createPartialMockAndInvokeDefaultConstructor(686 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);687 PowerMock.expectPrivate(688 exchangeAdapter,689 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,690 eq(BALANCE),691 eq(null))692 .andReturn(exchangeResponse);693 PowerMock.replayAll();694 exchangeAdapter.init(exchangeConfig);695 final BigDecimal sellPercentageFee =696 exchangeAdapter.getPercentageOfSellOrderTakenForExchangeFee(MARKET_ID);697 assertEquals(0, sellPercentageFee.compareTo(new BigDecimal("0.0025")));698 PowerMock.verifyAll();699 }700 @Test(expected = ExchangeNetworkException.class)701 public void testGettingExchangeSellingFeeHandlesTimeoutException() throws Exception {702 final BitstampExchangeAdapter exchangeAdapter =703 PowerMock.createPartialMockAndInvokeDefaultConstructor(704 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);705 PowerMock.expectPrivate(706 exchangeAdapter,707 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,708 eq(BALANCE),709 eq(null))710 .andThrow(new ExchangeNetworkException("That's no moon. It's a space station."));711 PowerMock.replayAll();712 exchangeAdapter.init(exchangeConfig);713 exchangeAdapter.getPercentageOfSellOrderTakenForExchangeFee(MARKET_ID);714 PowerMock.verifyAll();715 }716 @Test(expected = TradingApiException.class)717 public void testGettingExchangeSellingFeeHandlesUnexpectedException() throws Exception {718 final BitstampExchangeAdapter exchangeAdapter =719 PowerMock.createPartialMockAndInvokeDefaultConstructor(720 BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);721 PowerMock.expectPrivate(722 exchangeAdapter,723 MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,724 eq(BALANCE),725 eq(null))726 .andThrow(727 new IllegalStateException(728 "Don't be too proud of this technological terror you've constructed. "729 + "The ability to destroy a planet is insignificant next to the power of the "730 + "Force."));731 PowerMock.replayAll();732 exchangeAdapter.init(exchangeConfig);733 exchangeAdapter.getPercentageOfSellOrderTakenForExchangeFee(MARKET_ID);734 PowerMock.verifyAll();735 }736 // --------------------------------------------------------------------------737 // Get Ticker tests738 // --------------------------------------------------------------------------739 @Test740 public void testGettingTickerSuccessfully() throws Exception {741 final byte[] encoded = Files.readAllBytes(Paths.get(TICKER_JSON_RESPONSE));742 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =743 new AbstractExchangeAdapter.ExchangeHttpResponse(744 200, "OK", new String(encoded, StandardCharsets.UTF_8));745 final BitstampExchangeAdapter exchangeAdapter =746 PowerMock.createPartialMockAndInvokeDefaultConstructor(747 BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD);748 PowerMock.expectPrivate(749 exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, eq(TICKER + MARKET_ID))750 .andReturn(exchangeResponse);751 PowerMock.replayAll();752 exchangeAdapter.init(exchangeConfig);753 final Ticker ticker = exchangeAdapter.getTicker(MARKET_ID);754 assertEquals(0, ticker.getLast().compareTo(new BigDecimal("230.33")));755 assertEquals(0, ticker.getAsk().compareTo(new BigDecimal("230.69")));756 assertEquals(0, ticker.getBid().compareTo(new BigDecimal("230.34")));757 assertEquals(0, ticker.getHigh().compareTo(new BigDecimal("231.38")));758 assertEquals(0, ticker.getLow().compareTo(new BigDecimal("224.39")));759 assertEquals(0, ticker.getOpen().compareTo(new BigDecimal("220.69")));760 assertEquals(0, ticker.getVolume().compareTo(new BigDecimal("16666.06077534")));761 assertEquals(0, ticker.getVwap().compareTo(new BigDecimal("228.28")));762 assertEquals(1441040860L, (long) ticker.getTimestamp());763 PowerMock.verifyAll();764 }765 @Test(expected = ExchangeNetworkException.class)766 public void testGettingTickerHandlesExchangeNetworkException() throws Exception {767 final BitstampExchangeAdapter exchangeAdapter =768 PowerMock.createPartialMockAndInvokeDefaultConstructor(769 BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD);770 PowerMock.expectPrivate(771 exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, eq(TICKER + MARKET_ID))772 .andThrow(new ExchangeNetworkException("Indy, why does the floor move?"));773 PowerMock.replayAll();774 exchangeAdapter.init(exchangeConfig);775 exchangeAdapter.getTicker(MARKET_ID);776 PowerMock.verifyAll();777 }778 @Test(expected = TradingApiException.class)779 public void testGettingTickerHandlesUnexpectedException() throws Exception {780 final BitstampExchangeAdapter exchangeAdapter =781 PowerMock.createPartialMockAndInvokeDefaultConstructor(782 BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD);783 PowerMock.expectPrivate(784 exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, eq(TICKER + MARKET_ID))785 .andThrow(786 new IllegalArgumentException(787 "Well, I mean that for nearly three thousand years man has "788 + "been searching for the lost ark. It's not something to be taken lightly. "789 + "No one knows its secrets. It's like nothing you've ever gone after before"));790 PowerMock.replayAll();791 exchangeAdapter.init(exchangeConfig);792 exchangeAdapter.getTicker(MARKET_ID);793 PowerMock.verifyAll();794 }795 // --------------------------------------------------------------------------796 // Non Exchange visiting tests797 // --------------------------------------------------------------------------798 @Test799 public void testGettingImplNameIsAsExpected() {800 PowerMock.replayAll();801 final BitstampExchangeAdapter exchangeAdapter = new BitstampExchangeAdapter();802 exchangeAdapter.init(exchangeConfig);803 assertEquals("Bitstamp HTTP API v2", exchangeAdapter.getImplName());804 PowerMock.verifyAll();805 }806 // --------------------------------------------------------------------------807 // Initialisation tests assume config property files are located under808 // src/test/resources809 // --------------------------------------------------------------------------810 @Test811 public void testExchangeAdapterInitialisesSuccessfully() {812 PowerMock.replayAll();813 final BitstampExchangeAdapter exchangeAdapter = new BitstampExchangeAdapter();814 exchangeAdapter.init(exchangeConfig);815 assertNotNull(exchangeAdapter);816 PowerMock.verifyAll();817 }818 @Test(expected = IllegalArgumentException.class)819 public void testExchangeAdapterThrowsExceptionIfClientIdConfigIsMissing() {820 PowerMock.reset(authenticationConfig);821 expect(authenticationConfig.getItem("client-id")).andReturn(null);822 expect(authenticationConfig.getItem("key")).andReturn("your_client_key");823 expect(authenticationConfig.getItem("secret")).andReturn("your_client_secret");824 PowerMock.replayAll();825 final ExchangeAdapter exchangeAdapter = new BitstampExchangeAdapter();826 exchangeAdapter.init(exchangeConfig);827 PowerMock.verifyAll();828 }829 @Test(expected = IllegalArgumentException.class)830 public void testExchangeAdapterThrowsExceptionIfPublicKeyConfigIsMissing() {831 PowerMock.reset(authenticationConfig);832 expect(authenticationConfig.getItem("client-id")).andReturn("your-client-id");833 expect(authenticationConfig.getItem("key")).andReturn(null);834 expect(authenticationConfig.getItem("secret")).andReturn("your_client_secret");835 PowerMock.replayAll();836 final ExchangeAdapter exchangeAdapter = new BitstampExchangeAdapter();837 exchangeAdapter.init(exchangeConfig);838 PowerMock.verifyAll();839 }840 @Test(expected = IllegalArgumentException.class)841 public void testExchangeAdapterThrowsExceptionIfSecretConfigIsMissing() {842 PowerMock.reset(authenticationConfig);843 expect(authenticationConfig.getItem("client-id")).andReturn("your-client-id");844 expect(authenticationConfig.getItem("key")).andReturn("your-client-key");845 expect(authenticationConfig.getItem("secret")).andReturn(null);846 PowerMock.replayAll();847 final ExchangeAdapter exchangeAdapter = new BitstampExchangeAdapter();848 exchangeAdapter.init(exchangeConfig);849 PowerMock.verifyAll();850 }851 @Test(expected = IllegalArgumentException.class)852 public void testExchangeAdapterThrowsExceptionIfTimeoutConfigIsMissing() {853 PowerMock.reset(networkConfig);854 expect(networkConfig.getConnectionTimeout()).andReturn(0);855 PowerMock.replayAll();856 final ExchangeAdapter exchangeAdapter = new BitstampExchangeAdapter();857 exchangeAdapter.init(exchangeConfig);858 PowerMock.verifyAll();859 }860 // --------------------------------------------------------------------------861 // Request sending tests862 // --------------------------------------------------------------------------863 @Test864 public void testSendingPublicRequestToExchangeSuccessfully() throws Exception {865 final byte[] encoded = Files.readAllBytes(Paths.get(TICKER_JSON_RESPONSE));866 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =867 new AbstractExchangeAdapter.ExchangeHttpResponse(868 200, "OK", new String(encoded, StandardCharsets.UTF_8));869 final BitstampExchangeAdapter exchangeAdapter =870 PowerMock.createPartialMockAndInvokeDefaultConstructor(871 BitstampExchangeAdapter.class, MOCKED_MAKE_NETWORK_REQUEST_METHOD);872 final URL url = new URL(API_BASE_URL + TICKER + MARKET_ID);873 PowerMock.expectPrivate(874 exchangeAdapter,875 MOCKED_MAKE_NETWORK_REQUEST_METHOD,876 eq(url),877 eq("GET"),878 eq(null),879 eq(new HashMap<>()))880 .andReturn(exchangeResponse);881 PowerMock.replayAll();882 exchangeAdapter.init(exchangeConfig);883 final BigDecimal lastMarketPrice = exchangeAdapter.getLatestMarketPrice(MARKET_ID);884 assertEquals(0, lastMarketPrice.compareTo(new BigDecimal("230.33")));885 PowerMock.verifyAll();886 }887 @Test(expected = ExchangeNetworkException.class)888 public void testSendingPublicRequestToExchangeHandlesExchangeNetworkException() throws Exception {889 final BitstampExchangeAdapter exchangeAdapter =890 PowerMock.createPartialMockAndInvokeDefaultConstructor(891 BitstampExchangeAdapter.class, MOCKED_MAKE_NETWORK_REQUEST_METHOD);892 final URL url = new URL(API_BASE_URL + TICKER + MARKET_ID);893 PowerMock.expectPrivate(894 exchangeAdapter,895 MOCKED_MAKE_NETWORK_REQUEST_METHOD,896 eq(url),897 eq("GET"),898 eq(null),899 eq(new HashMap<>()))900 .andThrow(901 new ExchangeNetworkException(902 "When 900 years old, you reach… Look as good, you will not."));903 PowerMock.replayAll();904 exchangeAdapter.init(exchangeConfig);905 exchangeAdapter.getLatestMarketPrice(MARKET_ID);906 PowerMock.verifyAll();907 }908 @Test(expected = TradingApiException.class)909 public void testSendingPublicRequestToExchangeHandlesTradingApiException() throws Exception {910 final BitstampExchangeAdapter exchangeAdapter =911 PowerMock.createPartialMockAndInvokeDefaultConstructor(912 BitstampExchangeAdapter.class, MOCKED_MAKE_NETWORK_REQUEST_METHOD);913 final URL url = new URL(API_BASE_URL + TICKER + MARKET_ID);914 PowerMock.expectPrivate(915 exchangeAdapter,916 MOCKED_MAKE_NETWORK_REQUEST_METHOD,917 eq(url),918 eq("GET"),919 eq(null),920 eq(new HashMap<>()))921 .andThrow(new TradingApiException("He’s holding a thermal detonator!"));922 PowerMock.replayAll();923 exchangeAdapter.init(exchangeConfig);924 exchangeAdapter.getLatestMarketPrice(MARKET_ID);925 PowerMock.verifyAll();926 }927 @Test928 @SuppressWarnings("unchecked")929 public void testSendingAuthenticatedRequestToExchangeSuccessfully() throws Exception {930 final byte[] encoded = Files.readAllBytes(Paths.get(SELL_JSON_RESPONSE));931 final AbstractExchangeAdapter.ExchangeHttpResponse exchangeResponse =932 new AbstractExchangeAdapter.ExchangeHttpResponse(933 200, "OK", new String(encoded, StandardCharsets.UTF_8));934 final Map<String, String> requestParamMap = PowerMock.createPartialMock(HashMap.class, "put");935 expect(936 requestParamMap.put(937 "price",938 new DecimalFormat("#.##", getDecimalFormatSymbols()).format(SELL_ORDER_PRICE)))939 .andStubReturn(null);940 expect(941 requestParamMap.put(942 "amount",943 new DecimalFormat("#.########", getDecimalFormatSymbols())944 .format(SELL_ORDER_QUANTITY)))945 .andStubReturn(null);946 expect(requestParamMap.put("type", "sell")).andStubReturn(null);947 expect(requestParamMap.put("key", "key123")).andStubReturn(null);948 expect(requestParamMap.put(eq("nonce"), anyString())).andStubReturn(null);949 expect(requestParamMap.put(eq("signature"), anyString())).andStubReturn(null);950 final Map<String, String> requestHeaderMap = PowerMock.createPartialMock(HashMap.class, "put");951 expect(requestHeaderMap.put("Content-Type", "application/x-www-form-urlencoded"))952 .andStubReturn(null);953 PowerMock.replay(requestHeaderMap); // map needs to be in play early954 final BitstampExchangeAdapter exchangeAdapter =955 PowerMock.createPartialMockAndInvokeDefaultConstructor(956 BitstampExchangeAdapter.class,957 MOCKED_MAKE_NETWORK_REQUEST_METHOD,958 MOCKED_CREATE_REQUEST_HEADER_MAP_METHOD,959 MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD);960 PowerMock.expectPrivate(exchangeAdapter, MOCKED_CREATE_REQUEST_HEADER_MAP_METHOD)961 .andReturn(requestHeaderMap);962 PowerMock.expectPrivate(exchangeAdapter, MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD)963 .andReturn(requestParamMap);964 final URL url = new URL(API_BASE_URL + SELL + MARKET_ID + "/");965 PowerMock.expectPrivate(966 exchangeAdapter,967 MOCKED_MAKE_NETWORK_REQUEST_METHOD,968 eq(url),969 eq("POST"),970 anyString(),971 eq(requestHeaderMap))972 .andReturn(exchangeResponse);973 PowerMock.replayAll();974 exchangeAdapter.init(exchangeConfig);975 final String orderId =976 exchangeAdapter.createOrder(977 MARKET_ID, OrderType.SELL, SELL_ORDER_QUANTITY, SELL_ORDER_PRICE);978 assertEquals("80890993", orderId);979 PowerMock.verifyAll();980 }981 @Test(expected = ExchangeNetworkException.class)982 @SuppressWarnings("unchecked")983 public void testSendingAuthenticatedRequestToExchangeHandlesExchangeNetworkException()984 throws Exception {985 final Map<String, String> requestParamMap = PowerMock.createPartialMock(HashMap.class, "put");986 expect(987 requestParamMap.put(988 "price",989 new DecimalFormat("#.##", getDecimalFormatSymbols()).format(SELL_ORDER_PRICE)))990 .andStubReturn(null);991 expect(992 requestParamMap.put(993 "amount",994 new DecimalFormat("#.########", getDecimalFormatSymbols())995 .format(SELL_ORDER_QUANTITY)))996 .andStubReturn(null);997 expect(requestParamMap.put("type", "sell")).andStubReturn(null);998 expect(requestParamMap.put("key", "key123")).andStubReturn(null);999 expect(requestParamMap.put(eq("nonce"), anyString())).andStubReturn(null);1000 expect(requestParamMap.put(eq("signature"), anyString())).andStubReturn(null);1001 final Map<String, String> requestHeaderMap = PowerMock.createPartialMock(HashMap.class, "put");1002 expect(requestHeaderMap.put("Content-Type", "application/x-www-form-urlencoded"))1003 .andStubReturn(null);1004 PowerMock.replay(requestHeaderMap); // map needs to be in play early1005 final BitstampExchangeAdapter exchangeAdapter =1006 PowerMock.createPartialMockAndInvokeDefaultConstructor(1007 BitstampExchangeAdapter.class,1008 MOCKED_MAKE_NETWORK_REQUEST_METHOD,1009 MOCKED_CREATE_REQUEST_HEADER_MAP_METHOD,1010 MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD);1011 PowerMock.expectPrivate(exchangeAdapter, MOCKED_CREATE_REQUEST_HEADER_MAP_METHOD)1012 .andReturn(requestHeaderMap);1013 PowerMock.expectPrivate(exchangeAdapter, MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD)1014 .andReturn(requestParamMap);1015 final URL url = new URL(API_BASE_URL + SELL + MARKET_ID + "/");1016 PowerMock.expectPrivate(1017 exchangeAdapter,1018 MOCKED_MAKE_NETWORK_REQUEST_METHOD,1019 eq(url),1020 eq("POST"),1021 anyString(),1022 eq(requestHeaderMap))1023 .andThrow(new ExchangeNetworkException("These aren’t the droids you’re looking for..."));1024 PowerMock.replayAll();1025 exchangeAdapter.init(exchangeConfig);1026 exchangeAdapter.createOrder(MARKET_ID, OrderType.SELL, SELL_ORDER_QUANTITY, SELL_ORDER_PRICE);1027 PowerMock.verifyAll();1028 }1029 @Test(expected = TradingApiException.class)1030 @SuppressWarnings("unchecked")1031 public void testSendingAuthenticatedRequestToExchangeHandlesTradingApiException()1032 throws Exception {1033 final Map<String, String> requestParamMap = PowerMock.createPartialMock(HashMap.class, "put");1034 expect(1035 requestParamMap.put(1036 "price",1037 new DecimalFormat("#.##", getDecimalFormatSymbols()).format(SELL_ORDER_PRICE)))1038 .andStubReturn(null);1039 expect(1040 requestParamMap.put(1041 "amount",1042 new DecimalFormat("#.########", getDecimalFormatSymbols())1043 .format(SELL_ORDER_QUANTITY)))1044 .andStubReturn(null);1045 expect(requestParamMap.put("type", "sell")).andStubReturn(null);1046 expect(requestParamMap.put("key", "key123")).andStubReturn(null);1047 expect(requestParamMap.put(eq("nonce"), anyString())).andStubReturn(null);1048 expect(requestParamMap.put(eq("signature"), anyString())).andStubReturn(null);1049 final Map<String, String> requestHeaderMap = PowerMock.createPartialMock(HashMap.class, "put");1050 expect(requestHeaderMap.put(eq("Content-Type"), eq("application/x-www-form-urlencoded")))1051 .andStubReturn(null);1052 PowerMock.replay(requestHeaderMap); // map needs to be in play early1053 final BitstampExchangeAdapter exchangeAdapter =1054 PowerMock.createPartialMockAndInvokeDefaultConstructor(1055 BitstampExchangeAdapter.class,1056 MOCKED_MAKE_NETWORK_REQUEST_METHOD,1057 MOCKED_CREATE_REQUEST_HEADER_MAP_METHOD,1058 MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD);1059 PowerMock.expectPrivate(exchangeAdapter, MOCKED_CREATE_REQUEST_HEADER_MAP_METHOD)1060 .andReturn(requestHeaderMap);1061 PowerMock.expectPrivate(exchangeAdapter, MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD)1062 .andReturn(requestParamMap);1063 final URL url = new URL(API_BASE_URL + SELL + MARKET_ID + "/");1064 PowerMock.expectPrivate(1065 exchangeAdapter,1066 MOCKED_MAKE_NETWORK_REQUEST_METHOD,1067 eq(url),1068 eq("POST"),...

Full Screen

Full Screen

Source:ResourceManagerTest.java Github

copy

Full Screen

...34 {35 super.setUp(); 36 37 PowerMock.mockStatic(ResourceManager.class);38 manager = PowerMock.createPartialMockAndInvokeDefaultConstructor(ResourceManager.class, new String[]{"getCurrentBundle"});39 40 currentBundle = PowerMock.createMock(ResourceBundle.class);41 EasyMock.expect(manager.getCurrentBundle()).andReturn(currentBundle).anyTimes();42 }4344 @Override45 protected void tearDown() throws Exception46 {47 super.tearDown();48 }49 50 public void testSetCurrentLocale()51 {52 currentBundle.setLocale("es_MX"); ...

Full Screen

Full Screen

Source:Log4jUserTest.java Github

copy

Full Screen

...16@MockPolicy(Log4jMockPolicy.class)17public class Log4jUserTest {18 @Test19 public void assertThatLog4jMockPolicyWorks() throws Exception {20 final Log4jUser tested = createPartialMockAndInvokeDefaultConstructor(Log4jUser.class, "getMessage");21 final String otherMessage = "other message";22 final String firstMessage = "first message and ";23 expect(tested.getMessage()).andReturn(firstMessage);24 replayAll();25 final String actual = tested.mergeMessageWith(otherMessage);26 Class<? extends Logger> class1 = Whitebox.getInternalState(Log4jUserParent.class, Logger.class).getClass();27 assertTrue(class1.getName().contains("org.apache.log4j.Logger$$EnhancerByCGLIB$$"));28 verifyAll();29 assertEquals(firstMessage + otherMessage, actual);30 }31}...

Full Screen

Full Screen

createPartialMockAndInvokeDefaultConstructor

Using AI Code Generation

copy

Full Screen

1import org.powermock.api.easymock.PowerMock;2import org.powermock.api.easymock.annotation.Mock;3import org.powermock.api.easymock.annotation.MockNice;4import org.powermock.api.easymock.annotation.MockStrict;5import org.powermock.api.easymock.annotation.MockUp;6import org.powermock.api.easymock.annotation.Mocked;7import org.powermock.api.easymock.annotation.Tested;8import org.powermock.api.easymock.annotation.UsingMocksAndStubs;9import org.powermock.api.easymock.annotation.UsingMocksAndStubs;10import org.powermock.api.easymock.internal.expectation.ConstructorExpectation;11import org.powermock.api.easymock.internal.expectation.ConstructorExpectationBuilder;12import org.powermock.api.easimock.PowerMock;13import org.powermock.api.easimock.annotation.Mock;14import org.powermock.api.easimock.annotation.MockNice;15import org.powermock.api.easimock.annotation.MockStrict;16import org.powermock.api.easimock.annotation.MockUp;17import org.powermock.api.easimock.annotation.Mocked;18import org.powermock.api.easimock.annotation.Tested;19import org.powermock.api.easimock.annotation.UsingMocksAndStubs;20import org.powermock.api.easimock.annotation.UsingMocksAndStubs;21import org.powermock.api.easimock.internal.expectation.ConstructorExpectation;22import org.powermock.api.easimock.internal.expectation.ConstructorExpectationBuilder;23import org.powermock.core.classloader.annotations.PrepareForTest;24import org.powermock.core.classloader.annotations.PrepareForTest;25import org.powermock.modules.junit4.PowerMockRunner;26import org.powermock.modules.junit4.PowerMockRunner;27import org.powermock.modules.junit4.PowerMockRunnerDelegate;

Full Screen

Full Screen

createPartialMockAndInvokeDefaultConstructor

Using AI Code Generation

copy

Full Screen

1package org.powermock.examples.tutorial.partialmocking;2import org.junit.Test;3import org.junit.runner.RunWith;4import org.powermock.api.easymock.annotation.MockNice;5import org.powermock.core.classloader.annotations.PrepareForTest;6import org.powermock.modules.junit4.PowerMockRunner;7import static org.easymock.EasyMock.expect;8import static org.junit.Assert.assertEquals;9import static org.powermock.api.easymock.PowerMock.*;10@RunWith(PowerMockRunner.class)11@PrepareForTest( { PartialMockingExample.class })12public class PartialMockingExampleTest {13 private Collaborator collaborator;14 public void testPartialMocking() throws Exception {15 PartialMockingExample test = createPartialMockAndInvokeDefaultConstructor(PartialMockingExample.class, "methodToMock");16 expectPrivate(test, "methodToMock", "Hello EasyMock").andReturn("Bye EasyMock");17 replayAll();18 String result = test.sayHello("Hello EasyMock");19 assertEquals("Bye EasyMock", result);20 verifyAll();21 }22}23package org.powermock.examples.tutorial.partialmocking;24import org.junit.Test;25import org.junit.runner.RunWith;26import org.powermock.api.easymock.annotation.MockNice;27import org.powermock.core.classloader.annotations.PrepareForTest;28import org.powermock.modules.junit4.PowerMockRunner;29import static org.easymock.EasyMock.expect;30import static org.junit.Assert.assertEquals;31import static org.powermock.api.easymock.PowerMock.*;32@RunWith(PowerMockRunner.class)33@PrepareForTest( { PartialMockingExample.class })34public class PartialMockingExampleTest {35 private Collaborator collaborator;36 public void testPartialMocking() throws Exception {37 PartialMockingExample test = createPartialMockAndInvokeDefaultConstructor(PartialMockingExample.class, "methodToMock");

Full Screen

Full Screen

createPartialMockAndInvokeDefaultConstructor

Using AI Code Generation

copy

Full Screen

1package org.powermock.examples.tutorial.partialmocking;2import junit.framework.Assert;3import org.junit.Test;4import org.junit.runner.RunWith;5import org.powermock.api.easymock.PowerMock;6import org.powermock.core.classloader.annotations.PrepareForTest;7import org.powermock.modules.junit4.PowerMockRunner;8import java.util.ArrayList;9import static org.powermock.api.easymock.PowerMock.*;10@RunWith(PowerMockRunner.class)11@PrepareForTest({PartialMockingExample.class, ArrayList.class})12public class PartialMockingExampleTest {13 public void testPartialMocking() throws Exception {14 ArrayList<String> mockedList = PowerMock.createPartialMockAndInvokeDefaultConstructor(ArrayList.class, "add");15 PowerMock.expectPrivate(mockedList, "add", "test").andReturn(true);16 PowerMock.replay(mockedList);17 Assert.assertTrue(mockedList.add("test"));18 PowerMock.verify(mockedList);19 }20}21package org.powermock.examples.tutorial.partialmocking;22import junit.framework.Assert;23import org.junit.Test;24import org.junit.runner.RunWith;25import org.powermock.api.easymock.PowerMock;26import org.powermock.core.classloader.annotations.PrepareForTest;27import org.powermock.modules.junit4.PowerMockRunner;28import java.util.ArrayList;29import static org.powermock.api.easymock.PowerMock.*;30@RunWith(PowerMockRunner.class)31@PrepareForTest({PartialMockingExample.class, ArrayList.class})32public class PartialMockingExampleTest {33 public void testPartialMocking() throws Exception {34 ArrayList<String> mockedList = PowerMock.createPartialMockAndInvokeDefaultConstructor(ArrayList.class, "add");35 PowerMock.expectPrivate(mockedList, "add", "test").andReturn(true);36 PowerMock.replay(mockedList);37 Assert.assertTrue(mockedList.add("test"));38 PowerMock.verify(mockedList);39 }40}41package org.powermock.examples.tutorial.partialmocking;42import junit.framework.Assert;43import org.junit.Test;44import org.junit.runner.RunWith;45import org.powermock.api.easymock.PowerMock;46import org.powermock.core.classloader.annotations.PrepareForTest;

Full Screen

Full Screen

createPartialMockAndInvokeDefaultConstructor

Using AI Code Generation

copy

Full Screen

1package org.powermock.examples.tutorial.partialmocking;2import junit.framework.Assert;3import org.junit.Test;4import org.junit.runner.RunWith;5import org.powermock.api.easymock.PowerMock;6import org.powermock.core.classloader.annotations.PrepareForTest;7import org.powermock.modules.junit4.PowerMockRunner;8import java.util.ArrayList;9import static org.powermock.api.easymock.PowerMock.*;10@RunWith(PowerMockRunner.class)11@PrepareForTest({PartialMockingExample.class, ArrayList.class})12public class PartialMockingExampleTest {13 public void testPartialMocking() throws Exception {14 ArrayList<String> mockedList = PowerMock.createPartialMockAndInvokeDefaultConstructor(ArrayList.class, "add");15 PowerMock.expectPrivate(mockedList, "add", "test").andReturn(true);16 PowerMock.replay(mockedList);17 Assert.assertTrue(mockedList.add("test"));18 PowerMock.verify(mockedList);19 }20}21package org.powermock.examples.tutorial.partialmocking;22import junit.framework.Assert;23import org.junit.Test;24import org.junit.runner.RunWith;25import org.powermock.api.easymock.PowerMock;26import org.powermock.core.classloader.annotations.PrepareForTest;27import org.powermock.modules.junit4.PowerMockRunner;28import java.util.ArrayList;29import static org.powermock.api.easymock.PowerMock.*;30@RunWith(PowerMockRunner.class)31@PrepareForTest({PartialMockingExample.class, ArrayList.class})32public class PartialMockingExampleTest {33 public void testPartialMocking() throws Exception {34 ArrayList<String> mockedList = PowerMock.createPartialMockAndInvokeDefaultConstructor(ArrayList.class, "add");35 PowerMock.expectPrivate(mockedList, "add", "test").andReturn(true);36 PowerMock.replay(mockedList);37 Assert.assertTrue(mockedList.add("test"));38 PowerMock.verify(mockedList);39 }40}41package org.powermock.examples.tutorial.partialmocking;42import junit.framework.Assert;43import org.junit.Test;44import org.junit.runner.RunWith;45import org.powermock.api.easymock.PowerMock;46import org.powermock.core.classloader.annotations.PrepareForTest;

Full Screen

Full Screen

createPartialMockAndInvokeDefaultConstructor

Using AI Code Generation

copy

Full Screen

1package com.example;2import static org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor;3import static org.powermock.api.easymock.PowerMock.expectPrivate;4import org.easymock.EasyMock;5import org.junit.Test;6public class ExampleTest {7 public void test() throws Exception {8 Example example = createPartialMockAndInvokeDefaultConstructor(Example.class, "privateMethod");9 expectPrivate(example, "privateMethod", EasyMock.anyInt()).andReturn(true);10 }11}12package com.example;13import static org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor;14import static org.powermock.api.easymock.PowerMock.expectPrivate;15import org.easymock.EasyMock;16import org.junit.Test;17public class ExampleTest {18 public void test() throws Exception {19 Example example = createPartialMockAndInvokeDefaultConstructor(Example.class, "privateMethod");20 expectPrivate(example, "privateMethod", EasyMock.anyInt()).andReturn(true);21 }22}23package com.example;24import static org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor;25import static org.powermock.api.easymock.PowerMock.expectPrivate;26import org.easymock.EasyMock;27import org.junit.Test;28public class ExampleTest {29 public void test() throws Exception {30 Example example = createPartialMockAndInvokeDefaultConstructor(Example.class, "privateMethod");31 expectPrivate(example, "privateMethod", EasyMock.anyInt()).andReturn(true);32 }33}34package com.example;35import static org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor;36import static org.powermock.api.easymock.PowerMock.expectPrivate;37import org.easymock.EasyMock;38import org.junit.Test;39public class ExampleTest {40 public void test() throws Exception {

Full Screen

Full Screen

createPartialMockAndInvokeDefaultConstructor

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.powermock.api.easymock.PowerMock;3import static org.junit.Assert.assertEquals;4public class TestClass {5 public void test() {6 TestClass testClass = PowerMock.createPartialMockAndInvokeDefaultConstructor(TestClass.class, "testMethod");7 assertEquals("Test", testClass.testMethod());8 }9 public String testMethod() {10 return "Test";11 }12}13BUILD SUCCESSFUL (total time: 0 seconds)

Full Screen

Full Screen

createPartialMockAndInvokeDefaultConstructor

Using AI Code Generation

copy

Full Screen

1package com.powermock;2import java.io.IOException;3import java.io.InputStream;4import org.powermock.api.easymock.PowerMock;5public class PowerMockExample {6 public static void main(String[] args) throws IOException {7 InputStream inputStream = PowerMock.createPartialMockAndInvokeDefaultConstructor(InputStream.class, "read");8 PowerMock.expectPrivate(inputStream, "read").andReturn(2);9 PowerMock.replay(inputStream);10 System.out.println(inputStream.read());11 }12}13This is a guide to PowerMock.createPartialMockAndInvokeDefaultPonstructor(). Here we discuss how to use the PowerMock.cartialMockAndockAndInvokeDefaultConstructor() method to create a partial mInv and invoke the default constructor of the class along with the examples. You may also have a look at the following articles to learn more –14PowerMock.createPartialMockAndInvokeConstructor() Method in PowerMock15PowerMock.createPartialMockAndInvokeSuperConstructor() Method in PowerMock16PowerMock.createPartialMockAndExpectNew() Method in PowerMock17PowerMock.createPartialMock() Method in PowerMock18PowerMock.createMock() Method in PowerMock19PowerMock.verify() Method in PowerMock20PowerMock.verifyAll() Method in PowerMock21PowerMock.verifyNoMoreInteractions() Method in PowerMock22PowerMock.expectPrivate() Method in PowerMock23PowerMock.expectLastCall() Method in PowerMock24PowerMock.expectNew() Method in PowerMock25PowerMock.expect() Method in PowerMock26PowerMock.expectAndReturn() Method in PowerMock27PowerMock.expectAndThrow() Method in PowerMock28PowerMock.expectAndDelegateTo() Method in PowerMock29PowerMock.expectAndStub()okeDefaultConstructor method of org.powermock.api.easymock.PowerMock class30package com.example;31import static org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor;32import org.easymock.EasyMockSupport;33import org.junit.Test;34public class ExampleTest extends EasyMockSupport {35 public void testCreatePartialMockAndInvokeDefaultConstructor() throws Exception {36 Example mock = createPartialMockAndInvokeDefaultConstructor(Example.class);37 }38}39package com.example;40import static org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor;41import org.easymock.EasyMockSupport;42import org.junit.Test;43public class ExampleTest extends EasyMockSupport {44 public void testCreatePartialMockAndInvokeDefaultConstructor() throws Exception {45 Example mock = createPartialMockAndInvokeDefaultConstructor(Example.class);46 }47}48package com.example;49import static org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor;50import org.easymock.EasyMockSupport;51import org.junit.Test;52public class ExampleTest extends EasyMockSupport {53 public void testCreatePartialMockAndInvokeDefaultConstructor() throws Exception {54 Example mock = createPartialMockAndInvokeDefaultConstructor(Example.class);55 }56}57package com.example;58import static org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor;59import org.easymock.EasyMockSupport;60import org.junit.Test;61public class ExampleTest extends EasyMockSupport {

Full Screen

Full Screen

createPartialMockAndInvokeDefaultConstructor

Using AI Code Generation

copy

Full Screen

1package com.powermock;2import java.io.IOException;3import java.io.InputStream;4import org.powermock.api.easymock.PowerMock;5public class PowerMockExample {6 public static void main(String[] args) throws IOException {7 InputStream inputStream = PowerMock.createPartialMockAndInvokeDefaultConstructor(InputStream.class, "read");8 PowerMock.expectPrivate(inputStream, "read").andReturn(2);9 PowerMock.replay(inputStream);10 System.out.println(inputStream.read());11 }12}13This is a guide to PowerMock.createPartialMockAndInvokeDefaultConstructor(). Here we discuss how to use the PowerMock.createPartialMockAndInvokeDefaultConstructor() method to create a partial mock and invoke the default constructor of the class along with the examples. You may also have a look at the following articles to learn more –14PowerMock.createPartialMockAndInvokeConstructor() Method in PowerMock15PowerMock.createPartialMockAndInvokeSuperConstructor() Method in PowerMock16PowerMock.createPartialMockAndExpectNew() Method in PowerMock17PowerMock.createPartialMock() Method in PowerMock18PowerMock.createMock() Method in PowerMock19PowerMock.verify() Method in PowerMock20PowerMock.verifyAll() Method in PowerMock21PowerMock.verifyNoMoreInteractions() Method in PowerMock22PowerMock.expectPrivate() Method in PowerMock23PowerMock.expectLastCall() Method in PowerMock24PowerMock.expectNew() Method in PowerMock25PowerMock.expect() Method in PowerMock26PowerMock.expectAndReturn() Method in PowerMock27PowerMock.expectAndThrow() Method in PowerMock28PowerMock.expectAndDelegateTo() Method in PowerMock29PowerMock.expectAndStub()

Full Screen

Full Screen

createPartialMockAndInvokeDefaultConstructor

Using AI Code Generation

copy

Full Screen

1public class 4 extends TestCase {2 public void test4() throws Exception {3 4 mock = PowerMock.createPartialMockAndInvokeDefaultConstructor(4.class, "method1", "method2");4 mock.method1();5 mock.method2();6 PowerMock.verifyAll();7 }8}9public class 5 extends TestCase {10 public void test5() throws Exception {11 5 mock = PowerMock.createPartialMockAndInvokeDefaultConstructor(5.class, "method1", "method2");12 mock.method1();13 mock.method2();14 PowerMock.verifyAll();15 }16}17public class 6 extends TestCase {18 public void test6() throws Exception {19 6 mock = PowerMock.createPartialMockAndInvokeDefaultConstructor(6.class, "method1", "method2");20 mock.method1();21 mock.method2();22 PowerMock.verifyAll();23 }24}25public class 7 extends TestCase {26 public void test7() throws Exception {27 7 mock = PowerMock.createPartialMockAndInvokeDefaultConstructor(7.class, "method1", "method2");28 mock.method1();29 mock.method2();30 PowerMock.verifyAll();31 }32}

Full Screen

Full Screen

Automation Testing Tutorials

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

LambdaTest Learning Hubs:

YouTube

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful