Skip to content

Mocked MavenProject used in InjectMojo is rather unsafe to use #245

@elmuerte

Description

@elmuerte

Affected version

3.4.0

Bug description

The mocked maven project created when using @InjectMojo is quite unsafe, only the properties are usable.

private MavenProject mockMavenProject() {
MavenProject mavenProject = Mockito.mock(MavenProject.class);
lenient().when(mavenProject.getProperties()).thenReturn(new Properties());
return mavenProject;
}

Everything else would return null. This is not documented and thus results in surprises during test executions like an NPE when in the mojo when you do project.getBuild().getDirectory() to get the target directory.

You can work around this like this:

    @Inject
    MavenProject project;

    @BeforeEach
    @Basedir("target/test-classes/projects/run")
    @InjectMojo(goal = "findings")
    void setUp(FindingsMojo findingsMojo) {
        when(project.getBuild()).thenReturn(mock());
        when(project.getBuild().getDirectory()).thenReturn("target/test-classes/projects/run/target");
    }

It would be nicer if parts of the project would have more safe to use defaults. But it should be at least documented that basic mocks are being inserted into the resolved mojos.

In the Javadoc for MojoExtension it also hints that certain properties are resolved

@MojoParameter(name = "outputDirectory", value = "${project.build.directory}/generated")

But that does not happen at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions