1
我創建了Spring Boot 1.4 Web應用程序,我想爲服務層創建一些測試。當我使用SB 1.3註釋時,測試成功運行。但IDE向我展示@SpringApplicationConfiguration(classes = {RepositoryConfiguration.class})
已被棄用,所以我嘗試了新的解決方案,但我總是得到一個異常。使用spring-boot測試Java服務層1.4
例外:
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
我與1.4註解測試:
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 {
...
}
}
我用1.3的註釋(這是作品)測試:
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 {
...
}
}
我的服務:
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<StreetType> 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<StreetType,Integer> {
}
package com.cma.services;
/**
*
* @param <E>
* @param <K>
*/
public interface BasicService<E,K> {
Iterable<E> list();
E find(K key);
E save(E entity);
void delete(K key);
}
謝謝。這是註解解決了我的問題。 :) –