使用 maven2 删除嵌套测试类

发布于 2024-07-16 17:44:33 字数 294 浏览 4 评论 0 原文

我使用嵌套类来访问 JUnit 测试中的私有成员。 它们总是被命名为“TestProxy”。

我想在构建时使用 maven2 删除它们,不将其包含到 jar 文件中。

  • 有什么配置选项吗?
  • 可以用插件来完成吗? 如果是这样的话,原型就很好了! ;-)

谢谢

编辑:为什么使用私有方法? 我需要从第 3 方系统注入数据,但无法为每个 JUnit 测试运行调用这些数据。 我真的不想要私有数据的公共设置器,否则迟早另一个程序员可能会滥用它。

I use nested classes for accessing private members in JUnit tests. They are alaways named "TestProxy".

I would like to remove them at Build time using maven2, to not include it into the jar file.

  • Is there any configuration option?
  • Can it be done with a plugin? If so, a prototype would be nice! ;-)

Thanks

Edit: Why use private methods? I need to inject data from 3rd party systems, that just can't be called for every JUnit test run. And i really don't want a public setter for private data, or sooner or later another programmer may misuse it.

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

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

发布评论

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

评论(2

初见你 2024-07-23 17:44:33

答案是这样的:可以用maven来配置。 只需将以下代码插入到文件 pom.xml 的 build/plugins 部分:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-jar-plugin</artifactId>
 <configuration>
   <excludes>
     <exclude>**/*$TestProxy*</exclude>
   </excludes>
 </configuration>
</plugin>

@see the Documentation: http://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html#excludes

Here is the answer: It can be configured with maven. Just insert the following code into the file pom.xml in the build/plugins section:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-jar-plugin</artifactId>
 <configuration>
   <excludes>
     <exclude>**/*$TestProxy*</exclude>
   </excludes>
 </configuration>
</plugin>

@see the Documentation: http://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html#excludes

枫林﹌晚霞¤ 2024-07-23 17:44:33

对于此类事情,您可能会发现 ant 脚本是第一个调用端口,而不是专用的自定义插件。 让 Maven 在构建过程中运行任意 ant 脚本很简单。 比创建插件更简单。 这个问题很好地描述了如何使用 Maven 的 process-sources 构建执行阶段运行 ant 脚本。

稍微不同的是,我希望您会得到一堆答案,询问您为什么要对可私有访问的类成员进行单元测试......

For this sort of thing you might find that an ant script is the first port of call rather than a dedicated custom plugin. It is simple to get Maven to run arbitrary ant scripts as part of the build. Simpler than creating a plugin. This question has an excellent description of how to run an ant script with Maven's process-sources build execution phase.

On a slightly different note, I expect you will get a bunch of answers asking you why you are unit testing privately accessible class members...

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