Skip to content

Commit 446afa7

Browse files
authored
Injection of web target with correct endpoint. (helidon-io#2380)
* Injection of web target with correct endpoint. * Support for junit5 tests already in MP Config implementation * Support for inherited repeating annotations. Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
1 parent 62279c7 commit 446afa7

37 files changed

Lines changed: 600 additions & 347 deletions

File tree

docs/mp/testing/01_testing.adoc

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ A test can be annotated with `io.helidon.microprofile.tests.junit5.HelidonTest`
4141
CDI test. This annotation will start the CDI container before any test method is invoked, and stop it after
4242
the last method is invoked. This annotation also enables injection into the test class itself.
4343
44+
The annotations described in this section are inherited (for the non-repeatable ones), and additive (for repeatable).
45+
So if you declare `@DisableDiscovery` on abstract class, all implementations will have discovery disabled, unless you
46+
annotate the implementation class with `@DisableDiscovery(false)`.
47+
If you declare `@AddBean` on both abstract class and implementation class, both beans will be added.
48+
4449
In addition to this simplification, the following annotations are supported:
4550
4651
- `io.helidon.microprofile.tests.junit5.AddBean` - to add one or more beans to the container
@@ -49,6 +54,7 @@ In addition to this simplification, the following annotations are supported:
4954
(if not added through service loader, or when discovery is disabled)
5055
- `io.helidon.microprofile.tests.junit5.AddConfig` - to add one or more configuration properties to MicroProfile config
5156
without the need of creating a `microprofile-config.properties` file
57+
- `io.helidon.microprofile.tests.junit5.DisableDiscovery` - to disable automated discovery of beans and extensions
5258
5359
[source,java]
5460
.Code sample
@@ -84,10 +90,23 @@ This will change the behavior as follows:
8490
8591
== Usage - configuration
8692
In addition to the `@AddConfig` annotation, you can also use
87-
`@Configuration`.
93+
`@Configuration` to configure additional classpath properties config sources using `configSources`, and to
94+
mark that a custom configuration is desired.
95+
You can set up config in `@BeforeAll` method and register it with `ConfigProviderResolver` using MP Config APIs, and declare
96+
`@Configuration(useExisting=true)`.
97+
Note that this is not compatible with repeatable tests that use method sources that access CDI, as we must delay the CDI
98+
startup to the test class instantiation (which is too late, as the method sources are already invoked by this time).
99+
100+
*If you want to use method sources that use CDI with repeatable tests, please do not use `@Configuration(useExisting=true)`*
101+
102+
== Usage - added parameters and injection types
103+
The following types are available for injection (when a single CDI container is used per test class):
104+
105+
- `WebTarget` - a JAX-RS client's target configured for the current hostname and port when `helidon-micorprofile-server` is on
106+
the classpath
88107
89-
This allows you to do the following:
108+
The following types are available as method parameters (in any type of Helidon tests):
90109
91-
- when `useExisting` is set to `true`, the configuration will not be changed
92-
and current MP configuration will be used
93-
- you can configure additional classpath properties config sources using `configSources`
110+
- `WebTarget` - a JAX-RS client's target configured for the current hostname and port when `helidon-micorprofile-server` is on
111+
the classpath
112+
- `SeContainer` - the current container instance

fault-tolerance/src/test/java/io/helidon/faulttolerance/RetryTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@
3636
import static org.hamcrest.CoreMatchers.instanceOf;
3737
import static org.hamcrest.CoreMatchers.is;
3838
import static org.hamcrest.MatcherAssert.assertThat;
39-
import static org.hamcrest.Matchers.isOneOf;
4039
import static org.hamcrest.Matchers.contains;
41-
import static org.junit.jupiter.api.Assertions.assertThrows;
40+
import static org.hamcrest.Matchers.isOneOf;
4241

4342
class RetryTest {
4443
@Test

microprofile/config/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,10 @@
8686
<version>${project.version}</version>
8787
<scope>test</scope>
8888
</dependency>
89+
<dependency>
90+
<groupId>io.helidon.microprofile.tests</groupId>
91+
<artifactId>helidon-microprofile-tests-junit5</artifactId>
92+
<scope>test</scope>
93+
</dependency>
8994
</dependencies>
9095
</project>
Lines changed: 22 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,30 +18,23 @@
1818

1919
import java.lang.annotation.Retention;
2020
import java.lang.annotation.Target;
21-
import java.util.Map;
2221

2322
import javax.enterprise.context.Dependent;
24-
import javax.enterprise.inject.se.SeContainer;
25-
import javax.enterprise.inject.se.SeContainerInitializer;
26-
import javax.enterprise.inject.spi.CDI;
27-
import javax.enterprise.util.AnnotationLiteral;
2823
import javax.inject.Inject;
2924
import javax.inject.Qualifier;
3025

31-
import io.helidon.config.test.infra.RestoreSystemPropertiesExt;
3226
import io.helidon.microprofile.config.Converters.Ctor;
3327
import io.helidon.microprofile.config.Converters.Of;
3428
import io.helidon.microprofile.config.Converters.Parse;
3529
import io.helidon.microprofile.config.Converters.ValueOf;
30+
import io.helidon.microprofile.tests.junit5.AddBean;
31+
import io.helidon.microprofile.tests.junit5.AddConfig;
32+
import io.helidon.microprofile.tests.junit5.HelidonTest;
3633

3734
import org.eclipse.microprofile.config.inject.ConfigProperty;
38-
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
39-
import org.eclipse.microprofile.config.spi.ConfigSource;
40-
import org.junit.jupiter.api.AfterAll;
41-
import org.junit.jupiter.api.BeforeAll;
4235
import org.junit.jupiter.api.Test;
43-
import org.junit.jupiter.api.extension.ExtendWith;
4436

37+
import static java.lang.annotation.ElementType.FIELD;
4538
import static java.lang.annotation.ElementType.TYPE;
4639
import static java.lang.annotation.RetentionPolicy.RUNTIME;
4740
import static org.hamcrest.MatcherAssert.assertThat;
@@ -51,41 +44,23 @@
5144
/**
5245
* Unit test for MP config injection.
5346
*/
54-
@ExtendWith(RestoreSystemPropertiesExt.class)
47+
@HelidonTest
48+
@AddConfig(key = "inject.of", value = "of")
49+
@AddConfig(key = "inject.valueOf", value = "valueOf")
50+
@AddConfig(key = "inject.parse", value = "parse")
51+
@AddConfig(key = "inject.ctor", value = "ctor")
52+
@AddBean(value = MpConfigInjectionTest.Bean.class, scope = Dependent.class)
53+
@AddBean(value = MpConfigInjectionTest.SubBean.class, scope = Dependent.class)
5554
class MpConfigInjectionTest {
56-
private static SeContainer container;
55+
@Inject
56+
private Bean bean;
5757

58-
@BeforeAll
59-
static void initClass() {
60-
61-
// Removed use of system properties, as those stay around after test is finished
62-
ConfigProviderResolver configProvider = ConfigProviderResolver.instance();
63-
64-
configProvider.registerConfig(configProvider.getBuilder()
65-
.addDefaultSources()
66-
.withSources(new TestSource())
67-
.build(),
68-
Thread.currentThread().getContextClassLoader());
69-
70-
71-
// CDI container
72-
container = SeContainerInitializer.newInstance()
73-
.addBeanClasses(Bean.class, SubBean.class)
74-
.initialize();
75-
}
76-
77-
@AfterAll
78-
static void destroyClass() {
79-
if (null != container) {
80-
container.close();
81-
}
82-
}
58+
@Inject
59+
@Specific
60+
private SubBean subBean;
8361

8462
@Test
8563
public void testImplicitConversion() {
86-
87-
Bean bean = CDI.current().select(Bean.class).get();
88-
8964
assertAll("Implicit conversion injection",
9065
() -> assertThat("of", bean.of, is(Of.of("of"))),
9166
() -> assertThat("valueOf", bean.valueOf, is(ValueOf.valueOf("valueOf"))),
@@ -96,22 +71,15 @@ public void testImplicitConversion() {
9671

9772
@Test
9873
public void testImplicitConversionSubclass() {
99-
100-
Bean bean = CDI.current().select(SubBean.class,
101-
new AnnotationLiteral<Specific>() {
102-
}).get();
103-
10474
assertAll("Implicit conversion injection",
105-
() -> assertThat("of", bean.of, is(Of.of("of"))),
106-
() -> assertThat("valueOf", bean.valueOf, is(ValueOf.valueOf("valueOf"))),
107-
() -> assertThat("parse", bean.parse, is(Parse.parse("parse"))),
108-
() -> assertThat("ctor", bean.ctor, is(new Ctor("ctor")))
75+
() -> assertThat("of", subBean.of, is(Of.of("of"))),
76+
() -> assertThat("valueOf", subBean.valueOf, is(ValueOf.valueOf("valueOf"))),
77+
() -> assertThat("parse", subBean.parse, is(Parse.parse("parse"))),
78+
() -> assertThat("ctor", subBean.ctor, is(new Ctor("ctor")))
10979
);
11080
}
11181

112-
@Dependent
11382
public static class Bean {
114-
11583
@Inject
11684
@ConfigProperty(name = "inject.of")
11785
public Of of;
@@ -131,36 +99,11 @@ public static class Bean {
13199

132100
@Qualifier
133101
@Retention(RUNTIME)
134-
@Target(TYPE)
102+
@Target({TYPE, FIELD})
135103
public @interface Specific {
136104
}
137105

138-
@Dependent
139106
@Specific
140107
public static class SubBean extends Bean {
141108
}
142-
143-
private static class TestSource implements ConfigSource {
144-
private final Map<String, String> properties = Map.of(
145-
"inject.of", "of",
146-
"inject.valueOf", "valueOf",
147-
"inject.parse", "parse",
148-
"inject.ctor", "ctor"
149-
);
150-
151-
@Override
152-
public Map<String, String> getProperties() {
153-
return properties;
154-
}
155-
156-
@Override
157-
public String getValue(String propertyName) {
158-
return properties.get(propertyName);
159-
}
160-
161-
@Override
162-
public String getName() {
163-
return getClass().getName();
164-
}
165-
}
166109
}

microprofile/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,25 @@
2222

2323
import javax.enterprise.context.Dependent;
2424
import javax.enterprise.inject.se.SeContainer;
25-
import javax.enterprise.inject.se.SeContainerInitializer;
26-
import javax.enterprise.inject.spi.CDI;
2725
import javax.inject.Inject;
2826

27+
import io.helidon.microprofile.tests.junit5.AddBean;
28+
import io.helidon.microprofile.tests.junit5.Configuration;
29+
import io.helidon.microprofile.tests.junit5.HelidonTest;
30+
2931
import org.eclipse.microprofile.config.inject.ConfigProperty;
3032
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
3133
import org.eclipse.microprofile.config.spi.ConfigSource;
32-
import org.junit.jupiter.api.AfterAll;
3334
import org.junit.jupiter.api.BeforeAll;
3435
import org.junit.jupiter.api.Test;
3536

3637
import static org.hamcrest.CoreMatchers.is;
3738
import static org.hamcrest.MatcherAssert.assertThat;
3839

39-
public class MutableMpTest {
40-
private static SeContainer container;
40+
@HelidonTest
41+
@Configuration(useExisting = true)
42+
@AddBean(value = MutableMpTest.Bean.class, scope = Dependent.class)
43+
class MutableMpTest {
4144
private static MutableSource source;
4245

4346
@BeforeAll
@@ -50,35 +53,21 @@ static void initClass() {
5053
.withSources(source)
5154
.build(),
5255
Thread.currentThread().getContextClassLoader());
53-
54-
// CDI container
55-
container = SeContainerInitializer.newInstance()
56-
.addBeanClasses(Bean.class)
57-
.initialize();
58-
}
59-
60-
@AfterAll
61-
static void destroyClass() {
62-
if (null != container) {
63-
container.close();
64-
}
65-
source = null;
6656
}
6757

6858
@Test
69-
public void testMutable() {
70-
Bean bean = CDI.current().select(Bean.class).get();
59+
public void testMutable(SeContainer cdi) {
60+
Bean bean = cdi.select(Bean.class).get();
7161

7262
assertThat(bean.value, is("initial"));
7363

7464
source.setValue("updated");
7565

76-
bean = CDI.current().select(Bean.class).get();
66+
bean = cdi.select(Bean.class).get();
7767

7868
assertThat(bean.value, is("updated"));
7969
}
8070

81-
@Dependent
8271
public static class Bean {
8372
@Inject
8473
@ConfigProperty(name = "value")

microprofile/cors/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,10 @@
7171
<artifactId>helidon-microprofile-server</artifactId>
7272
<scope>test</scope>
7373
</dependency>
74+
<dependency>
75+
<groupId>io.helidon.microprofile.tests</groupId>
76+
<artifactId>helidon-microprofile-tests-junit5</artifactId>
77+
<scope>test</scope>
78+
</dependency>
7479
</dependencies>
7580
</project>

0 commit comments

Comments
 (0)