mercredi 29 août 2018

javafx & testfx : HeadlessException at java.awt.MouseInfo.getPointerInfo

I created a skeletton of a java gradle project using javaFx and testFx. When running the test gradle task, I get the following error, on the line which clicks on the button :

java.awt.HeadlessException at java.awt.MouseInfo.getPointerInfo(MouseInfo.java:74) at org.testfx.service.adapter.impl.AwtRobotAdapter.getMouseLocation(AwtRobotAdapter.java:78) at org.testfx.robot.impl.BaseRobotImpl.retrieveMouse(BaseRobotImpl.java:77) at org.testfx.robot.impl.MoveRobotImpl.moveTo(MoveRobotImpl.java:55) at org.testfx.robot.impl.ClickRobotImpl.clickOn(ClickRobotImpl.java:57) at org.testfx.api.FxRobot.clickOn(FxRobot.java:595) at ...

all the tests pass excepted "should_click_on_button(FxRobot)".

here are the test class and my build.gradle. In the test class, an arrow shows the faulty line.

thank you!

test class:

package imports;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.testfx.api.FxRobot;
import org.testfx.framework.junit5.ApplicationExtension;
import org.testfx.framework.junit5.ApplicationTest;

import java.awt.*;
import java.util.concurrent.TimeoutException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.testfx.api.FxAssert.verifyThat;
import static org.testfx.api.FxToolkit.registerPrimaryStage;
import static org.testfx.matcher.control.LabeledMatchers.hasText;

@TestInstance(TestInstance.Lifecycle.PER_METHOD)
@ExtendWith(ApplicationExtension.class)
class ClickableButtonTest extends ApplicationTest {

    @BeforeAll
    static void setUp() throws TimeoutException {
        System.setProperty("testfx.robot", "glass");
        System.setProperty("testfx.headless", "true");
        System.setProperty("prism.order", "sw");
        System.setProperty("prism.text", "t2k");
        System.setProperty("java.awt.headless", "true");
        registerPrimaryStage();

    }



    public void start(Stage stage) {
        Button button = new Button("click me!");
        button.setId("marqueur");
        button.setOnAction(actionEvent -> button.setText("clicked!"));
        stage.setScene(new Scene(new StackPane(button), 100, 100));
        stage.show();
    }

    public Parent getRootNode() {
        ClickableButtonTest app = new ClickableButtonTest();
        return app.getRootNode(); // the root StackPane with button
    }


    @Test
    void should_contain_button() {
        // expect:
        //verifyThat(".button", hasText("click me!"));
        verifyThat("#marqueur", hasText("click me!"));
    }

    @Test
    void should_click_on_button(FxRobot robot) {
        // when:
        robot.clickOn(".button");

        // then:
        verifyThat(".button", hasText("clicked!"));
    }

    @Test
    public void given2Strings_whenEqual_thenCorrect() {
        String a = "foo";
        String b = "FOO";
        assertThat(a, equalToIgnoringCase(b));
    }




}

build.gradle:

buildscript {
    repositories {
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.3'
        classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.8.2'
        classpath 'eu.appsatori:gradle-fatjar-plugin:0.3'
    }
}

plugins {
    id 'java'
    id 'edu.sc.seis.launch4j' version '2.4.4'
}

apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'javafx-gradle-plugin'
apply plugin: 'eu.appsatori.fatjar'

junitPlatform {
    platformVersion '1.0.3'
    reportsDir file('build/test-results/junit-platform')
    enableStandardTestTask true
    //show results summary even on success.
    details details.SUMMARY
    filters {
        tags {
            // Framework tests need to be run only when required to verify that this framework is still working.
            exclude "Framework"
        }
        includeClassNamePatterns '.*Test', '.*Tests'
    }
}



group 'lorry'
version '1'

sourceCompatibility = 1.8
//mainClassName="imports.ColorfulCircles"

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    def final junitVersion = "5.2.0"
    compile group: 'com.google.inject', name: 'guice', version: '4.1.0'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
    compile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitVersion
    //compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.11.0'
    compile group: 'org.assertj', name: 'assertj-core', version: '3.9.0'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: junitVersion
    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.7.22'

    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitVersion

    compile 'org.hamcrest:hamcrest-all:1.3'

    testCompile "org.testfx:testfx-core:4.0.13-alpha"
    testCompile 'org.testfx:testfx-junit5:4.0.13-alpha'
    testRuntime 'org.testfx:openjfx-monocle:8u60-b27'

}

test {
    useJUnitPlatform()
}

test.dependsOn 'clean'

jfx {
    // minimal requirement for jfxJar-task
    //1mainClass = 'imports.ColorfulCircles'
    mainClass = 'imports.ClickableButtonTest'
    // minimal requirement for jfxNative-task
    vendor = 'lolveley'
}

jar {
    baseName = 'executable3'
    version =  ''
    manifest {
        attributes(
                'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                //1'Main-Class': 'imports.ColorfulCircles'
                'Main-Class': 'imports.ColorfulCircles'
        )
    }
}
launch4j {
    outfile='bibliotek-v3.exe'
    mainClassName = 'imports.ColorfulCircles'
    icon = "${projectDir}\\icons\\hands2.ico"
    copyConfigurable = project.tasks.fatJar.outputs.files
    jar = "lib/${project.tasks.fatJar.archiveName}"
    //headerType = "console"
    //jar = "${buildDir}\\productFatJar\\fat.jar"
}

junitPlatformTest {
        jvmArgs = [
                "-Djava.awt.headless=true",
                "-Dtestfx.robot=glass",
                "-Dtestfx.headless=true",
                "-Dprism.order=sw",
                "-Dprism.text=t2k",
                "-Dheadless.geometry=1920x1200-32"
        ]
    }

Aucun commentaire:

Enregistrer un commentaire