问题尚未解决,希望知道伙伴回复讨论:2022-1-13 spring boot测试类junit可以注入Service但是无法注入User

package com.yuyouya.householdledger;

import com.yuyouya.householdledger.service.FamilyService;
import com.yuyouya.householdledger.user.Family;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
class HouseholdledgerApplicationTests {

    @Test
    void contextLoads() {
    }

    @Resource
    private FamilyService familyService; //可以注入
    @Resource
    Family family; //无法注入

    @Test
    void insertFamily() {
        family.setCode("haha1");
        family.setName("很爱很爱你!");
        System.err.println(family);
//        Family family = new Family();
//        family.setName("测试家庭5");
//        familyService.insertFamily(family);
    }
}

报错如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.yuyouya.householdledger.HouseholdledgerApplicationTests': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.yuyouya.householdledger.user.Family' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}

	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:332)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:417)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
	at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)

上一篇:【C# 异常处理】StackTrace 堆栈跟踪


下一篇:多线程