View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.testing;
20  
21  // START SNIPPET: resolve-mojo-test
22  import javax.inject.Inject;
23  
24  import java.io.File;
25  
26  import org.apache.maven.api.plugin.testing.InjectMojo;
27  import org.apache.maven.api.plugin.testing.MojoParameter;
28  import org.apache.maven.api.plugin.testing.MojoTest;
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.plugin.logging.Log;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Nested;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.io.TempDir;
35  import org.mockito.Mockito;
36  
37  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
38  
39  /**
40   * Test class for simple artifact resolution mojo with repository system injection.
41   */
42  @MojoTest(realRepositorySession = true)
43  class SimpleResolveMojoTest {
44  
45      @TempDir
46      private File tempDir;
47  
48      // inject the Maven session to customize it for tests
49      @Inject
50      private MavenSession session;
51  
52      @Inject
53      private Log log;
54  
55      @BeforeEach
56      void beforeEach() {
57          // optionally customize the session to use a temporary local repository
58          // default would be basedir/target/local-repo
59          session.getRequest().setLocalRepositoryPath(tempDir);
60  
61          // other customizations can be done here, if needed
62          // session.getRequest().setRemoteRepositories(customRemoteRepositories());
63          // session.getRequest().setOffline(true);
64          // session.getUserProperties().setProperty("maven.repo.local.tail", "your local repository");
65      }
66  
67      @Test
68      @InjectMojo(goal = "simple-resolve")
69      @MojoParameter(name = "artifact", value = "org.apache.commons:commons-lang3:3.20.0")
70      void artifactShouldBeResolved(SimpleResolveMojo mojo) {
71          assertDoesNotThrow(mojo::execute);
72  
73          Mockito.verify(log, Mockito.times(1)).info(Mockito.contains("Resolved artifact to"));
74      }
75  
76      @Nested
77      class NestedTest {
78          @Test
79          @InjectMojo(goal = "simple-resolve")
80          @MojoParameter(name = "artifact", value = "org.apache.commons:commons-lang3:3.20.0")
81          void artifactShouldBeResolved(SimpleResolveMojo mojo) {
82              assertDoesNotThrow(mojo::execute);
83  
84              Mockito.verify(log, Mockito.times(1)).info(Mockito.contains("Resolved artifact to"));
85          }
86      }
87  }
88  // END SNIPPET: resolve-mojo-test