Extension to run tests with classpath customizations.
The main goal of this project is to allow to write tests (most probably integration ones) against various classpaths without the need to create complex configurations astride build tool and code.
For example, testing a library behavior without an optional dependency.
@Test
void junit_extension_can_be_loaded() throws ClassNotFoundException {
assertThat(Class.forName("org.junit.jupiter.api.extension.Extension")).isExactlyInstanceOf(Class.class);
}
@Test
@ModifiedClasspath(excludeDependencies = "junit-jupiter-api")
void junit_extension_cannot_be_loaded() {
assertThatExceptionOfType(ClassNotFoundException.class)
.isThrownBy(() -> Class.forName("org.junit.jupiter.api.extension.Extension"));
}
@CompatibilityTestWithClasspath(dependencies = {
"spring-rabbit:[1.7.7.RELEASE, 2.0.14.RELEASE, 2.2.4.RELEASE]"
})
void amqp_basic_get() {
String messageBody = "Hello world!";
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AmqpConfiguration.class)) {
rabbitTemplate.convertAndSend(EXCHANGE_NAME, "test.key1", messageBody);
Message message = rabbitTemplate.receive(QUEUE_NAME);
assertThat(message).isNotNull();
assertThat(message.getBody()).isEqualTo(messageBody.getBytes());
}
}
Use the maven-invoker-plugin
with pom.xml template (see an example here).
Currently this extension uses a workaround to get things done, but it is waiting for JUnit5 #201 to get a cleaner approach at this.
Next things to do:
- Replace dependencies by other ones (different versions or implementations)
- Support other Build Tools (Gradle, SBT, Ivy, etc.)
- Make the annotation
- available at class level
- work in
@Nested
tests - work in conjunction with injection / test-templates (may require the classloader extension)
- repeatable, so that the same test can be expected to work against various classpath (different version of a library per se)
Any contribution is greatly appreciated.
Add the following dependency to your pom.xml
<dependency>
<groupId>com.github.fridujo</groupId>
<artifactId>classpath-junit-extension</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
Add the following dependency to your build.gradle
repositories {
mavenCentral()
}
// ...
dependencies {
// ...
testCompile('com.github.fridujo:classpath-junit-extension:1.0.0')
// ...
}
You need JDK-8+ (at least) to build this extension. The project can be built with Maven using the following command.
mvn clean package
The project can be installed in a local Maven Repository for usage in other projects via the following command.
mvn clean install
The master of the project pushes SNAPSHOTs in Sonatype's repo.
To use the latest master build add Sonatype OSS snapshot repository, for Maven:
<repositories>
...
<repository>
<id>sonatype-oss-spanshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
For Gradle:
repositories {
// ...
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}