gwtjunitmockitoguicegwt-gin

Difference between @inject and @mock annotations


In the Context of JUnit, what is the difference between @inject and @mock, and in which conditions both can be used?

thanks, Rohit


Solution

  • @Inject
    

    ... is an annotation which is defined in Guice and is quite simliar to Spring @Autowire. You can use these Annotations to inject a Object which you whant to use in your tests (i.e. persistence context to work with jpa)

    @Mock
    

    ... is an annotation to (more or less) inject mock objects into your test class. In the method annoteted with @Before you can initialize the @Mock annoteted attributes via MockitoAnnotations.initMocks(this). Another way you can go is to annotate the testclass with @RunWith(MockitoJUnitRunner.class).

    I hope this will help for the first steps ;-)