Skip to content

ENH: Enhance @InjectTest with support for @Setup methods & when @Inject, @Mock provide values use them #260

@rbygrave

Description

@rbygrave

Add support for @Setup methods like:

  @Inject
  HelloService helloService;

  HelloData myTestDouble;

  @Setup
  void setup(BeanScopeBuilder builder) {
    myTestDouble = mock(HelloData.class);
    when(myTestDouble.helloData()).thenReturn("MockedViaSetupMethod");
    builder.bean(HelloData.class, myTestDouble);
  }

  @Test
  void hello_1() {
    assertEquals("hello+MockedViaSetupMethod", helloService.hello());
  }

Change such that when @Inject field has a value then it's expected to be a provided test double and the instance value is registered and used for wiring.

  @Inject HelloService helloService;    // Get this OUT of the DI BeanScope

  @Inject HelloData myTestDouble = () -> "ImActuallyATestDouble";    // Put this INTO the DI BeanScope

  @Test
  void hello_1() {
    assertEquals("hello+ImActuallyATestDouble", helloService.hello());
    assertEquals("ImActuallyATestDouble", myTestDouble.helloData());
  }

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions