如何从Maven的主文件夹访问测试类? [英] How can I access a test class from Maven's main folder?

查看:61
本文介绍了如何从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的主文件夹访问测试类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆