Interface BeanScopeBuilder.ForTesting

All Superinterfaces:
BeanScopeBuilder
Enclosing interface:
BeanScopeBuilder

public static interface BeanScopeBuilder.ForTesting extends BeanScopeBuilder
Extends the building with testing specific support for mocks and spies.
  • Method Details

    • mock

      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

      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

      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

      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();
        }