2

I'm trying to build some karate tests to a spring boot application. Here's the project structure :

co-training-backend            (parent)
   |_ co-training-rest         (maven module)
   |_ co-training-rest-karate  (maven module)

If i run the server and launch the karate tests everything works fine. Now i'd like to automate that by running the server from the rest-karate module. Here's my config :

class CoTrainingTests {

    private static ConfigurableApplicationContext context = null;

    @BeforeAll
    public static void setUp() throws Exception {

        context = SpringApplication.run(RestBootstrap.class, new String[]{});
    }

    @Karate.Test
    Karate testAll() {
        return Karate.run().relativeTo(getClass());
    }

    @AfterAll
    public static void tearDown() throws Exception {
       if(context!=null)
           context.stop();
    }
}

pom.xml

 <artifactId>co-training-rest-karate</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <junit-jupiter.version>5.6.2</junit-jupiter.version>
    <karate.version>0.9.6</karate.version>
    <maven.compiler.version>3.8.1</maven.compiler.version>
</properties>
<dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-apache</artifactId>
            <version>${karate.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-junit5</artifactId>
            <version>${karate.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- this is the spring boot rest project -->
        <dependency>
            <artifactId>co-training-rest</artifactId>
            <groupId>com.co.training</groupId>
            <version>1.0.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <testResources>
            <testResource>
                <directory>src/test/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.version}</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

If i run the tests from the command line i have this maven compiler error :

mvn clean test

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project co-training-rest-karate: Compilation failure [ERROR] /home/salto/tutorials/co-training/co-training-backend/co-training-rest-karate/src/test/java/cotraining/CoTrainingTests.java:[3,28] package com.co.training.rest does not exist

but this class exists in the rest dependency :

ls /home/salto/.m2/repository/com/co/training/co-training-rest/1.0.0-SNAPSHOT/co-training-rest-1.0.0-SNAPSHOT.jar

/home/salto/.m2/repository/com/co/training/co-training-rest/1.0.0-SNAPSHOT/co-training-rest-1.0.0-SNAPSHOT.jar

any idea ?

Amdouni Mohamed Ali
  • 410
  • 1
  • 4
  • 18

1 Answers1

3

You can use the integration-test phase of the maven lifecycle:

  1. pre-integration-test: start the co-training-rest module

  2. integration-test: run you karate integration tests

  3. post-integration-test: shut down the co-training-rest module and do any other necessary cleanup

Katy
  • 1,023
  • 7
  • 19
  • 2
    Thx Mas. for your it points me to the solution (it's a workaround because i didn't figure out why maven can't find the dependency ). I've used maven-dependency-plugin to copy the co-training-rest sources into the karate module then i used process-exec-maven-plugin to run and stop the server (https://stackoverflow.com/questions/50575478/maven-spring-boot-plugin-how-to-run-spring-boot-from-another-project) the last thing to do is to configure the surefire plugin to run only on the integration-test phase (https://stackoverflow.com/questions/1399240/how-do-i-get-my-maven-integration-tests-to-run). – Amdouni Mohamed Ali Sep 28 '20 at 18:49