king_bt
瘦小王
2019-03-25 11:17
采纳率: 100%
浏览 7.5k
已采纳

springboot项目用junit进行单元测试,无法注入bean

这个是报错信息:
图片说明
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cn.com.zhshzh.xs.pms.XsApplicationTests': Unsatisfied dependency expressed through field 'reimbursementInfoProcService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.com.zhshzh.xs.pms.system.test.service.ReimbursementInfoProcService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:400)
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.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.com.zhshzh.xs.pms.system.test.service.ReimbursementInfoProcService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
... 29 common frames omitted

这个是测试类代码:

package cn.com.zhshzh.xs.pms;

import java.math.BigDecimal;
import java.util.Date;

import javax.ws.rs.core.Application;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import cn.com.zhshzh.xs.pms.system.test.PO.ReimbursementInfoProc;
import cn.com.zhshzh.xs.pms.system.test.service.ReimbursementInfoProcService;
import lombok.extern.slf4j.Slf4j;

// 获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类(被 @SpringBootApplication 注解的)
@SpringBootTest(classes = Application.class)
// 让 JUnit 运行 Spring 的测试环境, 获得 Spring 环境的上下文的支持
@RunWith(SpringRunner.class)
@WebAppConfiguration
@Slf4j
public class XsApplicationTests {
@Autowired
private ReimbursementInfoProcService reimbursementInfoProcService;

@Test
public void TestCase() {
    ReimbursementInfoProc reimbursementInfoProc = new ReimbursementInfoProc();
    reimbursementInfoProc.setProcessId("1");
    reimbursementInfoProc.setPostId("1");
    reimbursementInfoProc.setReimbursementPerson("23456");
    reimbursementInfoProc.setReimbursementMoney(new BigDecimal("50.00"));
    reimbursementInfoProc.setReimbursementDate(new Date());
    reimbursementInfoProc.setApprovalOpinion("审批通过");
    reimbursementInfoProc.setCreateBy("23456");
    reimbursementInfoProc.setUpdateBy("23456");
    try {
        reimbursementInfoProcService.insertReimbursementInfo(reimbursementInfoProc);
    } catch (Exception e) {
        log.error("message", e);
    }
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • king_bt
    瘦小王 2019-03-25 15:50
    已采纳

    首先把SpringBootTest注解中的Application.class改为XsApplication.class(项目的启动类是XsApplication),再加上webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT,最后再把@WebAppConfiguration注解删了就行了

    点赞 评论
  • qq_22847605
    qq_22847605 2019-03-25 11:21

    注入的bean是否上下文有扫描到,是否实现依赖引入

    点赞 评论
  • ko289830707
    Solo丶慕颜 2019-03-25 11:38

    ReimbursementInfoProcServiceImpl类上加@Service

    点赞 评论

相关推荐