【问题标题】:JUnit dependencies not being loaded in未加载 JUnit 依赖项
【发布时间】:2016-08-17 22:09:45
【问题描述】:

我有以下 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: {}

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;
    }

}

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;
    }
}

【问题讨论】:

  • 您是否尝试过从 setter 中移除 @Autowired 并将其设置在属性上?
  • 并且不要在每个Test中创建一个新的PingService()。对象的创建为您处理 Spring。使用 @Autowired 在 Test 类中注入 PingService 并使用它。

标签: java spring spring-mvc junit spring-boot


【解决方案1】:

你正在创建你的 pingerService 像

PingerService pingerService = new PingerService();

在测试类中,所以它不是一个spring bean,所以spring不会在那里注入任何东西,它不会工作。 而是将 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

【讨论】:

  • 对于配置类,带注释的@SpringBootApplication 类就足够了吗?其中调用 SpringApplication.run(SimplePingApplication.class, args);
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 2020-03-01
  • 2019-01-03
  • 2015-03-09
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多