Movie Store – Part 2
Test-Driven Development: write tests before writing code.
Unit testing results in modular code. Modular code is:
- easy to test.
- immune to bugs.
- easy to understand.
- scalable.
Task 1: @Before
@Before
runs a method before each test.
- Create a
void
method namedsetup
, and annotate it with@Before
. - Inside the function, set
store
equal to a new object of theStore
class.
Task 2: Unit Testing
1. Create a test that fails
Inside @Before
, add two Movie
objects.
store.addMovie(new Movie("The Shawshank Redemption", "Blue-Ray", 9.2)); store.addMovie(new Movie("The Godfather", "Blue-Ray", 9.1));
Inside Store.java
, create an addMovie
method.
/** * Function name: addMovie * @param movie * * Inside the function: * 1. adds a movie object */
Write a unit test named moviedAdded
. Inside, use assertTrue
to assert the store
.contains(new Movie("The Godfather", "Blue-Ray", 9.1)
.
Inside Store.java
, write code to make the test fail.
/** * Function name: contains * @param movie * @return (boolean) * * Inside the function: * 1. checks if movies list contains() movie. */
2. Make the test pass
Hint: contains()
needs your equals
method to effectively compare Movie
objects. See lesson: the equals()
method.
public boolean equals(Object obj) {
// return false if parameter is null.
// return false if parameter isn't instance of Movie class.
// typecast the object to Movie.
// compare fields from both objects and return the result.
}
3. Refactor
Can the code be simpler? No. Refactoring complete.
Good Luck
Feedback Summary
4.7
43 students
5
91%
4
2%
3
0%
2
0%
1
7%
Written Reviews
There are no written reviews yet.