Lab03 repository
Goal¶
Have a way of saving and finding orders in memory, behind a single interface.
Important
Be sure to skim through all of the steps before getting started.
A. Give CoffeeOrder an Identity¶
Make the CoffeeOrder an Entity.
-
Add a
Long id
private member variable to theCoffeeOrder
class.Important
Make sure you create the member variable exactly: its type is
Long
and its name isid
(all lower-case). -
Create a getter & setter for the
id
property.- Use IntelliJ to generate the getter & setter using
⌘ + N
- Use IntelliJ to generate the getter & setter using
B. Create Repository Interface¶
Create a new interface named CoffeeOrderRepository
(using New > Java Class
from the File
menu)
Add the following code:
public interface CoffeeOrderRepository {
CoffeeOrder findOne(Long id);
CoffeeOrder save(CoffeeOrder coffeeOrder);
List<CoffeeOrder> findAll();
}
C. Create Empty Implementation¶
-
Create a new class named
FakeCoffeeOrderRepository
that implements theCoffeeOrderRepository
interface and start with the following code:public class FakeCoffeeOrderRepository implements CoffeeOrderRepository { public FakeCoffeeOrderRepository() {} public FakeCoffeeOrderRepository(CoffeeOrder... coffeeOrders) { // You can iterate through the coffeeOrders array like this: for (CoffeeOrder coffeeOrder : coffeeOrders) { // process each coffeeOrder here } // You can also convert it directly to a List: // List<CoffeeOrder> coffeeOrderList = Arrays.asList(coffeeOrders); // See: https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...- } public CoffeeOrder findOne(Long id) { throw new UnsupportedOperationException(); } public CoffeeOrder save(CoffeeOrder coffeeOrder) { throw new UnsupportedOperationException(); } public List<CoffeeOrder> findAll() { } }
D. FindAll Implementation¶
Keep Method Signatures
You are not allowed to change any of the method signatures that are defined in the CoffeeOrderRepository
. You must abide by this defined interface.
-
Create a new Test Class called
CoffeeOrderRepositoryFindTest
-
For each of the following, write a failing test, then make that test pass:
-
If
findAll()
is called and there are no orders, it must return an emptyList
-
If there are orders,
findAll()
must return all of them in aList
- Use this test to get you started:
@Test public void findAllShouldReturn2Orders() { CoffeeOrder coffeeOrder1 = new CoffeeOrder(); coffeeOrder1.setId(1L); CoffeeOrder coffeeOrder2 = new CoffeeOrder(); coffeeOrder2.setId(2L); CoffeeOrderRepository repo = new FakeCoffeeOrderRepository(coffeeOrder1, coffeeOrder2); assertThat(repo.findAll()) .hasSize(2); }
- Use this test to get you started:
-
Make sure all tests pass before continuing.
-
E. Find One Implementation¶
Which Collection Class?
What is appropriate Java Collection class to use to store the coffeeOrders so that it's easy to find CoffeeOrder
's by their id
?
-
Open the
CoffeeOrderRepositoryFindTest
test class -
Implement the
findOne()
method (remember to write failing tests first):- If there's no
CoffeeOrder
with thatid
, returnnull
. - If there exists a
CoffeeOrder
with the givenid
, return it - Make sure all tests pass before continuing.
- If there's no
F. Save Implementation¶
-
Create a new test class named
CoffeeOrderRepositorySaveTest
-
Implement the
save()
method, which stores the given order in the repository so it can be found later.-
IF the incoming coffeeOrder object's
id
property is ALREADY set, DON'T change theid
, just store the order (replacing the existing instance, just like an "update" might). -
IF the coffeeOrder came in with NO
id
(null
), SET it with a UNIQUE one that the repository generatesUnique?
How will you guarantee that each ID is unique?
-
Return the
CoffeeOrder
object that must now have its id set (i.e.,id
must NOT benull
)
-
Here's a test to use to ensure that each newly saved CoffeeOrder is given an ID that's different from other newly saved CoffeeOrders.
@Test
public void newlySavedCoffeeOrdersHaveUniqueIds() {
CoffeeOrderRepository coffeeOrderRepository = new FakeCoffeeOrderRepository();
CoffeeOrder coffeeOrder1 = new CoffeeOrder();
coffeeOrder1 = coffeeOrderRepository.save(coffeeOrder1);
CoffeeOrder coffeeOrder2 = new CoffeeOrder();
coffeeOrder2 = coffeeOrderRepository.save(coffeeOrder2);
assertThat(coffeeOrder1.getId())
.isNotEqualTo(coffeeOrder2.getId());
}
Once you've completed the above steps,
check in with the instructor to review your code.
Optional¶
-
Implement the "delete" functionality by adding the following method to the
CoffeeOrderRepository
interface:void delete(CoffeeOrder coffeeOrder);
-
Implement the following behavior in your
FakeCoffeeOrderRepository
, remember to write a failing test first:
NOTE: Put tests for
delete
into a new Test class namedCoffeeOrderRepositoryDeleteTest
-
Deleting a coffeeOrder means that trying to find it by its
id
will returnnull
-- as if the coffeeOrder was never saved.NOTE: Don't modify the coffeeOrder object that's being deleted -- it will continue to have the id that was assigned upon
save
-
If
null
reference for theCoffeeOrder
is passed in, throw anIllegalArgumentException
-
If a
CoffeeOrder
with anid
that's not found is passed in, throw anIllegalArgumentException
Once you've completed the above steps,
check in with the instructor to review your code.
References¶
Article: "Mocks Aren't Stubs" by Martin Fowler
Book: xUnit Test Patterns by Gerard Meszaros