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  import java.util.Properties;
22  
23  import org.apache.maven.api.di.Provides;
24  import org.apache.maven.api.plugin.testing.InjectMojo;
25  import org.apache.maven.api.plugin.testing.MojoTest;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.plugin.MojoExecution;
28  import org.apache.maven.project.MavenProject;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.api.extension.ExtendWith;
32  import org.mockito.Mock;
33  import org.mockito.junit.jupiter.MockitoExtension;
34  
35  import static org.junit.jupiter.api.Assertions.assertNotNull;
36  import static org.junit.jupiter.api.Assertions.assertSame;
37  import static org.mockito.Mockito.when;
38  
39  @ExtendWith(MockitoExtension.class)
40  @MojoTest
41  class ProvidesInjectOverrideMojoTest {
42  
43      private static final String POM = "<project></project>";
44  
45      @Mock
46      private MavenProject project;
47  
48      @Mock
49      private MavenSession session;
50  
51      @Mock
52      private MojoExecution mojoExecution;
53  
54      @BeforeEach
55      void setup() {
56          when(session.getUserProperties()).thenReturn(new Properties());
57          when(session.getSystemProperties()).thenReturn(new Properties());
58      }
59  
60      @Provides
61      public MavenProject mockMavenProject() {
62          return project;
63      }
64  
65      @Provides
66      public MavenSession mockMavenSession() {
67          return session;
68      }
69  
70      @Provides
71      MojoExecution mockMojoExecution() {
72          return mojoExecution;
73      }
74  
75      @Test
76      @InjectMojo(pom = POM, goal = "test:test-plugin:0.0.1-SNAPSHOT:provides")
77      public void bennShouldBeInjected(ProvidesInjectMojo mojo) {
78          assertNotNull(mojo);
79          // session provided by the @Provides method should be used
80          assertSame(session, mojo.getSession());
81          assertSame(session, mojo.getSessionFromBean());
82  
83          assertSame(project, mojo.getProject());
84          assertSame(project, mojo.getProjectFromBean());
85  
86          assertSame(mojoExecution, mojo.getMojoExecution());
87          assertSame(mojoExecution, mojo.getMojoExecutionFromBean());
88      }
89  }