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.api.plugin.testing;
20  
21  import java.lang.annotation.ElementType;
22  import java.lang.annotation.Retention;
23  import java.lang.annotation.RetentionPolicy;
24  import java.lang.annotation.Target;
25  
26  import org.junit.jupiter.api.extension.ExtendWith;
27  
28  /**
29   * Annotation that enables Maven plugin (Mojo) testing support in JUnit tests.
30   * When applied to a test class, it automatically sets up the testing environment
31   * for Maven plugins, including dependency injection and parameter resolution.
32   *
33   * <p>This annotation works in conjunction with {@link InjectMojo} and {@link MojoParameter}
34   * to provide a comprehensive testing framework for Maven plugins. It automatically registers
35   * the {@link MojoExtension} which handles the plugin lifecycle and dependency injection.</p>
36   *
37   * <p>Example usage:</p>
38   * <pre>
39   * {@code
40   * @MojoTest
41   * class MyMojoTest {
42   *     @Inject
43   *     private SomeComponent component;
44   *
45   *     @Test
46   *     @InjectMojo(goal = "my-goal")
47   *     @MojoParameter(name = "parameter", value = "value")
48   *     void testMojoExecution(MyMojo mojo) {
49   *         // mojo is instantiated with the specified parameters
50   *         // component is automatically injected
51   *         mojo.execute();
52   *         // verify execution results
53   *     }
54   *
55   *     @Provides
56   *     @Singleton
57   *     SomeComponent provideMockedComponent() {
58   *         return mock(SomeComponent.class);
59   *     }
60   * }
61   * }
62   * </pre>
63   *
64   * <p>The annotation supports:</p>
65   * <ul>
66   *   <li>Automatic Mojo instantiation and configuration</li>
67   *   <li>Parameter injection via {@link MojoParameter}</li>
68   *   <li>Component injection via {@link org.apache.maven.api.di.Inject}</li>
69   *   <li>Mock component injection via {@link org.apache.maven.api.di.Provides}</li>
70   *   <li>Custom POM configuration via {@link InjectMojo#pom()}</li>
71   *   <li>Base directory configuration for test resources</li>
72   * </ul>
73   *
74   * <p>This annotation replaces the legacy maven-plugin-testing-harness functionality
75   * with a modern, annotation-based approach that integrates with JUnit Jupiter and
76   * Maven's new dependency injection framework.</p>
77   *
78   * @see MojoExtension
79   * @see InjectMojo
80   * @see MojoParameter
81   * @see org.apache.maven.api.di.Inject
82   * @see org.apache.maven.api.di.Provides
83   * @since 4.0.0
84   */
85  @Retention(RetentionPolicy.RUNTIME)
86  @ExtendWith(MojoExtension.class)
87  @Target(ElementType.TYPE)
88  public @interface MojoTest {
89      /**
90       * If true, the test harness will provide a real Maven Session created by ApiRunner.createSession(),
91       * instead of the default mock session. Default is false.
92       */
93      boolean realSession() default false;
94  }