如何在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.
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 bothjunit.jarandhamcrest-core.jarinto 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 likeC:\Java\projects\calc. Open Command Prompt, navigate to that folder withcd C:\Java\projects\calc, then run:javac -cp "C:\Java\libs\junit.jar;C:\Java\libs\hamcrest-core.jar;." Calculator.java CalculatorTest.javaBreakdown 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 CalculatorTestYou'll see output showing passed/failed tests right in the command prompt.
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 toTools > Build System > New Build System.... Replace the default JSON with this (adjust the JAR paths to match yourC:\Java\libsfolder 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 yourUserbuild systems folder.Step 2: Run tests in Sublime
Open your test file (e.g.,CalculatorTest.java) in Sublime. Go toTools > Build Systemand selectJUnit. PressCtrl+Bto compile and run your tests. The results will appear in the console at the bottom of the Sublime window.
- 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.*;andimport 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

