Class GuiceExtension

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.InvocationInterceptor, org.junit.jupiter.api.extension.TestInstanceFactory

    public class GuiceExtension
    extends Object
    implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.TestInstanceFactory, org.junit.jupiter.api.extension.InvocationInterceptor
    JUnit5 test extension that creates injector using the modules provided with GuiceModules configuration.

    Here is a simple test:

    
     @ExtendWith(GuiceExtension.class)
     @GuiceModules({MyModule.class})
     public class MyTest {
    
      @Inject
      Foo foo;
    
      public void testFooInjected() {
          assertNotNull(foo);
      }
     }
    
     
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.InvocationInterceptor

        org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<T extends Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      GuiceExtension()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void afterAll​(org.junit.jupiter.api.extension.ExtensionContext context)  
      void beforeAll​(org.junit.jupiter.api.extension.ExtensionContext context)  
      Object createTestInstance​(org.junit.jupiter.api.extension.TestInstanceFactoryContext factoryContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)  
      protected List<Module> getModules​(Class<?> klass)  
      void interceptTestMethod​(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<Void> invocation, org.junit.jupiter.api.extension.ReflectiveInvocationContext<Method> invocationContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)  
      • Methods inherited from interface org.junit.jupiter.api.extension.InvocationInterceptor

        interceptAfterAllMethod, interceptAfterEachMethod, interceptBeforeAllMethod, interceptBeforeEachMethod, interceptDynamicTest, interceptDynamicTest, interceptTestClassConstructor, interceptTestFactoryMethod, interceptTestTemplateMethod
    • Constructor Detail

      • GuiceExtension

        public GuiceExtension()
    • Method Detail

      • beforeAll

        public void beforeAll​(org.junit.jupiter.api.extension.ExtensionContext context)
                       throws Exception
        Specified by:
        beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
        Throws:
        Exception
      • afterAll

        public void afterAll​(org.junit.jupiter.api.extension.ExtensionContext context)
                      throws Exception
        Specified by:
        afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
        Throws:
        Exception
      • getModules

        protected List<Module> getModules​(Class<?> klass)
                                   throws org.junit.jupiter.api.extension.TestInstantiationException
        Throws:
        org.junit.jupiter.api.extension.TestInstantiationException
      • createTestInstance

        public Object createTestInstance​(org.junit.jupiter.api.extension.TestInstanceFactoryContext factoryContext,
                                         org.junit.jupiter.api.extension.ExtensionContext extensionContext)
                                  throws org.junit.jupiter.api.extension.TestInstantiationException
        Specified by:
        createTestInstance in interface org.junit.jupiter.api.extension.TestInstanceFactory
        Throws:
        org.junit.jupiter.api.extension.TestInstantiationException
      • interceptTestMethod

        public void interceptTestMethod​(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<Void> invocation,
                                        org.junit.jupiter.api.extension.ReflectiveInvocationContext<Method> invocationContext,
                                        org.junit.jupiter.api.extension.ExtensionContext extensionContext)
                                 throws Throwable
        Specified by:
        interceptTestMethod in interface org.junit.jupiter.api.extension.InvocationInterceptor
        Throws:
        Throwable