如何从Maven的主文件夹访问测试类?

编程入门 行业动态 更新时间:2024-10-09 04:19:13
本文介绍了如何从Maven的主文件夹访问测试类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用标准文件夹结构-即src/main/java,src/test/java等创建了一个Maven项目.

I created a Maven project with the standard folder strucure - i.e. src/main/java, src/test/java, etc.

我写了一个ClassA类和一个测试类TestA.

I wrote a class ClassA and a test class TestA.

在ClassA的主程序中,我引用了TestA的静态成员.

From the main program of ClassA, I refer to a static member of TestA.

代码可以编译,但是当我运行它时,我得到:NoClassDefFoundError:TestA.

The code compiles, but when I run it, I get: NoClassDefFoundError: TestA.

如何从ClassA中访问TestA?

How can I access TestA from within ClassA?

推荐答案

我建议不要重新考虑您的测试设计,而不是直接解决您的问题. Maven完全有能力自行运行测试,只需输入

Instead of solving your issue directly, I would advise to rethink your test design. Maven is perfectly capable of running tests on its own, just enter

mvn test

在命令行上.如果要运行一个测试类,请输入

on the command line. If you want to run a single test class, enter

mvn test -Dtest=MyTest

对于一种测试方法,请使用

for a single test method, use

mvn test -Dtest=MyTest#shouldRunPerfectly

它还支持通配符,因此要运行一些常规测试,您可以输入

It also supports wildcards, so to run some common tests, you could type

mvn test -Dtest=Integration*#shouldBeFaster*.

大多数IDE允许通过快捷方式直接运行测试.如果我没记错的话,它是 Shift + Alt + X ,然后是 T 用于Eclipse, Shift + Ctrl + F10 . IntelliJ还使用 Ctrl + Shift + T 快捷方式导航到正在使用的类的测试

Most IDEs allow to run tests directly by a shortcut. If I recall correctly, It's Shift+Alt+X then T for Eclipse and Shift+Ctrl+F10 for IntelliJ. IntelliJ also uses the Ctrl+Shift+T shortcut to navigate to the test of the class you are working with.

Maven目录结构强调了测试与应用程序的分离,这使得执行计划中的工作变得更加困难.

Maven directory structure emphasizes the separation of the tests from the application and makes it much harder to do what you are planning to.

tl; dr-以行之有效的方式

tl;dr - do it the maven way

更多推荐

如何从Maven的主文件夹访问测试类?

本文发布于:2023-11-28 10:44:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主文   测试   Maven

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!