Java 无法在SpringBoot的集成测试中执行@Autowired a bean

Java 无法在SpringBoot的集成测试中执行@Autowired a bean,java,spring-boot,spring-mvc,integration-testing,war,Java,Spring Boot,Spring Mvc,Integration Testing,War,我在SpringBoot 2.x上有一个应用程序 @SpringBootConfiguration @启用自动配置 @组件扫描 公共类WebSpringBootJarApplication扩展了SpringBootServletilizer实现了WebApplicationInitializer{ 公共静态void main(字符串[]args){ run(WebSpringBootJarApplication.class,args); } @凌驾 受保护的SpringApplicationBu

我在SpringBoot 2.x上有一个应用程序

@SpringBootConfiguration
@启用自动配置
@组件扫描
公共类WebSpringBootJarApplication扩展了SpringBootServletilizer实现了WebApplicationInitializer{
公共静态void main(字符串[]args){
run(WebSpringBootJarApplication.class,args);
}
@凌驾
受保护的SpringApplicationBuilder配置(SpringApplicationBuilder){
返回builder.sources(WebSpringBootJarApplication.class);
}
}
pom.xml


org.springframework.boot
spring启动程序父级
2.2.4.1发布
weblogic.war.spring.boot
WebLogicWarSpringBoot
0.0.1-快照
WebLogicWarSpringBoot
战争
这是应该自动连接的类

@服务
公共类CompanyReadServiceImpl实现CompanyReadService{
私人公司老板公司老板;
私人公司ReadRepository CompanyReadRepository;
@自动连线
上市公司ReadServiceImpl(公司映射公司映射,
公司ReadRepository公司ReadRepository){
this.companyMapper=companyMapper;
this.companyReadRepository=companyReadRepository;
}
@交易的
@凌驾
上市公司到getById(长id){
公司=查找(id);
回报转化(公司);
}
@交易的
@凌驾
公共布尔值isByName(字符串名称){
返回此.companyReadRepository.findByName(name).isPresent();
}
@交易的
@凌驾
public CompanyTo getByName(字符串名称){
Company Company=companyReadRepository.findByName(name).orElse(new Company());
回报转化(公司);
}
私人公司查找(长id){
返回此.companyReadRepository.findById(id).orelsetrow(()->新运行时异常(“未找到用户”);
}
私有公司待改造(公司){
将此.companyMapper.companyToCompanyDto返回(公司);
}
}
pom.xml


org.springframework.boot
弹簧起动试验
测验
更新

这个类来自类似的项目,并且它可以工作(但是Intellij idea将字段标记为错误)

src/test/java/weblogic/war/spring/boot/service/read/CompanyReadServiceTest.java

@RunWith(SpringRunner.class)
@春靴测试
公共类公司ReadServiceTest{
私有静态最终记录器Logger=LoggerFactory.getLogger(CompanyReadServiceTest.class);
私有静态字符串名称\u方法\u读取\u按\u名称\u BOOLEAN=“isByName”;
@自动连线
私人公司ReadRepository CompanyReadRepository;
@自动连线
私人公司领导服务公司领导服务;
@自动连线
私人公司readrepositorytest公司readrepositorytest;
@自动连线
私人公司老板公司老板;
@试验
public void getById(){
公司lastEntry=getLastEntry();
Long-ideexpected=null;
if(lastEntry!=null)ideexpected=lastEntry.getId();
如果(IdeExpected!=null){
CompanyDto CompanyDto=searchByIdentity(IdeExpected);
Long-idActual=companyDto.getId();
Assert.assertEquals(“条目查找-失败!!!”,IdeExpected,idActual);
}
}
@试验
public void isByName(){
公司lastEntry=getLastEntry();
字符串名称=null;
如果(lastEntry!=null)name=lastEntry.getName();
if(name!=null){
布尔entryByName=isEntryByName(名称);
Assert.assertTrue(“条目查找-失败”,entryByName);
}
}
私人公司到SearchByIdentity(长id){
返回此.companyReadService.getById(id);
}
专用布尔值isEntryByName(字符串名称){
返回getByNameReflection(companyReadService,NAME\u METHOD\u READ\u BY\u NAME\u BOOLEAN,NAME);
}
私人公司getLastEntry(){
可选lastEntry=companyReadRepositoryTest.getLastEntry();
返回lastEntry.orElse(空);
}
}
我在一个项目中复制一份

更新_2

我清除了本地Maven存储。 IDE还(在编译时)突出显示可能未自动连接的字段。但是我运行了一个测试类,它可以工作

为什么??(这可能是在我刚刚重命名包并编辑pom.xml时开始的)

自动布线在应用程序的工作范围内正常。但它在应用程序的测试范围内不起作用


谁能分享关于这个问题的想法?

您正试图加载实际的应用程序上下文以用于Junit。 您可以使用下面的WebSpringBootJarApplication类作为原始Spring上下文来实现这一点

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {WebSpringBootJarApplication.class})
public class HelloWorldTest {

@Autowired(required = true)
SomeService service;

@Test
public void helloTest() {
    System.out.println(service);
}
}

有开胃菜吗?所以你得到的是
companySaveService
的NullPointerException,对吗?Iitellij IDE给我指出了一个错误。所以测试不起作用,在测试类中,我可以连接任何bean,不管它位于何处,也不管它应该连接。这是我在集成测试中经常做的,没错。甚至不为null,但在编译阶段,开发环境指示一个错误…我删除了该类,以便它不会混淆您。。。