@@ Autowired对象在使用@SpringBootTest时为null

问题描述 投票:0回答:1

因此,我试图在测试类中自动连接我的http对象,并且尝试与@SpringBootTest集成,但是我的http对象仍然为null。

我的测试班级是这样的。

//@RunWith(SpringRunner.class)
@SpringBootTest(classes=Http.class)
public class GetItemTests {
private static final Logger LOGGER = LoggerFactory.getLogger(GetItemTests.class);

@Autowired
private Http httpClass;
}

我的SpringBootMain类看起来像这样

@SpringBootConfiguration
@SpringBootApplication
public class SpringBootMain implements CommandLineRunner {

@Bean
ResourceConfig resourceConfig() {
    return new ResourceConfig().registerClasses(Version1Api.class,TokenUtilityClass.class, Paypal.class);
}

@Override
public void run(String... args) throws Exception {
    //test.authenticationToken();
}

public static void main(String[] args) {
    SpringApplication.run(SpringBootMain.class);
}
}

我已经尝试过使用SpringRunner以及与此同时运行,但是收到关于未能加载应用程序上下文的错误。

java spring-boot autowired spring-boot-test
1个回答
0
投票

除非您的Http类用@Component注释(这意味着它是由Spring IoC容器维护的bean),否则您将无法以您编写的方式来对其进行@Autowire

[也请发布您正在获取的异常以及您的Http类的实现,以便我们有可能提供进一步的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.