Spring Unit Test在IntelliJ中工作,但在使用Maven时不能工作(多项目)

时间:2018-04-16 09:08:13

标签: java maven spring-boot intellij-idea

所以,我有以下设置:

Project
   Module Database
   Module Core
   Module REST

Core和REST将数据库作为依赖关系,而数据库内部是一个配置类,它在启动时连接到数据库。

现在,当我运行这个类的每个测试时(默认测试看起来像这样:

@RunWith(SpringRunner.class)
@SpringBootTest
public class RestfulApplicationTests {

@Test
public void contextLoads(){}

}

直接从IntelliJ运行时可以使用。

但如果我这样做"清理安装"在Maven,Maven找不到依赖。我的核心项目中的示例pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>myproject</groupId>
    <artifactId>myproject-restful</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>myproject-restful</name>
    <description>Restful Services for myproject</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>myproject</groupId>
            <artifactId>backend-cassandra</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>myproject</groupId>
            <artifactId>backend-cassandra</artifactId>
            <version>${project.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project> 

来自数据库部分的Pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.mycompany.mypackage</groupId>
<artifactId>backend-cassandra</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>backend-cassandra</name>
<description>Cassandra Backend for </description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-cassandra</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
            <!-- Generate test jar too -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    </plugins>
</build>

主要项目Pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.mycompany.mypackage</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>project</name>
<description>MyProject</description>


<modules>
    <module>backend-cassandra</module>
    <module>application</module>
    <module>restful</module>
</modules>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <type>pom</type>
        <version>1.2.3.RELEASE</version>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

配置类

@Configuration 
public class CassandraConfiguration extends AbstractCassandraConfiguration {

private static final AtomicReference<CassandraConfigurationFile> config
        = new AtomicReference<>(new CassandraConfigurationFile("cassandra.ini"));

@Override
protected String getKeyspaceName() {
    return config.get().getKeyspace();
}

@Bean
public CassandraClusterFactoryBean cluster() {
    CassandraClusterFactoryBean cluster =
            new CassandraClusterFactoryBean();
    cluster.setContactPoints(config.get().getHostname());
    cluster.setPort(config.get().getPort());
    cluster.setProtocolVersion(ProtocolVersion.V4);
    cluster.setTimestampGenerator(getTimestampGenerator());
    cluster.setKeyspaceCreations(getKeyspaceCreations());
    return cluster;
}

@Bean
public CassandraSessionFactoryBean session(){
    CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
    session.setCluster(cluster().getObject());
    session.setKeyspaceName(getKeyspaceName());
    session.setConverter(cassandraConverter());
    session.setSchemaAction(SchemaAction.NONE);
    return session;
}

@Bean
public CassandraConverter cassandraConverter(CassandraMappingContext mappingContext,
                                             CassandraCustomConversions cassandraCustomConversions) {
    MappingCassandraConverter converter = new MappingCassandraConverter(mappingContext);
    converter.setCustomConversions(cassandraCustomConversions);
    return converter;
}

@Bean
public CassandraMappingContext mappingContext() {
    return new BasicCassandraMappingContext();
}

@Override
public SchemaAction getSchemaAction() {
    return SchemaAction.CREATE_IF_NOT_EXISTS;
}


@Override
public String[] getEntityBasePackages() {
    return new String[]{"myproject.cassandra.domain"};
}

protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
    List<CreateKeyspaceSpecification> createKeyspaceSpecifications = new ArrayList<>();
    createKeyspaceSpecifications.add(getKeySpaceSpecification());
    return createKeyspaceSpecifications;
}

private CreateKeyspaceSpecification getKeySpaceSpecification() {
    return CreateKeyspaceSpecification.createKeyspace(config.get().getKeyspace())
            .ifNotExists(true)
            .withNetworkReplication(DataCenterReplication.of("dcl",3L));
}

@Bean
CassandraCustomConversions cassandraCustomConversions() {
    List<Converter<?, ?>> converters = new ArrayList<>();
    converters.add(new DateToTimestampConverter());
    return new CassandraCustomConversions(converters);
}

}

0 个答案:

没有答案