我有以下JUnit测试套件,当我尝试加载测试时,我在测试的类中自动连接的依赖项似乎没有加载,我收到以下错误消息:
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: {}
PingerService:
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;
}
}
HttpAdapter:
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;
}
}
答案 0 :(得分:1)
您正在创建像
这样的pingerServicePingerService pingerService = new PingerService();
在测试类中,所以它不是一个弹簧豆,所以弹簧不会在那里注入任何东西,它不会工作。 而是将PingerService添加到弹簧配置中: 使用@Component对其进行注释,并将其放在可以在类路径中找到的位置,或者在Spring配置类中使用@Bean注释方法创建它。
这导致了第二个问题:
@SpringApplicationConfiguration(classes = PingerService.class)
您必须在此处提供配置类,而不是单个服务。 配置类必须实例化spring bean,在你的情况下至少是PingerService和HttpAdapter。
查看Spring java config (older version)
关于你的评论:对于配置类,带注释的@SpringBootApplication类是否足够?
是的,如果PingerService和HttpAdapter位于SpringBootApplication注释类的子包中,那就足够了,所以ComponentScan可以找到它们。
如果使用@SpringBootApplication
,则会自动配置ComponentScan