-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QuarkusComponentTest: convenient handling of nested classes
- add static nested classes declared on test class to the set of components under test by default - exclude static nested classes declated on a QuarkusComponentTest from discovery during `@QuarkusTest`
- Loading branch information
Showing
9 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
integration-tests/main/src/test/java/io/quarkus/it/main/MyComponentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.quarkus.it.main; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.enterprise.inject.Produces; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.component.QuarkusComponentTest; | ||
|
||
@QuarkusComponentTest | ||
public class MyComponentTest { | ||
|
||
@Inject | ||
@ConfigProperty // name and default value are nonbinding | ||
String myProperty; | ||
|
||
@Test | ||
public void testProperty() { | ||
assertEquals("foo", myProperty); | ||
} | ||
|
||
@Singleton | ||
static class PropertyProducer { | ||
|
||
// This producer would normally break all @QuarkusTest in the test suite | ||
// However, since it's a static nested class declared on a @QuarkusComponentTest it's excluded from the bean discovery | ||
@Produces | ||
@ConfigProperty | ||
String myString() { | ||
return "foo"; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...omponent/src/test/java/io/quarkus/test/component/declarative/IgnoreNestedClassesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.quarkus.test.component.declarative; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.Unremovable; | ||
import io.quarkus.test.component.QuarkusComponentTest; | ||
import io.quarkus.test.component.declarative.IgnoreNestedClassesTest.Alpha; | ||
|
||
@QuarkusComponentTest(value = Alpha.class, addNestedClassesAsComponents = false) | ||
public class IgnoreNestedClassesTest { | ||
|
||
@Test | ||
public void testComponents() { | ||
assertTrue(Arc.container().instance(Alpha.class).isAvailable()); | ||
assertFalse(Arc.container().instance(Bravo.class).isAvailable()); | ||
} | ||
|
||
@Unremovable | ||
@Singleton | ||
static class Alpha { | ||
|
||
} | ||
|
||
@Unremovable | ||
@Singleton | ||
static class Bravo { | ||
|
||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...component/src/test/java/io/quarkus/test/component/declarative/InterceptorMockingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.quarkus.test.component.declarative; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.component.QuarkusComponentTest; | ||
import io.quarkus.test.component.beans.Charlie; | ||
|
||
@QuarkusComponentTest | ||
public class InterceptorMockingTest { | ||
|
||
@Inject | ||
TheComponent theComponent; | ||
|
||
@InjectMock | ||
Charlie charlie; | ||
|
||
@Test | ||
public void testPing() { | ||
Mockito.when(charlie.ping()).thenReturn("ok"); | ||
assertEquals("ok", theComponent.ping()); | ||
} | ||
|
||
@Singleton | ||
static class TheComponent { | ||
|
||
@SimpleBinding | ||
String ping() { | ||
return "true"; | ||
} | ||
|
||
} | ||
|
||
@Target({ TYPE, METHOD }) | ||
@Retention(RUNTIME) | ||
@InterceptorBinding | ||
public @interface SimpleBinding { | ||
|
||
} | ||
|
||
// This interceptor is automatically added as a tested component | ||
@Priority(1) | ||
@SimpleBinding | ||
@Interceptor | ||
static class SimpleInterceptor { | ||
|
||
@Inject | ||
Charlie charlie; | ||
|
||
@AroundInvoke | ||
Object aroundInvoke(InvocationContext context) throws Exception { | ||
return Boolean.parseBoolean(context.proceed().toString()) ? charlie.ping() : "false"; | ||
} | ||
|
||
} | ||
|
||
} |