JUnit 5对没有SpringBootApplication的子项目进行测试

时间:2019-04-09 23:24:42

标签: java spring-boot gradle spring-data-jpa junit-jupiter

尤其是我有一个子项目,该子项目属于Multi-Project-Build。
该子项目没有自己的ios/Runner
经过一番研究,我发现在ClassPath <key>NSPhotoLibraryUsageDescription</key> <string>Using the library for ...</string> <key>NSMicrophoneUsageDescription</key> <string>Using the mic for ...</string> <key>NSCameraUsageDescription</key> <string>Using the camera for ...</string> 中搜索了
但是,由于找不到在SpringBootApplication中配置的@SpringBootConfiguration,所以现在我无能为力了。

引发以下异常:

DataSource

我的期望是看到输出:application.yml,如有关Spring Boot 2和Hikari的指南中所述。
但是,本指南对SpringBootApplication类中的“测试”感到满意。

我的错在哪里?

子项目的结构如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'de.acme.storage.DBConnectionTest': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
    ...

子项目的build.gradle:

DataSource: HikariDataSource (HikariPool-1)

主项目中的build.gradle包含以下依赖项部分:

src
+---main
|   \---java
|   |   \---de
|   |       \---acme
|   |           \---storage
|   |               +---model
|   |               |       User.java
|   |               |       
|   |               \---mysql
|   |                       UserDAOImpl.java
|               
\---test
    +---java
    |   \---de
    |       \---acme
    |           \---storage
    |                   DBConnectionTest.java
    |                   TestConfig.java
    |                   
    \---resources
            application.yml

application.yml内容:

plugins {
    id 'org.springframework.boot'
}

apply plugin: 'io.spring.dependency-management'

dependencies {
    implementation project(':api'),
            'javax.inject:javax.inject:1',
            'org.springframework.boot:spring-boot-starter-data-jpa'

    runtime 'mysql:mysql-connector-java'
}

JUnit测试:

dependencies {
    implementation 'org.slf4j:slf4j-api'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'junit', module: 'junit' //by both name and group
    }
    testImplementation 'org.junit.jupiter:junit-jupiter-api',
            'org.junit.jupiter:junit-jupiter-params'

    testRuntime 'org.junit.jupiter:junit-jupiter-engine'
}

最后是带有spring: datasource: url: jdbc:mysql://localhost:13306/testdb username: test-user password: geheim jpa: properties: hibernate: dialect: org.hibernate.dialect.MySQLDialect format_sql: true id: new_generator_mappings: false 注释的空配置:

@SpringBootTest
class DBConnectionTest {
    private static final Logger LOG = LoggerFactory.getLogger(DBConnectionTest.class);

    @Inject
    private DataSource dataSource;

    @Test
    void test() {
        LOG.debug("DataSource: {}", dataSource);
        assertThat(dataSource).as("Es gibt eine Datasource").isNotNull();
    }
}

1 个答案:

答案 0 :(得分:0)

仅缺少一个小东西,即注释@EnableAutoConfiguration。认为这将由@SpringBootConfiguration ...

完成
package de.acme.storage;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

@SpringBootConfiguration
@EnableAutoConfiguration
class TestConfig {
}