Skip to content

Commit 377532b

Browse files
Allow to customize Plexus container by test class
1 parent 586251d commit 377532b

File tree

4 files changed

+115
-4
lines changed

4 files changed

+115
-4
lines changed

pom.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>plexus-testing</artifactId>
12-
<version>1.6.2-SNAPSHOT</version>
12+
<version>1.7.0-SNAPSHOT</version>
1313

1414
<name>Plexus Testing</name>
1515
<description>Library to help testing plexus components</description>
@@ -36,7 +36,7 @@
3636

3737
<properties>
3838
<versions.eclipse.sisu>0.9.0.M4</versions.eclipse.sisu>
39-
<project.build.outputTimestamp>2025-10-11T14:58:36Z</project.build.outputTimestamp>
39+
<project.build.outputTimestamp>2025-10-13T21:13:09Z</project.build.outputTimestamp>
4040
</properties>
4141

4242
<dependencies>
@@ -72,6 +72,19 @@
7272
<groupId>org.junit.jupiter</groupId>
7373
<artifactId>junit-jupiter-api</artifactId>
7474
</dependency>
75+
76+
<dependency>
77+
<groupId>org.mockito</groupId>
78+
<artifactId>mockito-core</artifactId>
79+
<version>4.11.0</version>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.mockito</groupId>
84+
<artifactId>mockito-junit-jupiter</artifactId>
85+
<version>4.11.0</version>
86+
<scope>test</scope>
87+
</dependency>
7588
</dependencies>
7689

7790
<build>

src/main/java/org/codehaus/plexus/testing/PlexusExtension.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* This is a slightly modified version of the original plexus class
5959
* available at https://raw.githubusercontent.com/codehaus-plexus/plexus-containers/master/plexus-container-default/
6060
* src/main/java/org/codehaus/plexus/PlexusTestCase.java
61-
* in order to migrate the tests to JUnit 4.
61+
* in order to migrate the tests to JUnit 5.
6262
*
6363
* @author Jason van Zyl
6464
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
@@ -94,7 +94,6 @@ protected void setContext(ExtensionContext context) {
9494
this.context = context;
9595
}
9696

97-
@SuppressWarnings("ResultOfMethodCallIgnored")
9897
protected void setupContainer() {
9998
// ----------------------------------------------------------------------------
10099
// Context Setup
@@ -136,12 +135,14 @@ protected void setupContainer() {
136135
}
137136

138137
customizeContainerConfiguration(containerConfiguration);
138+
testInstanceCustomizeContainerConfiguration(containerConfiguration);
139139

140140
try {
141141
container = new DefaultPlexusContainer(containerConfiguration);
142142
} catch (PlexusContainerException e) {
143143
throw new IllegalArgumentException("Failed to create plexus container.", e);
144144
}
145+
testInstanceCustomizeContainer(container);
145146
}
146147

147148
/**
@@ -155,6 +156,20 @@ protected void customizeContainerConfiguration(ContainerConfiguration containerC
155156
containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
156157
}
157158

159+
private void testInstanceCustomizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
160+
Object testInstance = context.getRequiredTestInstance();
161+
if (testInstance instanceof PlexusTestConfiguration) {
162+
((PlexusTestConfiguration) testInstance).customizeConfiguration(containerConfiguration);
163+
}
164+
}
165+
166+
private void testInstanceCustomizeContainer(PlexusContainer container) {
167+
Object testInstance = context.getRequiredTestInstance();
168+
if (testInstance instanceof PlexusTestConfiguration) {
169+
((PlexusTestConfiguration) testInstance).customizeContainer(container);
170+
}
171+
}
172+
158173
protected void customizeContext(Context context) {}
159174

160175
protected PlexusConfiguration customizeComponentConfiguration() {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.codehaus.plexus.testing;
2+
3+
import org.codehaus.plexus.ContainerConfiguration;
4+
import org.codehaus.plexus.PlexusContainer;
5+
6+
/**
7+
* Allow to customize the Plexus container by test class.
8+
*
9+
* @since 1.7.0
10+
*/
11+
public interface PlexusTestConfiguration {
12+
13+
/**
14+
* Customize the container configuration before the container is created.
15+
*
16+
* @param containerConfiguration the container configuration to customize
17+
* @since 1.7.0
18+
*/
19+
default void customizeConfiguration(ContainerConfiguration containerConfiguration) {}
20+
21+
/**
22+
* Customize the container after it has been created.
23+
*
24+
* @param container the container to customize
25+
* @since 1.7.0
26+
*/
27+
default void customizeContainer(PlexusContainer container) {}
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.codehaus.plexus.testing;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
// START SNIPPET: test-customize-class
22+
import javax.inject.Inject;
23+
24+
import org.codehaus.plexus.PlexusContainer;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.extension.ExtendWith;
27+
import org.mockito.Mock;
28+
import org.mockito.junit.jupiter.MockitoExtension;
29+
30+
import static org.junit.jupiter.api.Assertions.assertNotNull;
31+
import static org.junit.jupiter.api.Assertions.assertSame;
32+
33+
// MockitoExtension must be first
34+
@ExtendWith(MockitoExtension.class)
35+
@PlexusTest
36+
class PlexusTestCustomizeTest implements PlexusTestConfiguration {
37+
38+
@Mock
39+
private TestJavaxComponent2 mockComponent2;
40+
41+
@Override
42+
public void customizeContainer(PlexusContainer container) {
43+
container.addComponent(mockComponent2, TestJavaxComponent2.class.getName());
44+
}
45+
46+
@Inject
47+
private TestJavaxComponent testJavaxComponent;
48+
49+
@Test
50+
void dependencyShouldBeInjected() {
51+
assertNotNull(testJavaxComponent);
52+
assertSame(testJavaxComponent.getTestComponent2(), mockComponent2);
53+
}
54+
}
55+
// END SNIPPET: test-customize-class

0 commit comments

Comments
 (0)