Last active
August 29, 2015 14:16
-
-
Save up1/d253854255cac8095cc9 to your computer and use it in GitHub Desktop.
Unit Test :: Demo
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
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; | |
} | |
} |
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
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()); | |
} | |
} |
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
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; | |
} | |
} |
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
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)); | |
} | |
} |
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
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)); | |
} | |
} |
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
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)); | |
} | |
} |
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
@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