https://github.com/fridujo/classpath-junit-extension
JUnit5 extension to run tests with classpath customizations
https://github.com/fridujo/classpath-junit-extension
classpath hacktoberfest junit5
Last synced: 7 months ago
JSON representation
JUnit5 extension to run tests with classpath customizations
- Host: GitHub
- URL: https://github.com/fridujo/classpath-junit-extension
- Owner: fridujo
- License: apache-2.0
- Created: 2019-12-13T23:17:38.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-07-08T04:26:57.000Z (over 1 year ago)
- Last Synced: 2025-04-06T06:34:36.067Z (10 months ago)
- Topics: classpath, hacktoberfest, junit5
- Language: Java
- Homepage:
- Size: 190 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Classpath modification extension for JUnit5
[](https://travis-ci.com/fridujo/classpath-junit-extension)
[](https://codecov.io/gh/fridujo/classpath-junit-extension/)
[](https://search.maven.org/#search|ga|1|a:"classpath-junit-extension")
[](https://jitpack.io/#fridujo/classpath-junit-extension)
[](https://opensource.org/licenses/Apache-2.0)
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
```java
@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
```java
@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](https://github.com/fridujo/rabbitmq-mock/blob/78cd20380ea46089193dfbf5e29efd55798343ee/pom.xml#L163)).
## Roadmap
Currently this extension uses a _workaround_ to get things done, but it is waiting for [JUnit5 #201](https://github.com/junit-team/junit5/issues/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.
[](https://gitpod.io/#github.com/fridujo/classpath-junit-extension.git)
## Getting Started
### Maven
Add the following dependency to your **pom.xml**
```xml
com.github.fridujo
classpath-junit-extension
1.0.0
test
```
### Gradle
Add the following dependency to your **build.gradle**
```groovy
repositories {
mavenCentral()
}
// ...
dependencies {
// ...
testCompile('com.github.fridujo:classpath-junit-extension:1.0.0')
// ...
}
```
### Building from Source
You need [JDK-8+](http://jdk.java.net/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:
```
...
sonatype-oss-spanshots
https://oss.sonatype.org/content/repositories/snapshots
```
For Gradle:
```groovy
repositories {
// ...
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}