java - 在 Spring 应用程序中运行 Junit 测试时依赖关系不满足

标签 java spring spring-mvc junit integration-testing

我在 Maven 项目中有这个配置类:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import lombok.Data;

@Data
@Configuration
public class SmsConfig {

    @Value("${sms.domainId}")
    private String domainId;

    @Value("${sms.gateway.url}")
    private String gatewayUrl;

    @Value("${sms.cmd}")
    private String cmd;

    @Value("${sms.login}")
    private String login;

    @Value("${sms.passwd}")
    private String passwd;

}

我在 Spring 项目中有这个服务类:

Service("smsService")
public class AltiriaSMSRestServiceImpl implements SmsService {

    private final SmsConfig smsConfig;

    public AltiriaSMSRestServiceImpl(SmsConfig smsConfig) {
        this.smsConfig = smsConfig;
    }

    @Override
    public boolean sendSMS(String msg, String to) throws Exception {
    ...
    }
...
}

这个测试:

@ContextConfiguration(classes = { SmsConfig.class })
@RunWith(SpringJUnit4ClassRunner.class)
public class AltiriaSMSRestServiceImplTest {

    @Autowired
    @Qualifier("smsService")
    private AltiriaSMSRestServiceImpl smsService;

    @Test
    public void testSendSMS() throws Exception {
        smsService.sendSMS("this is a test", "+34776498");
    }

}

但是当我运行测试时出现此错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.bonanza.service.AltiriaSMSRestServiceImplTest': Unsatisfied dependency expressed through field 'smsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bonanza.service.AltiriaSMSRestServiceImpl' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="smsService")}

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)

最佳答案

我认为您混淆了 Spring 配置和配置属性。

@Service("smsService")
public class AltiriaSMSRestServiceImpl implements SmsService {

    private final SmsConfigData smsConfig;

    public AltiriaSMSRestServiceImpl(SmsConfigData smsConfig) {
        this.smsConfig = smsConfig;
    }
    ...
}

// the configuration data, mapped to the application.properties/yml file
@ConfigurationProperties(prefix="sms")
@Data
public class SmsConfigData {

    @Value("${domainId}")
    private String domainId;
    ...
}

// this is a spring configuration class, ie the definition of spring beans
// I don't think you even need this, since the 
// SmsConfigData gets constructor-injected automatically
// this is the class that gets included in the test config class list

@Configuration
public class SmsServiceConfiguration {
@Bean
SmsService smsService(SmsConfigData config) {
    return new AltiriaSMSRestServiceImpl(config);
}

关于java - 在 Spring 应用程序中运行 Junit 测试时依赖关系不满足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59007355/

相关文章:

java - 使用 Spring JDBC 模板从数据库检索时间戳值

java - 带有可选嵌入式 Tomcat 容器的 Spring Boot 中的 JNDI

java - eclipse Spring MVC 4 +Hibernate 和 MySQL + Tiles.xml 中的 HTTP 状态 404

java - 本地线程变量或新线程 - Java

java - 并发异常

java - 贝塞尔曲线()不工作

java - 对 JTable 进行排序导致 NullPointerException

spring - 在 Spring Boot Data Rest 中向 HATEOAS 响应添加更多信息

java - 是否可以在 Spring MVC(或 Boot)的外部目录中使用 .jsp 文件?

java - 错误 :java. lang.IllegalStateException:BindingResult 和 bean 名称 'spcrId' 的普通目标对象都不能用作请求属性