classpath-junit-extension

JUnit5 extension to run tests with classpath customizations

License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

com.github.fridujo
ArtifactId

ArtifactId

classpath-junit-extension
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

classpath-junit-extension
JUnit5 extension to run tests with classpath customizations
Project URL

Project URL

https://github.com/fridujo/classpath-junit-extension
Source Code Management

Source Code Management

https://github.com/fridujo/classpath-junit-extension.git

Download classpath-junit-extension

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.fridujo/classpath-junit-extension/ -->
<dependency>
    <groupId>com.github.fridujo</groupId>
    <artifactId>classpath-junit-extension</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.fridujo/classpath-junit-extension/
implementation 'com.github.fridujo:classpath-junit-extension:1.0.0'
// https://jarcasting.com/artifacts/com.github.fridujo/classpath-junit-extension/
implementation ("com.github.fridujo:classpath-junit-extension:1.0.0")
'com.github.fridujo:classpath-junit-extension:jar:1.0.0'
<dependency org="com.github.fridujo" name="classpath-junit-extension" rev="1.0.0">
  <artifact name="classpath-junit-extension" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.fridujo', module='classpath-junit-extension', version='1.0.0')
)
libraryDependencies += "com.github.fridujo" % "classpath-junit-extension" % "1.0.0"
[com.github.fridujo/classpath-junit-extension "1.0.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.apache.maven : maven-core jar 3.6.3
org.assertj : assertj-core jar 3.14.0

provided (1)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.5.2

test (1)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter jar 5.5.2

Project Modules

There are no modules declared in this project.

Classpath modification extension for JUnit5

Build Status Coverage Status Maven Central JitPack License

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.

Testing 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"));
}

Testing retro-compatibility

@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());
    }
}

Alternatives

Use the maven-invoker-plugin with pom.xml template (see an example here).

Roadmap

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)

Contribute

Any contribution is greatly appreciated.

Open in Gitpod

Getting Started

Maven

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>

Gradle

Add the following dependency to your build.gradle

repositories {
	mavenCentral()
}

// ...

dependencies {
	// ...
	testCompile('com.github.fridujo:classpath-junit-extension:1.0.0')
	// ...
}

Building from Source

You need JDK-8+ (at least) to build this extension. The project can be built with Maven using the following command.

mvn clean package

Installing in the Local Maven Repository

The project can be installed in a local Maven Repository for usage in other projects via the following command.

mvn clean install

Using the latest SNAPSHOT

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"
    }
}
com.github.fridujo

Fridujo

It's Esperanto for freezer

Versions

Version
1.0.0