3

我一直在开发一个简单的 Java Web 应用程序以部署到 OpenShift 中。尽管我的 JUnit 测试在 Eclipse 上本地运行,但在推送到 git 存储库后它们被跳过。我添加了詹金斯,但还是一样。我不知道我应该有什么标记或配置文件?

这是我的 pom.xml 内容:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>kia</groupId>
    <artifactId>kia</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>kia</name>
    <repositories>
        <repository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <id>openshift</id>
            <build>
                <finalName>kia</finalName>
                <plugins>
                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.1.1</version>
                        <configuration>
                            <outputDirectory>webapps</outputDirectory>
                            <warName>ROOT</warName>
                        </configuration>
                    </plugin>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.4.3</version>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

这是我的测试课:

package info.zamanifar.mysite.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.junit.Test;

import static org.junit.Assert.*;


public class JDBCConnectionTest {

    @Test
    public void getConnectionTest() {
        try {
            String dbHost = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
            String user = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
            String password = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
            StringBuilder dbURL = new StringBuilder("jdbc:mysql://");
            dbURL.append(dbHost).append("/").append("kia");

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(dbURL.toString(),user,password);
            assertNotNull("Failed to create JDBC connection!", con);
            con.close();
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch (SQLException e) {          
            e.printStackTrace();
        }
    }

}

和来自 Jenkins 的控制台输出:

[INFO] 
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ kia ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ kia ---
[INFO] Packaging webapp
4

2 回答 2

2

如果我们谈论 Openshift Jenkins,很高兴知道 DEFAULT 构建脚本中发生了什么:

转到您的作业配置,在 Execute Shell 部分中您可以看到:

# Build/update libs and run user pre_build and build
gear build

什么触发了操作(请参阅构建日志)

+ gear build
Found pom.xml... attempting to build with 'mvn -e clean package -Popenshift -DskipTests'

所以你需要在之后添加maven test到你的构建中gear build

保存并触发工作

祝你好运 :)

于 2013-11-02T08:00:15.523 回答
0

你写了什么应该触发测试?您是否在其中一个操作挂钩中放置了一些代码来运行您的测试?

于 2013-10-22T14:31:36.717 回答