junit - 为什么我的 junit 测试在部署(git push)到 openshift 存储库后被跳过?

标签 junit openshift

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

这是我的 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

最佳答案

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

转到您的作业配置,在执行 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'

因此您需要在 gear build 之后将 maven test 添加到您的构建中

保存并触发作业

祝你好运:)

关于junit - 为什么我的 junit 测试在部署(git push)到 openshift 存储库后被跳过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19518451/

相关文章:

java - 使用私有(private)访问说明符访问具有参数的私有(private)方法

java - 如何使用 Junit 和 Mockito 对 REST Controller 进行单元测试?

java - 在没有 ArgumentCaptor 的情况下匹配可变对象

带有 Maven 和 Eclipse 的 Java 动态 Web 项目

scalability - 将后台进程(celery)添加到 OpenShift 上的缩放应用程序

java - Android jUnit 测试错误

node.js - 在线阅读 Redhat OpenShift 内部的 secret ?

spring - WebSocket 握手期间出错 : Unexpected response code: 400 when using Spring Websocket, Stomp,Openshift 中的 SockJS

MySql 被消灭了

maven - 如何在 Openshift 上重新部署 Tomcat 7 应用程序