Skip to content

Instantly share code, notes, and snippets.

@press0
Created February 10, 2021 19:10
Show Gist options
  • Save press0/fa8283898b4c401b70420d025a7609b8 to your computer and use it in GitHub Desktop.
Save press0/fa8283898b4c401b70420d025a7609b8 to your computer and use it in GitHub Desktop.
TradeExecutorJUnit5Test
package trp2.redonejunit5;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class TradeExecutorJUnit5Test {
@Test
public void test_exception_for_null_trade_junit5() {
//assemble
TradeSender sender = trade -> false;
TradeExecutor tradeExecutor = new TradeExecutor(sender);
Trade nullTrade = null;
//act
Exception exception = assertThrows(Exception.class, () -> tradeExecutor.executeTrade(nullTrade));
//assert
String expectedMessage = "Invalid trade";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
}
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