问题遇到的现象和发生背景
可以注入Service,但是就是无法注入实体对象
问题相关代码,请勿粘贴截图
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 org.springframework.test.context.web.WebAppConfiguration;
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=)}
我的解答思路和尝试过的方法
网上搜的与启动类名称一样不存在,因为测试后面加的Test
service为什么可以注入,实体对象为什么不可以?
我想要达到的结果
可以在测试类成功注入实体对象类!