简体   繁体   中英

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. The unit tests work when I run them from my machine (in 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. Is there something wrong with my setup?

The .travis.yml file:

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:

$ 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. Look at the imports I mistakingly used:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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