Last active
February 15, 2021 14:32
-
-
Save press0/5ff41513cb7f81212b1cae22f66a6be7 to your computer and use it in GitHub Desktop.
TradeExecutorJUnit4Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package trp2.redonejunit4; | |
import static org.junit.Assert.assertTrue; | |
import static org.junit.Assert.fail; | |
import org.junit.Test; | |
public class TradeExecutorJUnit4Test { | |
@Test | |
public void test_exception_for_null_trade_junit4_1() { | |
//assemble | |
TradeSender sender = trade -> false; | |
TradeExecutor tradeExecutor = new TradeExecutor(sender); | |
Trade nullTrade = null; | |
//act | |
try { | |
tradeExecutor.executeTrade(nullTrade); | |
fail("exception should have occurred"); | |
} catch (Exception e) { | |
//assert | |
assertTrue(e.getMessage().contains("Invalid trade")); | |
} | |
} | |
@Test(expected = Exception.class) | |
public void test_exception_for_null_trade_junit4_2() throws Exception { | |
//assemble | |
TradeSender sender = trade -> false; | |
TradeExecutor tradeExecutor = new TradeExecutor(sender); | |
Trade nullTrade = null; | |
//act | |
tradeExecutor.executeTrade(nullTrade); | |
//assert | |
fail("should not be here") | |
} | |
} | |
interface TradeSender { | |
boolean sendTrade(Trade trade); | |
} | |
class TradeExecutor { | |
private TradeSender sender; | |
public TradeExecutor(TradeSender sender) { | |
this.sender = sender; | |
} | |
public boolean executeTrade(Trade trade) throws Exception { | |
if (trade == null) | |
throw new Exception("Invalid trade"); | |
if (trade.getQuantity() % 100 != 0) | |
return false; | |
return this.sender.sendTrade(trade); | |
} | |
} | |
class Trade { | |
public int getQuantity() { | |
return 0; | |
} | |
public Security getSecurity() { | |
return null; | |
} | |
public Account getAccount() { | |
return null; | |
} | |
} | |
class Account { | |
} | |
class Security { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment