Search code examples
annotationsjmockinject

Inject mock object using jMock


I'm trying to use jMock to create a mock object. The mock object looks fine - but the thing is it is not injected to the tested class. Here is my code:

@RunWith(JMock.class)
public class FeederFilterTest {

private TestedClass service;

@Mock
private TheMockedInterface mock;
    ...
}

@Service
public class TestedClass {

@Autowired
private TheMockedInterface interface;
...
    }

As I said - the mock object is created as expected - but when I invoke a method on the TestClass that uses the TheMockInterface - I get a NullPointerException.

I know that Mockito has @InjectMocks - does jMock have something like that too?


Solution

  • JMock does not intercept automatic instance creation. That's a design choice. Our view is that internally created objects should not be so complicated that they need mocking. Anything that does should be passed in and the dependency made explicit. There are other frameworks that will do such clever things and I'm sure someone will post a link here pretty soon :)