java - Spock通过Maven启动后测试不成功

标签 java maven spock

独立运行(在 IDE 中)时测试成功,但通过 Maven 触发时抛出:groovy.lang.MissingPropertyException

我调试了 mvn test 并发现 gameObject 在通过 Maven 触发时没有在运行时定义的字段。具体异常信息为:

groovy.lang.MissingPropertyException:没有这样的属性:类的游戏对象:org.monterinio.games.asteroids.player.view.PlayerView

测试文件夹:src/test/groovy

包含(除其他外)org.monterinio.games.asteroids.player.controller.action.BasicActionHandlerTest

Src文件夹:src/main/java

包含(除其他外):

package org.monterinio.games.asteroids.player.view;

public class PlayerView extends GameObjectView {

  public Player player;

  public PlayerView(Node view, Player gameObject) {
      super(view);
      this.player = gameObject;
  }
}


package org.monterinio.games.asteroids.player.model;

public class Player extends GameObject {

  private String name;
  private double angle;
  private List<Bullet> bullets;
  private AbstractMovementSignals movementSignals;
  private AbstractRotationSignals rotationSignals;
  private AbstractActionSignals actionSignals;

  public Player(String name) {
    this.name = name;
    this.movementSignals = new BasicMovementSignals();
    this.rotationSignals = new BasicRotationSignals();
    this.actionSignals = new BasicActionSignals();
    this.bullets = new ArrayList<>();
  }
}

我当前的pom.xml:

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11.0.2</version>
    </dependency>
    <dependency>
        <groupId>com.airhacks</groupId>
        <artifactId>afterburner.fx</artifactId>
        <version>1.7.0</version>
    </dependency>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>

    <!-- TEST -->
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>1.3-groovy-2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testfx</groupId>
        <artifactId>testfx-core</artifactId>
        <version>4.0.15-alpha</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testfx</groupId>
        <artifactId>testfx-spock</artifactId>
        <version>4.0.15-alpha</version>
        <scope>test</scope>
    </dependency>
    <!-- FOR MOCKING IN SPOCK -->
    <dependency>
        <groupId>net.bytebuddy</groupId>
        <artifactId>byte-buddy</artifactId>
        <version>1.9.12</version>
    </dependency>
</dependencies>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>dashboard.fxml</include>
                <include>dashboard.css</include>
            </includes>
            <targetPath>org/monterinio/games/asteroids</targetPath>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M3</version>
            <configuration>
                <testSourceDirectory>src/test/groovy</testSourceDirectory>
                <includes>
                    <include>**/*Spec.java</include>
                    <!-- Yes, .java extension -->
                    <include>**/*Test.java</include>
                    <!-- Just in case having "normal" JUnit tests -->
                </includes>
                <argLine>--illegal-access=deny</argLine>
                <argLine>--add-exports javafx.graphics/com.sun.javafx.application=ALL-UNNAMED</argLine>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>${gmaven-plugin.version}</version>
            <executions>
                <execution>
                    <!-- Without joint compilation - no dependencies between Java and Groovy (inheritance)-->
                    <goals>
                        <goal>addTestSources</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-all</artifactId>
                    <version>${groovy.version}</version>
                    <type>pom</type>
                </dependency>
            </dependencies>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>org.monterinio.games.asteroids.MainAsteroids</mainClass>
            </configuration>
        </plugin>
    </plugins>

我预计测试由 Maven 执行时会成功。

最佳答案

打开正在测试的包解决了问题:

package.info

...
// needs to be opened for testing
opens org.monterinio.games.asteroids.player.model;
opens org.monterinio.games.asteroids.player.model.rotation;
...

或者简单地打开整个模块:

open module asteroids {
  ...
}

关于java - Spock通过Maven启动后测试不成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55892765/

相关文章:

java - 如何在 IntelliJ 中运行 Java .jar 文件?错误 - 参数 : archive-name [archive-type]

Maven Antrun 不执行任务

groovy - 仅运行测试设置一次

java - 如何在jtextarea中选择一行

java string.getBytes ("UTF-8") 等效的 javascript

java - 检查 JDBC String/VARCHAR 比较中的转义 %?

java - 本地化路由和 URL 字符串

maven - tomcat7-maven-plugin/tomcat7 :run where to store custom context. xml?

groovy - 页面事件后如何使用Geb检查元素属性值

groovy - Geb配置