gpt4 book ai didi

java - 使用 spring-boot 1.4 测试 Java 服务层

转载 作者:行者123 更新时间:2023-11-29 04:41:37 27 4
gpt4 key购买 nike

我已经创建了一个 Spring Boot 1.4 Web 应用程序,我想为服务层创建一些测试。当我使用 SB 1.3 注释时,测试运行成功。但是 IDE 向我显示 @SpringApplicationConfiguration(classes = {RepositoryConfiguration.class}) 已被弃用,所以我尝试了新的解决方案,但我总是遇到异常。

异常(exception):

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);
}

最佳答案

如果您查看 Spring Boot 1.4 中 SpringApplicationConfiguration 的 JavaDoc,您会发现,现在它的替代品是 @SpringBootTest。所以在你的测试中添加这个注释,它应该可以工作。

关于java - 使用 spring-boot 1.4 测试 Java 服务层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39000923/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com