Skip to content

Instantly share code, notes, and snippets.

@press0
Last active February 15, 2021 14:32
Show Gist options
  • Save press0/5ff41513cb7f81212b1cae22f66a6be7 to your computer and use it in GitHub Desktop.
Save press0/5ff41513cb7f81212b1cae22f66a6be7 to your computer and use it in GitHub Desktop.
TradeExecutorJUnit4Test
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