如何在Java中的graphics2d上使用Mockito.Spy 17

发布于 2025-02-06 12:41:58 字数 1672 浏览 2 评论 0原文

我有一个Maven项目,应该在Java-8(Java-11)上运行。我想增加对Java-17的支持,并且在测试方面有一些问题。

测试的简化版本看起来像:

@Test
public void testGraphics() throws Exception {
  BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
  Graphics2D graphics = Mockito.spy(bi.createGraphics());

  //my code for testing
  graphics.getColor();

  Mockito.verify(graphics, times(1)).getColor();
}

我执行测试:

mvn clean compile test

在Java 8和Java 11上可以正常工作。但是,在Java 17中,我得到:

java.lang.IllegalAccessException: class org.mockito.internal.util.reflection.ReflectionMemberAccessor cannot access class sun.java2d.SunGraphics2D (in module java.desktop) because module java.desktop does not export sun.java2d to unnamed module @62bd2070

可以通过添加-Add-opens = Java.desktop/可以解决此问题。 sun.java2d = test命令的全命名

mvn clean compile test -DargLine=--add-opens=java.desktop/sun.java2d=ALL-UNNAMED

但是,此命令在Java 8上不起作用,因为它无法识别- add-oppens选项:

Unrecognized option: --add-opens=java.desktop/sun.java2d=ALL-UNNAMED
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

我的连续集成是矩阵在不同的环境(不同的Java版本,不同的数据库等)中运行相同的测试,我宁愿避免根据环境具有不同的Maven选项。

是否有另一种运行此测试的方法不需要在不同的Java版本中更改Maven参数?我不能使用mockito.mock,因为我的代码与图形对象相互作用并取决于结果(并且有很多调用,因此嘲笑每个呼叫是目前的选择)。以下是我考虑的选项,但找不到方法:

  • 使用Graphics2d的不同实现而不是Sun.java2d.sungraphics2d
  • ,以有条件地为Surefire插件提供了选择
  • motify pom.xml

对于记录,我使用Junit 4.12和Mockito 4.6.1

I have a maven project that should run on java-8, java-11. I want to add support for java-17 and I have some issues with testing.

Simplified version of the test looks like:

@Test
public void testGraphics() throws Exception {
  BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
  Graphics2D graphics = Mockito.spy(bi.createGraphics());

  //my code for testing
  graphics.getColor();

  Mockito.verify(graphics, times(1)).getColor();
}

I execute test with:

mvn clean compile test

This works fine on java 8 and java 11. However, in java 17 I get:

java.lang.IllegalAccessException: class org.mockito.internal.util.reflection.ReflectionMemberAccessor cannot access class sun.java2d.SunGraphics2D (in module java.desktop) because module java.desktop does not export sun.java2d to unnamed module @62bd2070

This can be fixed by adding --add-opens=java.desktop/sun.java2d=ALL-UNNAMED to test command:

mvn clean compile test -DargLine=--add-opens=java.desktop/sun.java2d=ALL-UNNAMED

However, this command would not work on java 8 because it does not recognize --add-opens option:

Unrecognized option: --add-opens=java.desktop/sun.java2d=ALL-UNNAMED
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

My continuous integration is a matrix of running the same tests in different environments (different java version, different databases, etc) and I would prefer to avoid having different maven options depending on the environment.

Is there another way to run this test that does not require changing maven parameters in different java versions? I cannot use Mockito.mock because my code interacts with the graphics object and depends on the result (and there are many calls so mocking each one of them is not an option for the moment). Here are options that I considered but could not find a way to do it:

  • use different implementation of Graphics2D instead of sun.java2d.SunGraphics2D
  • modify pom.xml to conditionally provide options to surefire plugin depending on the java version
  • force surefire to ignore unrecognized options

For the record I'm using junit 4.12 and mockito 4.6.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱我好吗 2025-02-13 12:41:58

一种方法是为Surefire提供一个选项,要求Java忽略未认可的选项。可以使用以下命令完成:

mvn clean compile test -DargLine="--add-opens=java.desktop/sun.java2d=ALL-UNNAMED -XX:+IgnoreUnrecognizedVMOptions"

One way of doing it is to provide surefire with an option that asks java to ignore unrecognized option. It can be done using following command:

mvn clean compile test -DargLine="--add-opens=java.desktop/sun.java2d=ALL-UNNAMED -XX:+IgnoreUnrecognizedVMOptions"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文