How to use the SpringBoot's context for testing by JerseyTest
<p>I have some <strong><em>@Component</em></strong> and <strong><em>@Resource</em></strong> in my <strong><em>SpringBoot</em></strong> application.
I have the right JDBC datasource, and also I have some REST services, by Jersey.
I want to test one of the services, but it will fail, it says:</p>

<blockquote>
  <p>Injection of autowired dependencies failed</p>
</blockquote>

<p>But it does not use any component.</p>

<p>This is a simple test for testing db, and <strong>it is working</strong>:</p>

<pre><code>@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
public class CommonRepositoryTest {

    @Autowired
    private MyRepository myRepository;

    @Test
    public void testDatabaseChangeLogsSize() {
        int resultSize = myRepository.getTableRowSize(MyTable.TABLE_NAME);
        System.out.println("MyTable result list size: "+resultSize);
        assertTrue("MyTable table should has at least one row!", resultSize&gt;0);
    }

}
</code></pre>

<p>But this REST tester <strong>is not working</strong>:</p>

<pre><code>@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
public class SampleResourceTest extends JerseyTest {

    @Override
    protected Application configure() {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyApplication.class);
        return new ResourceConfig(SampleResource.class).property("contextConfig", context);
    }

    @Test
    public void testSampleGet() throws Exception {
        long id = 1;
        String name = "name";
        SampleDomainModel sampleDomainModel = new SampleDomainModel();
        sampleDomainModel.setId(id);
        sampleDomainModel.setName(name);
        Response response = target("/sampleresource/samplepath/" + id).queryParam(name).request().get(Response.class);
        SampleDomainModel responseSampleDomainModel = response.readEntity(SampleDomainModel.class);
        assertEquals(sampleDomainModel.getId(), responseSampleDomainModel.getId());
    }

}
</code></pre>

<p>As you see, it must override the <strong><em>configure()</em></strong> method from <strong><em>JerseyTest</em></strong>.
I think the problam is, that the <strong><em>AnnotationConfigApplicationContext</em></strong> cannot load anything maybe (?).
The <strong><em>@SpringApplicationConfiguration(classes = MyApplication.class)</em></strong> annotation loads the context, but the <strong><em>new AnnotationConfigApplicationContext(MyApplication.class)</em></strong> code maybe do the failure, it does not have the full context.</p>

<p>If I replace the code with mocking, it works (but it is not a nice way):</p>

<pre><code>@Override
protected Application configure() {
    ApplicationContext mockContext = Mockito.mock(ApplicationContext.class);
    return new ResourceConfig(SampleResource.class).property("contextConfig", mockContext);
}
</code></pre>

<p>The fail message is:</p>

<pre><code>2016-05-13 13:25:39.617  WARN 9832 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRepositoryImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate com.repository.impl.myRepositoryImpl.jdbcTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.422 sec &lt;&lt;&lt; FAILURE! - in com.ws.server.SampleResourceTest
testSampleGetWithCorrectParameters(com.ws.server.SampleResourceTest)  Time elapsed: 0.015 sec  &lt;&lt;&lt; ERROR!
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRepositoryImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate com.repository.impl.MyRepositoryImpl.jdbcTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:180)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:121)
</code></pre>

<p>How to use the SpringBoot context for this jersey test?</p>