2016-04-24 89 views
0

我有以下JUnit测试套件,当我尝试加载我的测试时,我在测试的类中自动装入的依赖项似乎并未加载,并且出现以下错误消息:未加载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

你试过从二传手删除@Autowired,并设置它的属性

一个ComponentScan自动配置? –

+0

并且不要在每个测试中创建一个新的PingService()。为你创建对象句柄Spring。使用@Autowired在Test类中注入PingService并使用它。 –

回答

1

您肌酸g你的pingerService像

PingerService pingerService = new PingerService();

在测试类中,所以它不是spring bean,所以spring不会注入任何东西,它不会工作。 请将PingerService添加到您的弹簧配置中: 使用@Component注释它,并将其放在可以在类路径中找到的地方,或者在Spring配置类中用@Bean注释的方法创建它。

这导致了第二个问题:

@SpringApplicationConfiguration(classes = PingerService.class) 

你必须在这里提供一个配置类,而不是一个单一的服务。 配置类必须实例化spring bean,至少在PingerService和HttpAdapter中是这样。

看一看Spring java config (older version)

关于您的评论:对于配置类,将一个注解@SpringBootApplication类就足够了?

是的,如果PingerService和HttpAdapter位于SpringBootApplication注释类的子包中,那么它们可以通过ComponentScan找到。如果你使用@SpringBootApplication

+0

对于配置类,注释@SpringBootApplication类是否足够?其中调用SpringApplication.run(SimplePingApplication.class,args); –