Interface BeanScopeBuilder.ForTesting

    • Method Detail

      • withMock

        BeanScopeBuilder.ForTesting withMock​(Class<?> type)
        Use a mockito mock when injecting this bean type.
        
        
           try (BeanScope scope = BeanScope.newBuilder()
             .forTesting()
             .withMock(Pump.class)
             .withMock(Grinder.class)
             .build()) {
        
        
             CoffeeMaker coffeeMaker = scope.get(CoffeeMaker.class);
             coffeeMaker.makeIt();
        
             // this is a mockito mock
             Grinder grinder = scope.get(Grinder.class);
             verify(grinder).grindBeans();
           }
        
         
      • withMock

        BeanScopeBuilder.ForTesting withMock​(Class<?> type,
                                             String name)
        Register as a Mockito mock with a qualifier name.
        
        
           try (BeanScope scope = BeanScope.newBuilder()
             .forTesting()
             .withMock(Store.class, "red")
             .withMock(Store.class, "blue")
             .build()) {
        
             ...
           }
        
         
      • withMock

        <D> BeanScopeBuilder.ForTesting withMock​(Class<D> type,
                                                 Consumer<D> consumer)
        Use a mockito mock when injecting this bean type additionally running setup on the mock instance.
        
        
           try (BeanScope scope = BeanScope.newBuilder()
             .forTesting()
             .withMock(Pump.class)
             .withMock(Grinder.class, grinder -> {
        
               // setup the mock
               when(grinder.grindBeans()).thenReturn("stub response");
             })
             .build()) {
        
        
             CoffeeMaker coffeeMaker = scope.get(CoffeeMaker.class);
             coffeeMaker.makeIt();
        
             // this is a mockito mock
             Grinder grinder = scope.get(Grinder.class);
             verify(grinder).grindBeans();
           }
        
         
      • withSpy

        BeanScopeBuilder.ForTesting withSpy​(Class<?> type)
        Use a mockito spy when injecting this bean type.
        
        
           try (BeanScope scope = BeanScope.newBuilder()
             .forTesting()
             .withSpy(Pump.class)
             .build()) {
        
             // setup spy here ...
             Pump pump = scope.get(Pump.class);
             doNothing().when(pump).pumpSteam();
        
             // act
             CoffeeMaker coffeeMaker = scope.get(CoffeeMaker.class);
             coffeeMaker.makeIt();
        
             verify(pump).pumpWater();
             verify(pump).pumpSteam();
           }
        
         
      • withSpy

        BeanScopeBuilder.ForTesting withSpy​(Class<?> type,
                                            String name)
        Register a Mockito spy with a qualifier name.
        
        
           try (BeanScope scope = BeanScope.newBuilder()
             .forTesting()
             .withSpy(Store.class, "red")
             .withSpy(Store.class, "blue")
             .build()) {
        
             ...
           }
        
         
      • withSpy

        <D> BeanScopeBuilder.ForTesting withSpy​(Class<D> type,
                                                Consumer<D> consumer)
        Use a mockito spy when injecting this bean type additionally running setup on the spy instance.
        
        
           try (BeanScope scope = BeanScope.newBuilder()
             .forTesting()
             .withSpy(Pump.class, pump -> {
               // setup the spy
               doNothing().when(pump).pumpWater();
             })
             .build()) {
        
             // or setup here ...
             Pump pump = scope.get(Pump.class);
             doNothing().when(pump).pumpSteam();
        
             // act
             CoffeeMaker coffeeMaker = scope.get(CoffeeMaker.class);
             coffeeMaker.makeIt();
        
             verify(pump).pumpWater();
             verify(pump).pumpSteam();
           }