IntelliJ:运行单个方法,就像 BlueJ 那样

Hob*_*mok 6 java intellij-idea intellij-plugin bluej

所以我目前使用 IntelliJ(2017-3,如果这很重要),但在我当前的项目中,我需要测试具有特定输入(主要是文本操作)的单个方法,所以我记得从学校回来的 BlueJ,在那里你可以创建对象并运行无需编写任何额外代码的单一方法。所以,我想知道,是否有插件或其他解决方法可以在 IntelliJ 中为我提供该功能?

(使用 BlueJ 并行破坏了 IntelliJ 的项目,所以这不是一个可悲的选择)

小智 1

您可以将其设置为 Maven 项目,并使用 JUnit 来运行每个方法作为测试。

要将现有项目转换为 Maven 项目,请右键单击项目模块,单击“添加框架支持”,然后选中 Maven 框。

这应该生成一个 pom.xml。添加包含 JUnit 依赖项的部分:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

然后在测试源文件夹中创建一个包含您的方法的类,并向每个类添加“@Test”注释。之后您应该能够单独运行每个方法。