Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
In order to preserve the "unit" in unit testing, often we need to mock, or fake, the functionality of certain objects. There are several popular libraries out there to choose from. We'll be using Mockito, so this video shows you how to get started with Mockito.
Sync Your Code to the Start of this Video
git checkout -f v4
Using Git with this Workshop
See the README on Github for information on how to use Git with this repo to follow along with this workshop.
Alternate Approaches to Setting Up Mockito
Without the MockitoJUnitRunner
If you want to run your unit tests without the MockitoJUnitRunner
, you can delete the @RunWith
annotation from the class. Then, you'll need to tell Mockito to initialize the mocks according to your @InjectMocks
and @Mock
annotations:
public class FavoriteControllerTest {
private MockMvc mockMvc;
@InjectMocks
private FavoriteController controller;
@Mock
private FavoriteService service;
@Before
public void setup() {
MockitoAnnotations.initMocks(this); // Initialize according to annotations
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
}
Construct Mocks Yourself
If you'd like to construct and initialize the mocks yourself, you have options. Here is one way to do this:
public class FavoriteControllerTest {
private MockMvc mockMvc;
private FavoriteController controller;
private FavoriteService service;
@Before
public void setup() {
// Construct the service mock
service = Mockito.mock(FavoriteService.class);
controller = new FavoriteController(service,null,null);
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
}
Of course, this will require a constructor in the FavoriteController
class. Because we don't want to break Spring's dependency injection, you can mark this constructor as @Autowired
, which means that any parameter values will be injected according to available beans. This is a nice, test-friendly alternative to autowired fields, which require reflection for DI. Here's the relevant part of the FavoriteController
class:
@Controller
public class FavoriteController {
// @Autowired annotations removed from fields
private FavoriteService favoriteService;
private PlacesService placesService;
private WeatherService weatherService;
@Autowired // Added for Spring DI
public FavoriteController(
FavoriteService favoriteService,
PlacesService placesService,
WeatherService weatherService) {
this.favoriteService = favoriteService;
this.placesService = placesService;
this.weatherService = weatherService;
}
}
Overview of Mocking
Wikipedia has a nice explanation of what mock objects are: https://en.wikipedia.org/wiki/Mock_object
Here is a nice supplement to our mocking work, still within the context of Spring: https://dzone.com/articles/mocking-in-unit-tests
Popular Mocking Libraries
You need to sign up for Treehouse in order to download course files.
Sign up