测试在另一次测试期间生成的 Java 代码

发布于 2024-08-15 03:28:54 字数 286 浏览 8 评论 0原文

我想在我的模型上构建一个“toJavaCode()”,它将生成生成该模型所需的 Java 源代码(不要介意原因或是否应该这样做,也不要介意可能发生的兼容性问题)。

我不知道如何测试这个。我正在使用 Maven,但生成源对我来说并不真正有用,因为我的服务器需要进行适当的批量测试。我确实在“测试”目标期间启动了服务器,但生成源还为时过早。

另一方面,虽然我可以使用内置编译器(来自 JDK 中的 tools.jar)来执行此操作,但我不知道如何将其打包到 jar 中进行测试(或加载该 jar)。

有什么想法吗?

I want to build a "toJavaCode()" on my model that would generated the required Java source code to generate that model (never mind the reasons or if it should or shouldn't be done, nor the compatibility issues that may occur).

I'm at a loss at how to test this. I'm using maven, but generate-sources won't really work for me since my server needs to be up for proper, bulk testing. I do get the server up during the "test" goal, but generate-sources is just too early.

On the other hand, while I can use the built in compiler (from tools.jar in the JDK) to do this, I don't know how I can pack it into the jar for testing (or load that jar).

Any ideas?

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

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

发布评论

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

评论(2

静谧 2024-08-22 03:28:54

您可以使用 JavaCompiler API 进行编译您的源文件并设置 类加载器 到在测试中加载已编译的类(示例代码)。 tools.jar 必须位于类路径上。如果使用JDK就是这种情况。

如果您生成的代码对于任何给定的类都是稳定的,您可以使用 注释处理器生成源代码并在与注释类相同的javac运行中对其进行编译。

You can use the JavaCompiler API to compile your source files and setup a classloader to load the compiled classes in your test (sample code). tools.jar has to be on the classpath. This is the case if the JDK is used.

If your generated code is stable for any given class you could use annotation processor to generate the source code and compile it in the same javac run as the annotated class.

寂寞美少年 2024-08-22 03:28:54

您可以将 ant 任务添加到您的 Maven 中。这是在 Maven 构建过程中实现“不符合经典顺序”的一种方法。就像向 Maven 测试目标添加 javac ant 任务一样。

(抱歉,我和你的评论者 matt b 一样困惑 - 但嵌入式 ant 任务是你的瑞士军刀。)

You can add ant tasks to your maven. That's a way to something 'out-of-classical-order' during a maven build. Like adding a javac ant task to mavens test goal or so.

(sorry, I'm as confused as your commentor matt b - but the embedded ant tasks are your swiss army knife here.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文