使用 IntelliJ IDEA 设置 JUnit [英] Setting up JUnit with IntelliJ IDEA

查看:41
本文介绍了使用 IntelliJ IDEA 设置 JUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

熟悉Java但不熟悉IntelliJ,如何开始"JUnit集成?

Familiar with Java but unfamiliar with IntelliJ, how does one "get started" with JUnit integration?

灵感来自 寻找一个关于在 Intellij IDEA 9.x 中使用 JUnit 的教程,该教程没有回答我的问题,而是针对旧版 IntelliJ.

Inspired by Looking for a tutorial on using JUnit with Intellij IDEA 9.x which didn't answer my questions and was for an older version of IntelliJ.

推荐答案

基本上,您只需要类路径中的 junit.jar - 这里有一个快速的方法:

Basically, you only need junit.jar on the classpath - and here's a quick way to do it:

  1. 确保将源文件夹(例如 test)标记为 Test Root.

创建一个测试,例如像这样:

Create a test, for example like this:

public class MyClassTest {
    @Test
    public void testSomething() {

    }
}

  • 由于您还没有配置 junit.jar(还没有),@Test 注释将被标记为错误(红色),按 f2 导航到它.

  • Since you haven't configured junit.jar (yet), the @Test annotation will be marked as an error (red), hit f2 to navigate to it.

    按 alt-enter 并选择 将 junit.jar 添加到类路径

    Hit alt-enter and choose Add junit.jar to the classpath

    好了,大功告成!右键单击您的测试并选择 Run 'MyClassTest' 以运行它并查看测试结果.

    There, you're done! Right-click on your test and choose Run 'MyClassTest' to run it and see the test results.

    Maven 注意:或者,如果您使用的是 Maven,则在第 4 步您可以选择选项添加 Maven 依赖项...,转到搜索工件 窗格中,输入 junit 并选择哪个版本(例如 4.8 或 4.9).

    Maven Note: Altervatively, if you're using maven, at step 4 you can instead choose the option Add Maven Dependency..., go to the Search for artifact pane, type junit and take whichever version (e.g. 4.8 or 4.9).

    这篇关于使用 IntelliJ IDEA 设置 JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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