Skip to content

fridujo/classpath-junit-extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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"
    }
}