gpt4 book ai didi

java - JUnit 依赖项未加载

转载 作者:行者123 更新时间:2023-12-01 10:00:51 24 4
gpt4 key购买 nike

我有以下 JUnit 测试套件,当我尝试加载测试时,我在正在测试的类中 Autowiring 的依赖项似乎没有加载,并且收到以下错误消息:

package com.uk.jacob.service;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.uk.jacob.model.Ping;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PingerService.class)
@WebAppConfiguration
public class PingerServiceTests {

@Test
public void testPingerServiceReturnsOkWhenServiceIsUp(){
PingerService pingerService = new PingerService();
Ping ping = pingerService.ping("http://devnews.today");

assertEquals(true, ping.ok);
}

@Test
public void testPingerServiceReturnsOkWhenServiceIsDown(){
PingerService pingerService = new PingerService();
Ping ping = pingerService.ping("https://jacob.uk.comz");

assertEquals(false, ping.ok);
}

}

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.uk.jacob.service.PingerService.setHttpAdapter(com.uk.jacob.adapter.HttpAdapter); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.uk.jacob.adapter.HttpAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

Pinger服务:

package com.uk.jacob.service;

import java.io.IOException;
import java.net.HttpURLConnection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.uk.jacob.adapter.HttpAdapter;
import com.uk.jacob.model.Ping;

@Component
public class PingerService {

HttpAdapter httpAdapter;

public Ping ping(String urlToPing) {
Ping ping = new Ping();

try {
HttpURLConnection connection = httpAdapter.createHttpURLConnection(urlToPing);

if(connectionIsOk(connection)){
ping.ok = true;
}
} catch (Exception e) {
ping.ok = false;
}

return ping;
}

private boolean connectionIsOk(HttpURLConnection connection) throws IOException {
return connection.getResponseCode() == 200;
}

@Autowired
public void setHttpAdapter(HttpAdapter httpAdapter){
this.httpAdapter = httpAdapter;
}

}

Http适配器:

package com.uk.jacob.adapter;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.springframework.stereotype.Component;

@Component
public class HttpAdapter {
public HttpURLConnection createHttpURLConnection(String urlToPing) throws IOException{
URL url = new URL(urlToPing);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");
connection.connect();

return connection;
}
}

最佳答案

你正在创建你的 pingerService 就像

PingerService pingerService = new PingerService();

在测试类中,所以它不是一个spring bean,所以spring不会在那里注入(inject)任何东西,它不会工作。相反,将 PingerService 添加到您的 spring 配置中:使用 @Component 注释它,并将其放在类路径中可以找到的位置,或者在 Spring 配置类中使用 @Bean 注释的方法创建它。

这导致了第二个问题:

@SpringApplicationConfiguration(classes = PingerService.class)

您必须在此处提供一个配置类,而不是单个服务。配置类必须实例化 spring bean,在您的情况下至少实例化 PingerService 和 HttpAdapter。

看看Spring java config (older version)

关于您的评论:对于配置类,带注释的 @SpringBootApplication 类就足够了吗?

是的,如果 PingerService 和 HttpAdapter 位于 SpringBootApplication 带注释的类的子包中,那么这就足够了,这样 ComponentScan 就可以找到它们。

如果您使用@SpringBootApplication,则会自动配置 ComponentScan

关于java - JUnit 依赖项未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36824976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com