Unit Testing in Spring

Learn how to write two simple unit tests using the JUnit framework to test a method.

When writing tests, the foremost thing to remember is that the test code should always be separate from the production code. We will write all our tests in src/test/java. This will ensure that the test code is never part of the deployable jar or war and stays in our repository.

We will now write unit tests for the RecommenderImplementation class created earlier in this course. The code of the class is reproduced below:

Press + to interact
@Component
public class RecommenderImplementation {
@Autowired
private Filter filter;
public RecommenderImplementation(Filter filter) {
super();
this.filter = filter;
}
//use a filter to find recommendations
public String [] recommendMovies (String movie) {
String[] results = filter.getRecommendations(movie);
return results;
}
}

This class has a dependency on the Filter interface, which has two implementations, ContentBasedFilter and CollaborativeFilter ...