Interface BeanScopeBuilder.ForTesting

    • Method Detail

      • mock

        BeanScopeBuilder.ForTesting mock​(Class<?> type)
        Use a mockito mock when injecting this bean type.
        
        
           try (BeanScope scope = BeanScope.builder()
             .forTesting()
             .mock(Pump.class)
             .mock(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();
           }
        
         
      • mock

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

        <D> BeanScopeBuilder.ForTesting mock​(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.builder()
             .forTesting()
             .mock(Pump.class)
             .mock(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();
           }
        
         
      • spy

        BeanScopeBuilder.ForTesting spy​(Class<?> type)
        Use a mockito spy when injecting this bean type.
        
        
           try (BeanScope scope = BeanScope.builder()
             .forTesting()
             .spy(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();
           }
        
         
      • spy

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

        <D> BeanScopeBuilder.ForTesting spy​(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.builder()
             .forTesting()
             .spy(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();
           }