在独立运行(在IDE中)时测试成功,但是在通过Maven触发时抛出:groovy.lang.MissingPropertyException
我调试了mvn test
,发现gameObject
在通过Maven触发时在运行时没有定义任何字段。具体的异常消息是:
groovy.lang.MissingPropertyException: No such property: gameObject for class: 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执行测试时会成功。
答案 0 :(得分:0)
打开正在测试的软件包可以解决问题:
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 {
...
}