Creating a partial mock from an abstract class with injected fields

44 views
Skip to first unread message

Anton Beloglazov

unread,
Apr 30, 2014, 4:33:56 AM4/30/14
to juk...@googlegroups.com
Hi All,

I'm trying to test an abstract class that has some injected fields. Basically, the class is the following:

public abstract class AbstractConnectionHolder {

@Inject
private Configuration configuration;

@Inject
private ConnectionContext context;

protected ConnectionContext getConnectionContext() {
return context;
}

protected Configuration getConfiguration() {
return configuration;
}

public Connection openConnection() {
return getConnectionContext().openConnection();
}

public void closeConnection() {
getConnectionContext().closeConnection();
}

public Connection getConnection() {
return getConnectionContext().getConnection();
}

}

To test it, I'm trying to create a new partial mock for every test. I've managed to run the following:

@RunWith(JukitoRunner.class)
public class AbstractConnectionHolderTest {

@Inject
DataSource dataSource;

@Inject
Connection connection;

@Inject
Configuration configuration;

@Inject
ConnectionContext context;

public static class Module extends JukitoModule {
@Override
protected void configureTest() {
bind(Configuration.class).toInstance(new Configuration(new PostGISTemplates()));
bind(AbstractConnectionHolder.class).toInstance(
mock(AbstractConnectionHolder.class, CALLS_REAL_METHODS));
}
}

@Before
public void setupMocks() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
}

@Test
public void shouldReturnConfiguration(AbstractConnectionHolder holder) {
assertThat(holder.getConfiguration(), is(configuration));
}

}

This runs, but it only allows me to have a single AbstractConnectionHolder mock for all tests. The ideal case would be to make it somehow inject a new AbstractConnectionHolder partial mock into each test, which I couldn't figure out how to do. Is this possible?

I also tried to manually create mocks in the @Before method as follows:

@RunWith(JukitoRunner.class)
public class AbstractConnectionHolderTest {

@Inject
DataSource dataSource;

@Inject
Connection connection;

@Inject
Configuration configuration;

@Inject
ConnectionContext context;

AbstractConnectionHolder holder;

public static class Module extends JukitoModule {
@Override
protected void configureTest() {
bind(Configuration.class).toInstance(new Configuration(new PostGISTemplates()));
}
}

@Before
public void setupMocks() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
holder = mock(AbstractConnectionHolder.class, CALLS_REAL_METHODS);
}

@Test
public void shouldReturnConfiguration() {
assertThat(holder.getConfiguration(), is(configuration));
}

}

However, this fails with the following exception:

java.lang.AssertionError: 
Expected: is <com.mysema.query.sql.Configuration@7a79dbc7>
     but: was null
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:865)
at org.junit.Assert.assertThat(Assert.java:832)
at test.AbstractConnectionHolderTest.shouldReturnConfiguration(AbstractConnectionHolderTest.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.jukito.InjectedStatement.evaluate(InjectedStatement.java:96)
at org.jukito.InjectedBeforeStatements.evaluate(InjectedBeforeStatements.java:51)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.jukito.JukitoRunner.run(JukitoRunner.java:187)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


As I understand, for some reason in this case it wasn't able to inject the fields of AbstractConnectionHolder. Is there a way to solve the original problem or this injection issue?

Thanks a lot!

Best regards,
Anton

Reply all
Reply to author
Forward
0 new messages