Giter Site home page Giter Site logo

test-profile's Introduction

Split test type using profile

Main purpose is for what developers can write pure unit test without environment configuration and also can run integration/auto test for advanced test.

Run test

There are profile unit and integration in this project. integration required a database connection for test, this could use docker-compose.yml file for configuration, and unit doesn't need anything, it just runs.

# run unit test (deafult profile)
mvn clean test
# run unit test
mvn clean test -Punit

# run integration test
mvn clean test -Pintegration

Profiles

There are two kind of profiles. One is maven profile and another is spring boot profile. Need to understand the difference of these two profile then get the correct way to run test.

Maven profile

Maven profile is configured in pom.xml and only works for maven command such as mvn clean test -Punit. Maven profile have two points need to specified: spring boot profile and test class naming patter. Previous one indicate to use the profile in spring boot, and then next one decide which tests should be executed.

<profiles>
    <profile>
        <id>unit</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- spring boot profile, filled into application.properties -->
            <profile.active>unit</profile.active>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <!-- specify unit test class by path pattern -->
                        <includes>
                            <include>**/UniteBaseTest.java</include>
                            <include>*unit.*/*.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>integration</id>
        <properties>
            <!-- spring boot profile, filled into application.properties -->
            <profile.active>integration</profile.active>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <!-- include all test -->
                        <includes>
                            <include>**/*.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Spring boot profile

Spring boot profile indicate what kind of environment we will use in runtime. It may configured within a service or a configuration. As a example below, it defined two bean with same name, but split with different profile unit and integration. When using unit profile, there is no database connection available, so the implementation just return a mock of flyway object. In another way, implementation of integration profile initialized with existing connection and return a flayway instance.

@Configuration
public class FlywayConfig {

    @Profile({"integration"})
    @Bean("flyway")
    public Flyway flyway1(@Autowired DataSource dataSource) {
        Flyway flyway = Flyway.configure().dataSource(dataSource).load();
        flyway.clean();
        flyway.migrate();
        return flyway;
    }

    @Profile({"unit"})
    @Bean("flyway")
    public Flyway flyway2() {
        return Mockito.mock(Flyway.class);
    }

}

test-profile's People

Contributors

nanashi07 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.