2

When using MojoRule, the localRepository in the session is null even though I'm passing it in in my test pom

Test Pom

    <build>
    <plugins>
        <plugin>
            <groupId>myplugin</groupId>
            <artifactId>isolatedFeatureBranch</artifactId>
            <goals>
                <goal>SetRepositoryProperties</goal>
            </goals>
            <configuration>
                <localRepository>${localRepository}</localRepository>
                <branchName>feature/defaultInPomFile</branchName>
            </configuration>
        </plugin>

Mojo Class

/**
 * Maven Project Access
 */
@Component
protected MavenProject project;

/**
 * Local Repository.
 */
@Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
private ArtifactRepository localRepository;

Test Code

  @Rule public TestName name = new TestName();
  @Rule public MojoRule mojoRule = new MojoRule();
  @Rule public TestResources testResources = new TestResources(testProjects.getAbsolutePath(), workDir.getAbsolutePath());

  private MavenProject project;
  private MavenSession session;
  private SetRepositoryPropertiesMojo mojo;
  @Before
  public void setUp() throws Exception {
    // setup with pom set BRANCHNAME  set in pom
    File pomDir = testResources.getBasedir("SetPropertiesTestsDefaultInPom");
    project = mojoRule.readMavenProject(pomDir);

    session = mojoRule.newMavenSession(project);

    // Generate Execution and Mojo for testing
    MojoExecution execution = mojoRule.newMojoExecution("SetRepositoryProperties");
    mojo = (SetRepositoryPropertiesMojo) mojoRule.lookupConfiguredMojo(session, execution);

  }

Error

java.lang.NullPointerException
at SetPropertiesFeatureBranchTests.setRemoteRepositoryHappyPath(SetPropertiesFeatureBranchTests.java:98)
Peter Kahn
  • 12,364
  • 20
  • 77
  • 135

1 Answers1

3

We need to create a localRepository and then use the request to attach it to the session. After which, we create the mojo and the class's localRepository variable contains the repo

Test Code

  public void setUp() throws Exception {
    // setup with pom set BRANCHNAME  set in pom
    File pomDir = testResources.getBasedir("SetPropertiesTestsDefaultInPom");
    project = mojoRule.readMavenProject(pomDir);

    // Generate session
    session = mojoRule.newMavenSession(project);

    // add localRepo - framework doesn't do this on its own
    ArtifactRepository localRepo = createLocalArtifactRepository();
    session.getRequest().setLocalRepository(localRepo);

    // Generate Execution and Mojo for testing
    MojoExecution execution = mojoRule.newMojoExecution("SetRepositoryProperties");
    mojo = (SetRepositoryPropertiesMojo) mojoRule.lookupConfiguredMojo(session, execution);

  }

 /**
   * Generate a local repository
   * @return local repository object
   */
  private ArtifactRepository createLocalArtifactRepository() {
    return new MavenArtifactRepository("local",
        localRepoDir.toURI().toString(),
        new DefaultRepositoryLayout(),
        new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE ),
        new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE )

    );
  }
Peter Kahn
  • 12,364
  • 20
  • 77
  • 135