You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows上配置JUnit,适配Sublime Text 3与命令行运行测试

Hey there! Let's get JUnit set up properly on Windows for both command-line testing and Sublime Text 3. Since you already have junit.jar and hamcrest-core.jar ready, we just need to wire everything up step by step.

1. Command-Line Configuration

First, let's get the command line working—this is the foundation, and it'll make setting up Sublime easier too.

  • Step 1: Organize your JAR files
    Create a dedicated folder to store your JUnit libraries (e.g., C:\Java\libs). Move both junit.jar and hamcrest-core.jar into this folder. Keeping dependencies in a fixed spot avoids messy paths later.

  • Step 2: Compile your code and tests
    Suppose your source file (e.g., Calculator.java) and test file (e.g., CalculatorTest.java) are in a project folder like C:\Java\projects\calc. Open Command Prompt, navigate to that folder with cd C:\Java\projects\calc, then run:

    javac -cp "C:\Java\libs\junit.jar;C:\Java\libs\hamcrest-core.jar;." Calculator.java CalculatorTest.java
    

    Breakdown of the command:

    • -cp = classpath: tells Java where to find JUnit and your own classes.
    • Semicolons (;) separate paths on Windows (use colons : on Mac/Linux).
    • The . includes the current directory so Java can locate your source files.
  • Step 3: Run the tests
    Once compilation succeeds, execute the tests with:

    java -cp "C:\Java\libs\junit.jar;C:\Java\libs\hamcrest-core.jar;." org.junit.runner.JUnitCore CalculatorTest
    

    You'll see output showing passed/failed tests right in the command prompt.

2. Sublime Text 3 Configuration

Let's create a custom build system so you can run tests directly from Sublime with a single keypress.

  • Step 1: Create a JUnit build system
    Open Sublime, go to Tools > Build System > New Build System.... Replace the default JSON with this (adjust the JAR paths to match your C:\Java\libs folder if you used a different location):

    {
        "cmd": ["javac", "-cp", "\"C:\\Java\\libs\\junit.jar;C:\\Java\\libs\\hamcrest-core.jar;.\"", "$file", "${file_base_name}Test.java", "&&", "java", "-cp", "\"C:\\Java\\libs\\junit.jar;C:\\Java\\libs\\hamcrest-core.jar;.\"", "org.junit.runner.JUnitCore", "${file_base_name}Test"],
        "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
        "selector": "source.java",
        "shell": true,
        "working_dir": "${file_path}"
    }
    

    Save the file as JUnit.sublime-build—Sublime should auto-save it to your User build systems folder.

  • Step 2: Run tests in Sublime
    Open your test file (e.g., CalculatorTest.java) in Sublime. Go to Tools > Build System and select JUnit. Press Ctrl+B to compile and run your tests. The results will appear in the console at the bottom of the Sublime window.

Quick Troubleshooting
  • Class not found errors: Double-check the classpath includes the current directory (.) and the correct paths to your JAR files. Typos in paths are a common culprit!
  • Compilation fails: Ensure your test classes import JUnit correctly (e.g., import static org.junit.Assert.*; and import org.junit.Test;).
  • Sublime build doesn't run: Make sure you have the test file active (not the source file) when pressing Ctrl+B, and verify the JAR paths in the build system JSON use double backslashes (\\) for Windows paths.

内容的提问来源于stack exchange,提问作者ek9

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:08:48