Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ private MavenProject mockMavenProject() {
mavenProject.setBuild(build);
mavenProject.addCompileSourceRoot(build.getSourceDirectory());
mavenProject.addTestCompileSourceRoot(build.getTestSourceDirectory());

try {
// there is no setter for basedir, so set it via reflection
setVariableValueToObject(
mavenProject, "basedir", Paths.get(getBasedir()).toFile());
} catch (IllegalAccessException e) {
// should not happen
throw new RuntimeException(e);
}

return mavenProject;
}

Expand Down Expand Up @@ -463,20 +473,13 @@ protected Mojo lookupMojo(
}

if (mockingDetails(mavenProject).isMock()) {
File pomFile = Optional.ofNullable(pomPath).map(Path::toFile).orElse(null);
if (mockingDetails(mavenProject).isSpy()) {
// there is no setter for basedir, so set it via reflection
setVariableValueToObject(
mavenProject, "basedir", Paths.get(getBasedir()).toFile());

// we only set the pom file
// setFile also change a basedir, so should not be used here
mavenProject.setPomFile(
Optional.ofNullable(pomPath).map(Path::toFile).orElse(null));
mavenProject.setPomFile(pomFile);
} else {
lenient()
.doReturn(new File(getTestBasedir(extensionContext)))
.when(mavenProject)
.getBasedir();
lenient().doReturn(pomFile).when(mavenProject).getFile();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@

import javax.inject.Inject;

import java.io.File;

import org.apache.maven.api.plugin.testing.InjectMojo;
import org.apache.maven.api.plugin.testing.MojoExtension;
import org.apache.maven.api.plugin.testing.MojoTest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
Expand All @@ -45,6 +50,11 @@ public class ProvidesInjectMojoTest {
@Inject
private MojoExecution mojoExecution;

@BeforeEach
void setup() {
assertEquals(new File(MojoExtension.getBasedir()), project.getBasedir());
}

@Test
@InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
public void bennShouldBeInjected(ProvidesInjectMojo mojo) {
Expand Down