Skip to content

Instantly share code, notes, and snippets.

@SmallEndian
Last active August 27, 2021 12:56
Show Gist options
  • Select an option

  • Save SmallEndian/06f5ed6c95a8a5109d339236c3e89339 to your computer and use it in GitHub Desktop.

Select an option

Save SmallEndian/06f5ed6c95a8a5109d339236c3e89339 to your computer and use it in GitHub Desktop.
Using JUnit in a Java Project

Using JUnit in a Java Project

How to setup everything when you don't use either or

  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ

How I compile my Java code

.PHONY: all clean 

all:
	@mkdir -p bin
	@javac -classpath "lib/*" -d bin `find src -name "*.java"`

clean:
	rm -f `find . -name "*.class"`

Of course a Gradle script would probably be much more efficient, and I could use Makefile's partial compilation features. But I just don't want to. This is the kind of project where I intend to spend zero time on complilation details. This Makefile is quite short, but I can guarantee that it will work on any unix-like machine where Java is installed.

Running tests on the Console

I'm using to CLI to compile my code, so it stands to reason that I also want to run tests from the CLI. This is what ConsoleLauncher is for, and the latest standalone version should be available here.

Then it's just a matter of making sure that the latest *.jar is in the lib/ directory and

java -jar lib/junit-platform-console-standalone-1.7.2-all.jar -cp "bin:lib/*"  --scan-classpath

ConsoleLauncher with the --scan-classpath option will find all classes that have test in their name and run them.

package test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class SimpleTest {
    @Test
    @DisplayName("🏹➡️🍎")
    void unicode_is_so_useful() {
        assertEquals(7, 5 + 2);
    }
}
t:all
	@java -jar lib/junit-platform-console-standalone-1.7.2-all.jar -cp "bin:lib/*"  --scan-classpath

For a cleaner stdout/stderr output

t:all
	@java -jar lib/junit-platform-console-standalone-1.7.2-all.jar -cp "bin:lib/*"  --scan-classpath --config=junit.platform.output.capture.stdout=true --config=junit.platform.output.capture.stderr=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment