Take the 2-minute tour ×

I want to write unit test using Junit and Mockito, in this case I don't want to write integration tests. The method that I want to test uses variables that are injected via Spring using the @Value or @Autowired annotations. How can I populate the injected variables so that when I run a test they are not null. Before days of annotations I would have created mocked classes of the variables and set them via setter methods.

I'm writing unit tests so I would prefer not to use @RunWith(SpringJUnit4ClassRunner.class).

share|improve this question
add comment

1 Answer

You can use the MockitoJunitRunner:

class SystemUnderTest {
    private Dependency dep;

    // ...

public class YourTest {
    private Dependency mockDependency;

    private final SystemUnderTest testee = new SystemUnderTest();

    public void setup() {
        mockDependency = Mockito.mock(Dependency);
        // etc.

    public void testSystem() {
        // at this pint testee already injected with mockDependency
share|improve this answer
Thanks, what is the SutsDependency referring to? –  user86834 Jun 8 '13 at 17:16
Whoops, fixed; this was supposed to mean "SUT's dependency" but then I thought it wouldn't add much but forgot to change all occurences. –  kryger Jun 8 '13 at 19:40
add comment

Your Answer


