Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:16
Show Gist options
  • Save up1/d253854255cac8095cc9 to your computer and use it in GitHub Desktop.
Save up1/d253854255cac8095cc9 to your computer and use it in GitHub Desktop.
Unit Test :: Demo
public class Money {
private int amount;
private String currency;
public Money(int amount, String currency) {
this.amount = amount;
this.currency = currency;
}
public int amount() {
return amount;
}
public String currency() {
return currency;
}
}
public class Money {
private int amount;
private String currency;
public Money(int amount, String currency) {
this.amount = amount;
this.currency = currency;
}
public int amount() {
return amount;
}
public String currency() {
return currency;
}
public Money add(Money money) {
return new Money(amount() + money.amount(), currency());
}
}
public class Money {
...
public boolean equals(Object anObject) {
if (anObject instanceof Money) {
Money money = (Money) anObject;
return money.currency().equals(currency()) && amount() == money.amount();
}
return false;
}
}
public class MoneyTest {
@Test
public void simpleAddWithSameCurrency() {
//Arrange
Money money10THB = new Money(10, "THB");
Money money5THB = new Money(5, "THB");
Money expectedResult = new Money(15, "THB");
//Act
Money actualResult = money10THB.add(money5THB);
//Assert
assertTrue(expectedResult.equals(actualResult));
}
}
public class MoneyTest {
@Test
public void equalsMoney() throws Exception {
Money money10THB = new Money(10, "THB");
Money money5THB = new Money(5, "THB");
assertTrue(!money10THB.equals(null));
assertEquals(money10THB, money10THB);
assertEquals(money10THB, new Money(10, "THB"));
assertTrue(!money10THB.equals(money5THB));
}
}
public class MoneyTest {
private Money money10THB;
private Money money5THB;
@Before
public void setupFixture() {
money10THB = new Money(10, "THB");
money5THB = new Money(5, "THB");
}
@Test
public void simpleAddWithSameCurrency() {
Money expectedResult = new Money(15, "THB");
Money actualResult = money10THB.add(money5THB);
assertTrue(expectedResult.equals(actualResult));
}
@Test
public void equalsMoney() throws Exception {
assertTrue(!money10THB.equals(null));
assertEquals(money10THB, money10THB);
assertEquals(money10THB, new Money(10, "THB"));
assertTrue(!money10THB.equals(money5THB));
}
}
@RunWith(Suite.class)
@SuiteClasses({ MoneyTest.class })
public class Group01 {
}
@RunWith(Suite.class)
@SuiteClasses({ MoneyTest2.class, MoneyTest3.class })
public class Group02 {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment