Testing Java service layer with spring-boot 1.4
<p>I have created a Spring Boot 1.4 web application and I want to create some tests for the service layer. When I am using SB 1.3 annotations the test runs succesfully. But the IDE shows me the <code>@SpringApplicationConfiguration(classes = {RepositoryConfiguration.class})</code> is deprecated so I tried the new solution but I always get an exception.</p>

<p>Exception:</p>

<pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.cma.services.StreetTypeServiceImplTest': Unsatisfied dependency expressed through method 'setStreetTypeRepository' parameter 0: No qualifying bean of type [com.cma.repositories.StreetTypeRepository] found for dependency [com.cma.repositories.StreetTypeRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cma.repositories.StreetTypeRepository] found for dependency [com.cma.repositories.StreetTypeRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:648)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener.prepareTestInstance(AutoConfigureReportTestExecutionListener.java:46)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cma.repositories.StreetTypeRepository] found for dependency [com.cma.repositories.StreetTypeRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
    ... 33 more
</code></pre>

<p>My test with 1.4 annotation:</p>

<pre><code>package com.cma.services;

import com.cma.domains.StreetType;
import com.cma.repositories.StreetTypeRepository;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestDatabase;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
public class StreetTypeServiceImplTest {


    private StreetTypeRepository streetTypeRepository;

    @Autowired
    public void setStreetTypeRepository(StreetTypeRepository streetTypeRepository){
        this.streetTypeRepository = streetTypeRepository;
    }

    @Test
    public void save() throws Exception {
        ...
    }
}
</code></pre>

<p>My test with 1.3 annotation (which is works):</p>

<pre><code>package com.cma.services;

import com.cma.domains.StreetType;
import com.cma.repositories.StreetTypeRepository;
import com.cma.configuration.RepositoryConfiguration;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {RepositoryConfiguration.class})
public class StreetTypeServiceImplTest {


    private StreetTypeRepository streetTypeRepository;

    @Autowired
    public void setStreetTypeRepository(StreetTypeRepository streetTypeRepository){
        this.streetTypeRepository = streetTypeRepository;
    }

    @Test
    public void save() throws Exception {
        ...
    }
}
</code></pre>

<p>My services:</p>

<pre><code>    package com.cma.services;

import com.cma.domains.StreetType;
import com.cma.repositories.StreetTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StreetTypeServiceImpl implements StreetTypeService {
    private StreetTypeRepository streetTypeRepository;

    @Autowired
    public void setStreetTypeRepository(StreetTypeRepository streetTypeRepository) {
        this.streetTypeRepository = streetTypeRepository;
    }

    @Override
    public Iterable&lt;StreetType&gt; list() {
        return streetTypeRepository.findAll();
    }

    @Override
    public StreetType find(Integer key) {
        return streetTypeRepository.findOne(key);
    }

    @Override
    public StreetType save(StreetType entity) {
        return streetTypeRepository.save(entity);
    }

    @Override
    public void delete(Integer key) {
        streetTypeRepository.delete(key);
    }
}

package com.cma.services;

import com.cma.domains.StreetType;

public interface StreetTypeService extends BasicService&lt;StreetType,Integer&gt; {
}

package com.cma.services;

/**
 *
 * @param &lt;E&gt;
 * @param &lt;K&gt;
 */
public interface BasicService&lt;E,K&gt; {
    Iterable&lt;E&gt; list();

    E find(K key);

    E save(E entity);

    void delete(K key);
}
</code></pre>