Skip to content

Commit

Permalink
Merge pull request #19026 from geoand/#12761
Browse files Browse the repository at this point in the history
Add test for generics field usage of @InjectMock
  • Loading branch information
geoand authored Jul 28, 2021
2 parents 6ff45d3 + 260c5dc commit fdc74b8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.it.mockbean;

import javax.inject.Inject;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;

@QuarkusTest
public class GenericFieldsTest {

@Inject
MyContainerConsumer myContainerConsumer;

@InjectMock
MyContainer<String> stringContainer;

@InjectMock
MyContainer<Integer> integerContainer;

@Test
public void test() {
Mockito.when(stringContainer.getValue()).thenReturn("hi");
Mockito.when(integerContainer.getValue()).thenReturn(2);
Assertions.assertEquals("hi hi", myContainerConsumer.createString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.it.mockbean;

public interface MyContainer<T> {

T getValue();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.it.mockbean;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Singleton;

@Singleton
public class MyContainerConsumer {

private final MyContainer<String> stringContainer;
private final MyContainer<Integer> integerContainer;

public MyContainerConsumer(MyContainer<String> stringContainer, MyContainer<Integer> integerContainer) {
this.stringContainer = stringContainer;
this.integerContainer = integerContainer;
}

public String createString() {
List<String> strings = new ArrayList<>(integerContainer.getValue());
for (int i = 0; i < integerContainer.getValue(); i++) {
strings.add(stringContainer.getValue());
}
return String.join(" ", strings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.it.mockbean;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Singleton;
import javax.ws.rs.Produces;

@Singleton
public class MyContainerProducer {

@ApplicationScoped
@Produces
public MyContainer<String> stringContainer() {
return new MyContainer<>() {
@Override
public String getValue() {
return "hello";
}
};
}

@ApplicationScoped
@Produces
public MyContainer<Integer> integerContainer() {
return new MyContainer<>() {
@Override
public Integer getValue() {
return 1;
}
};
}
}

0 comments on commit fdc74b8

Please sign in to comment.