简体   繁体   English

Travis CI上JUnit测试用例的InitializationError

[英]InitializationError on JUnit test case on Travis CI

I'm trying to figure out Travis CI to run my JUnit tests for a Selenium project. 我试图弄清楚Travis CI,以便为Selenium项目运行我的JUnit测试。 The unit tests work when I run them from my machine (in IntelliJ IDEA). 从我的机器(在IntelliJ IDEA中)运行它们时,单元测试工作。

Since the test class doesn't seem to miss anything, I'm wondering why the tests run perfectly fine on my machine, but fail on Travis CI. 由于测试类似乎什么都没有遗漏,我想知道为什么测试可以在我的机器上完美运行,但是在Travis CI上却失败了。 Is there something wrong with my setup? 我的设置有问题吗?

The .travis.yml file: .travis.yml文件:

language: java

jdk:
  - oraclejdk8

script: ant test


#before_install:
# - cd java

The build file: 构建文件:

<?xml version="1.0" encoding="UTF-8"?>
<project name="bromine" default="test">
    <property name="dir.src" value="src"/>
    <property name="dir.test.src" value="tests"/>
    <property name="dir.build" value="build"/>
    <property name="dir.build.test" value="build/test"/>
    <property name="dir.classes" value="${dir.build}/classes"/>
    <property name="dir.jar" value="${dir.build}/jar"/>

    <path id="classpath.base">
        <pathelement location="lib/selenium-server-standalone-3.0.0-beta2.jar"/>
    </path>

    <path id="classpath.test">
        <pathelement location="lib/junit-4.12.jar"/>
        <pathelement location="lib/hamcrest-core-1.3.jar"/>
        <pathelement location="${dir.build}"/>
        <path refid="classpath.base"/>
    </path>

    <target name="test" depends="run, clean"/>

    <target name="compile">
        <mkdir dir="${dir.build}"/>
        <javac srcdir="${dir.src}" destdir="${dir.build}" includeantruntime="false">
            <classpath refid="classpath.base"/>
        </javac>
    </target>

    <target name="build" depends="compile">
        <mkdir dir="${dir.build.test}"/>
        <javac srcdir="${dir.test.src}" destdir="${dir.build.test}" includeantruntime="false">
            <classpath refid="classpath.test"/>
        </javac>
        <echo message="Build done"/>
    </target>

    <!-- Test and build all files  -->
    <!-- To run this: use "ant" (default) or "ant run" -->
    <target name="run" depends="build">
        <junit printsummary="on" haltonfailure="yes">
            <classpath>
                <path refid="classpath.test"/>
                <pathelement location="${dir.build.test}"/>
            </classpath>
            <formatter type="brief" usefile="false"/>
            <batchtest>
                <fileset dir="${dir.test.src}" includes="**/*Test*.java"/>
            </batchtest>
        </junit>
    </target>

    <!-- delete all class files -->
    <!-- To run this: use "ant clean" -->
    <target name="clean">
        <delete>
            <fileset dir="${basedir}" includes="**/*.class"/>
        </delete>
        <echo message="Clean done"/>
    </target>
</project>

The failing build output I get from Travis CI: 我从Travis CI获得的构建输出失败:

$ jdk_switcher use oraclejdk8
Switching to Oracle JDK8 (java-8-oracle), JAVA_HOME will be set to /usr/lib/jvm/java-8-oracle
$ java -Xmx32m -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
$ javac -J-Xmx32m -version
javac 1.8.0_31
2.33s$ ant test
Buildfile: /home/travis/build/Thibstars/Bromine/build.xml
compile:
    [mkdir] Created dir: /home/travis/build/Thibstars/Bromine/build
    [javac] Compiling 21 source files to /home/travis/build/Thibstars/Bromine/build
build:
    [mkdir] Created dir: /home/travis/build/Thibstars/Bromine/build/test
    [javac] Compiling 2 source files to /home/travis/build/Thibstars/Bromine/build/test
     [echo] Build done
run:
    [junit] Running NavigatorTests
    [junit] Testsuite: NavigatorTests
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.028 sec
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.028 sec
    [junit] 
    [junit] Testcase: initializationError(NavigatorTests):  Caused an ERROR
    [junit] No runnable methods
    [junit] java.lang.Exception: No runnable methods
    [junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    [junit] 
    [junit] 
BUILD FAILED
/home/travis/build/Thibstars/Bromine/build.xml:41: Test NavigatorTests failed
Total time: 2 seconds
The command "ant test" exited with 1.
Done. Your build exited with 1.

The failing test: 测试失败:

import commands.InitTestFrameworkCommand;
import navigation.Navigator;
import navigation.NavigatorFactory;
import org.junit.Rule;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import rules.ScreenShotOnFailure;
import sut.Environment;

import java.net.MalformedURLException;
import java.net.URL;

import static org.junit.Assert.assertTrue;

public class NavigatorTests {

    @Rule
    public ScreenShotOnFailure failure = new ScreenShotOnFailure();

    @BeforeClass
    public static void init() {
        new InitTestFrameworkCommand().execute();
        NavigatorFactory.createNavigator(new Environment("Google", "https://google.com"));
    }

    @Test
    public void shouldOpenGoogle() {
        try {
            Navigator.getInstance().navigateTo(new URL("https://google.com"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        assertTrue(Navigator.getInstance().getUrl().startsWith("https://www.google."));
    }

    @AfterClass
    public static void tearDown() {
        NavigatorFactory.destroyNavigator();
    }
}

It seems I have confused TestNg with JUnit. 看来我已经将TestNg与JUnit混淆了。 Look at the imports I mistakingly used: 看一下我误用的输入:

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM