How to use JUnit with Sublime Text 3

2019-07-07 01:00发布

问题:

This is a two part question. I'm trying to use JUnit for a project I'm working on in Sublime Text 3. The file setup I have looks somewhat like this

Vulcan/
 Vulcan.sublime-project
 bin/
  ...
 src/
  core/
   org/
    vulcan/
     geom/
      Vector.java
      ... (other similar files in the same package)
  tests/
   org/
    vulcan/
     geom/
      VectorTest.java
      ... (relevant test files to match core/org/vulcan/geom/)

At the top of Vector.java and VectorTest.java, I have the line package org.vulcan.geom. Now, in my sublime-project file, I have set the source folder to Vulcan/src/core/, so that when I build it, Vector.java is compiled to bin/org/vulcan/geom/Vector.class, but what this means is that upon building the whole project, the tests/ folder gets ignored and is not built to bin/.

I attempted to fix this by setting the source folder to Vulcan/src/, but that causes a bunch of errors in org/vulcan/geom/, saying that it cannot find any of the class names in that package (I assume the fix for this would be to change the package from org.vulcan.geom to core.org.vulcan.geom but that would be a tedious process).

Question 1: So how can I set up my project such that the tests/ get compiled along with the core/ when I build the whole project?

Question 2: With this file structure, how would I run the tests in tests/ with JUnit (each .java file in tests/ is a test class runnable by JUnit)? I've read the tutorials and I see that I can either run them from the command line (which I'm not quite sure how to do), or I can create a class whose main method runs all the tests. If I choose the latter option, where would I put the "runner" class? Would I put it in tests/org/vulcan/geom/, or in tests/org/vulcan/, or in tests/org/? And how would I run it from Sublime Text 3?

回答1:

About question 1:

  • install Package Control
  • install Sublime Linter
  • install Sublime Linter javac

edit Vulcan.sublime-project and add the following

"SublimeLinter": 
{
    "linters": 
    {
        "javac": 
        {
            "args": 
            [
                "-cp",
                "/usr/share/java/junit4.jar",
                "-sourcepath",
                "${project}/src/core:${project}/src/tests"
            ]
        }
    }
}

In the -cp you can add whatever .jar libraries are needed for compilation. In the -sourcepath you can add multiple sources of your project (beware to use unique package names). Both can take multiple paths separated by ":".

Those two parameters are actually javac cli parameters, so you can look up javac man page for more functionality.

If you use the setup above, every time you save a .java file in sublime, javac will compile it and create a .class file in the same dir. Sublime linter will show you compilation errors and warnings in the sidebar gutter.

About question 2:

If you want to test everything that's inside vulcan, TestRunner.java should be in tests/org/vulcan. If you want to test everything that's inside geom TestRunner.java should be in tests/org/vulcan/geom.

TestRunner.java example - http://www.vogella.com/tutorials/JUnit/article.html#juniteclipse_code

But for running the tests, you must save all your files so they get compiled and after run

cd .../Vulcan/src/tests
java -cp .:/usr/share/java/junit4.jar org.vulcan.TestRunner

Out of topic - why use sublime for java:

If you use less autocomplete, autoimport, copy/paste - you learn faster, your attention to details become better and in the long run you are writing correct code faster.