Skip to content

Repository files navigation

Testjars

Build Status

If you need a bunch of jar files you need to use in your tests, not as dependencies, but as artifacts you need to use in the tests, you may have wondered how to do that without resorting to storing the jars in source control or having an elaborate build set up where test archives would be built prior to the tests needing them.

This library brings simplicity to the picture. No need to invoke the compiler yourself, automatic cleanup taken care of.

JUnit 5

@ExtendWith(CompiledJarExtension.class)
classMyTestClass {
@JarSources(root = "/sources-on-classpath/", sources = {"a/MyClass.java", "b/MyOtherClass.java"})
privateCompiledJarjarFile;
@Testvoidtest() {
//directory containing the compiled classesjarFile.classes();
//the actual jar file containing the compiled sourcesjarFile.jarFile();
CompiledJar.Environmentenv = jarFile.analyze();
//analyze the compiled classes as if in annotation processorTypeElementmyClass = env.elements().getTypeElement("a.MyClass");
}
}

Dependencies

It is possible to declare dependencies between the jars or declare a dependency on a 3rd party archive using some resolver.

Declaring dependencies between 2 compiled jars is as simple as assigning names to the jars and then using those names in the @Dependencies annotation like this:

@ExtendWith(CompiledJarExtension.class)
classMyTestClass {
@JarSources(name = "core", root = "/sources-on-classpath/", sources = {"a/MyClass.java", "b/MyOtherClass.java"})
privateCompiledJarcoreJar;
@JarSources(root = "/sources-on-classpath/", sources = {"a/MyClass.java", "b/MyOtherClass.java"})
@Dependencies({"core"})
privateCompiledJarimplJar;
// ...
}

Using a custom resolver is also possible:

@ExtendWith(CompiledJarExtension.class)
classMyTestClass {
@JarSources(root = "/sources-on-classpath/", sources = {"a/MyClass.java", "b/MyOtherClass.java"})
@Dependencies(resolver = MavenCacheDependencyResolver.class, value = {"com.acme:acme-api:42.0"})
privateCompiledJarjar;
// ...
}

JUnit 4

publicclassMyTestClass {
@RulepublicJarjar = newJar();
@Testpublicvoidtest() {
CompiledJarjarFile = jar.from()
.classpathSources("/sources-on-classpath/", "a/MyClass.java", "b/MyOtherClass.java")
.build();
// now it works the same as above
}
}

Dependencies

publicclassMyTestClass {
@RulepublicJarjar = newJar();
@Testpublicvoidtest1() {
CompiledJarjarFile = jar.from(newMavenCacheDependencyResolver())
.classpathSources("/sources-on-classpath/", "a/MyClass.java", "b/MyOtherClass.java")
.dependencies("com.acme:my-dep:42.0", "com.google:guava:132")
.build();
// ...
}
@Testpublicvoidtest1() {
CompiledJarbaseJar = jar.from()
.classpathSources("/sources-on-classpath/", "a/MyClass.java", "b/MyOtherClass.java")
.build();
CompiledJarjarFile = jar.from(newMavenCacheDependencyResolver())
.classpathSources("/sources-on-classpath/", "dep/DepClass.java")
.dependencies(baseJar.jarFile())
.build();
// ...
}
}

About

Easily compile jars from source inside the unit tests

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages