https://github.com/yegor256/farea
Fake Maven Reactor for unit-testing of your Maven plugins: similar Maven Invoker Plugin, but JUnit friendly
https://github.com/yegor256/farea
integration-testing java maven maven-invoker maven-invoker-plugin maven-plugin testing
Last synced: about 1 month ago
JSON representation
Fake Maven Reactor for unit-testing of your Maven plugins: similar Maven Invoker Plugin, but JUnit friendly
- Host: GitHub
- URL: https://github.com/yegor256/farea
- Owner: yegor256
- License: mit
- Created: 2023-11-24T07:52:07.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-29T11:31:17.000Z (6 months ago)
- Last Synced: 2024-10-29T13:23:54.734Z (6 months ago)
- Topics: integration-testing, java, maven, maven-invoker, maven-invoker-plugin, maven-plugin, testing
- Language: Java
- Homepage:
- Size: 345 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Fake Maven Reactor, for Quick Unit Tests
[](https://www.elegantobjects.org)
[](http://www.rultor.com/p/yegor256/farea)
[](https://www.jetbrains.com/idea/)[](https://github.com/yegor256/farea/actions/workflows/mvn.yml)
[](http://www.0pdd.com/p?name=yegor256/farea)
[](https://maven-badges.herokuapp.com/maven-central/com.yegor256/farea)
[](http://www.javadoc.io/doc/com.yegor256/farea)
[](https://codecov.io/gh/yegor256/farea)
[](https://hitsofcode.com/view/github/yegor256/farea)
[](https://github.com/yegor256/farea/blob/master/LICENSE.txt)It's a fake Maven Reactor, helping you to integration-test
your custom Maven plugins.
There is a traditional way to do this:
[Maven Invoker Plugin][invoker].
It works perfectly, but it has two pretty annoying
drawbacks: 1) It doesn't run from IDE (at least from IntelliJ IDEA),
and 2) It always starts the entire build from scratch,
which makes 3) it pretty slow.Farea suggests an alternative way, which is way less flexible, but much
faster and JUnit-friendly.First, you add this to your `pom.xml`:
```xml
com.yegor256
farea
0.15.3```
Then, you use it like this, in your JUnit5 test
(obviously, you need to have `mvn` installed
and available on `$PATH`):```java
import com.yegor256.farea.Farea;
import com.yegor256.farea.RequisiteMatcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;class JavaCompilationTest {
@Test
void worksAsExpected(@Mktmp Path dir) {
new Farea(dir).together(f -> {
f.files()
.file("src/test/java/Hello.java")
.write("class Hello {}".getBytes());
f.dependencies().append("org.cactoos", "cactoos", "0.55.0");
f.exec("compile");
MatcherAssert.assertThat(
"Compiles without any issues",
f.files().log(),
RequisiteMatcher.SUCCESS
);
});
}
}
```This code will create a new `pom.xml` file in the temporary directory,
create a temporary `Hello.java` file, write simple content into it,
add a new `` to the `pom.xml`, and then run `mvn test` in this
temporary directory. The output of the build will be saved to `log.txt`,
which is available through the call to `.log()` method.You can also test the plugin that you are developing, inside the same reactor:
```java
class MyPluginTest {
@Test
void worksAsExpected(@Mktmp Path dir) {
new Farea(dir).together(f -> {
f.build()
.plugins()
.appendItself()
.execution()
.phase("test")
.goals("my-custom-goal")
.configuration()
.set("message", "Hello, world!");
f.exec("test");
assert f.files().log().contains("SUCCESS");
});
}
}
```Here, a `.jar` with the entire classpath will be packaged and saved
into the `~/.m2/repository/` directory. This is almost exactly what
the [`install`][install-mojo] goal of the
[invoker plugin][invoker] would do if you use it for
integration testing.It is recommended to add this to your `pom.xml`, in order
to enable interactive test runs right from the IDE:```xml
[...]
maven-resources-plugin
copy-descriptor
process-classes
copy-resources
${project.build.directory}
${project.build.outputDirectory}/META-INF/maven
plugin.xml
```
This will make sure the `META-INF/maven/plugin.xml` is not destroyed
in the `target/classes` by the IDE before the next test run.See how
[antlr2ebnf-maven-plugin](https://github.com/yegor256/antlr2ebnf-maven-plugin)
is using Farea.## How to Contribute
Fork repository, make changes, send us a
[pull request](https://www.yegor256.com/2014/04/15/github-guidelines.html).
We will review your changes and apply them to the `master` branch shortly,
provided they don't violate our quality standards. To avoid frustration,
before sending us your pull request please run full Maven build:```bash
mvn clean install -Pqulice
```You will need Maven 3.3+ and Java 11+.
[invoker]: https://maven.apache.org/plugins/maven-invoker-plugin/index.html
[install-mojo]: https://maven.apache.org/plugins/maven-invoker-plugin/install-mojo.html