Skip to content

Commit

Permalink
springframeworkguru#21 - New Currency "Franc" - Basic Test Driven Dev…
Browse files Browse the repository at this point in the history
…elopment approach, Write test first, make it compile and then flesh it out
  • Loading branch information
patrickcorbett committed Apr 22, 2024
1 parent 19ad9c6 commit ec361e2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/main/java/guru/springframework/Franc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package guru.springframework;

/**
* @author Patrick Corbett
*/
public class Franc {

private int amount;

public Franc(int pAmount) {
amount = pAmount;
}

Franc times(int pMultiplier) {
return new Franc(amount * pMultiplier);
}

public boolean equals(Object object) {
// Manual equals, left like this as equals with null or other class will be done later
Franc franc = (Franc) object;
return amount == franc.amount;
}

}
26 changes: 22 additions & 4 deletions src/test/java/guru/springframework/MoneyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

/**
* @author Patrick Corbett
*/
Expand All @@ -12,15 +15,30 @@ class MoneyTest {
void testMultiplication() {
Dollar five = new Dollar(5);
Dollar product = five.times(2);
Assertions.assertEquals(new Dollar(10), product);
assertEquals(new Dollar(10), product);
product = five.times(3);
Assertions.assertEquals(new Dollar(15), product);
assertEquals(new Dollar(15), product);
}

@Test
void testEquality() {
Assertions.assertEquals(new Dollar(5), new Dollar(5));
Assertions.assertNotEquals(new Dollar(5), new Dollar(8));
assertEquals(new Dollar(5), new Dollar(5));
assertNotEquals(new Dollar(5), new Dollar(8));
}

@Test
void testMultiplicationFranc() {
Franc five = new Franc(5);
Franc product = five.times(2);
assertEquals(new Franc(10), product);
product = five.times(3);
assertEquals(new Franc(15), product);
}

@Test
void testEqualityFranc() {
assertEquals(new Franc(5), new Franc(5));
assertNotEquals(new Franc(5), new Franc(8));
}

}

0 comments on commit ec361e2

Please sign in to comment.