By default, Mockito, a mocking framework, is also included in the dependencies, and it has a nice Spring integration as well.
You just add the runner by using the @RunWith annotation, and then you can define the class you want to test as a field and mark it with the @InjectMocks annotation. All dependencies that you want to use can be added with the @Mock annotation. They will be recreated for each test and contain a mocked version of the class. In your test, you can define the behavior for calls by using Mockito.when(...). You can also verify that, for example, certain calls have been made with given parameters. This is done using the Mockito.verify(...) methods. The injection works as it does with Spring, so when Spring is able to autowire a dependency, Mockito will very likely be able to as well:
@RunWith(MockitoJUnitRunner.class)
public class BlogRepositorySpringTest {
@Mock
List<BlogEntry> db;
@InjectMocks
BlogRepository repository;
@Test
public void testAdd(){
BlogEntry entry = new BlogEntry();
repository.add(entry);
Mockito.verify(db).add(eq(entry));
}
}
For detailed information regarding Mockito, please check out its website at http://site.mockito.org/.