Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JUnit 5 @ExtendWith(InjectExtension.class) #115

Closed
rbygrave opened this issue May 26, 2021 · 0 comments
Closed

Add JUnit 5 @ExtendWith(InjectExtension.class) #115

rbygrave opened this issue May 26, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@rbygrave
Copy link
Contributor

Means we can use the mockito annotations @Mock, @Spy, @Captor along with @Inject for "component testing".

For example:

@ExtendWith(InjectExtension.class)   // Use InjectExtension (JUnit 5) !!!
class WithExtnNamedMocksTest {

  @Mock @Blue SomeStore blueStore;

  @Mock @Named("green") SomeStore greenStore;

  @Inject StoreManagerWithSetterQualifier storeManager;

  @Test
  void when_plainCaptor() {
    when(blueStore.store()).thenReturn("BlueStoreStub");
    when(greenStore.store()).thenReturn("GreenStoreStub");

    assertThat(storeManager.blueStore()).isEqualTo("BlueStoreStub");
    assertThat(storeManager.greenStore()).isEqualTo("GreenStoreStub");
  }
}

Is effectively the same as:

    @Test
    void test() {

      try (BeanScope beanScope = BeanScope.newBuilder()
        .withMock(SomeStore.class, "Blue")
        .withMock(SomeStore.class, "green")
        .build()) {

        final SomeStore greenStore = beanScope.get(SomeStore.class, "green");
        final SomeStore blueStore = beanScope.get(SomeStore.class, "blue");
        when(blueStore.store()).thenReturn("BlueStoreStub");
        when(greenStore.store()).thenReturn("GreenStoreStub");

        final StoreManagerWithSetterQualifier storeManager = beanScope.get(StoreManagerWithSetterQualifier.class);
        assertThat(storeManager.blueStore()).isEqualTo("BlueStoreStub");
        assertThat(storeManager.greenStore()).isEqualTo("GreenStoreStub");
      }

    }
@rbygrave rbygrave added the enhancement New feature or request label May 26, 2021
@rbygrave rbygrave added this to the 6.0.RC3 milestone May 26, 2021
@rbygrave rbygrave self-assigned this May 26, 2021
rbygrave added a commit that referenced this issue May 26, 2021
Additionally:
#114 Add to Builder API support for withMock() and withSpy() taking name qualifier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant