How do I add dependencies in a Junit test case using Guice before running @Rule?

The framework I'm working with is Dropwizard 7, Guice, and for testing we have Junit with Jukito. I have a resource written in dw, and I need to write a test case corresponding to this resource. Note. We recently moved from dw 6 to dw 7.

In dw 6, we had test cases:

@RunWith(JukitoRunner.class)
public class AbcResourceTest extends ResourceTest{
  @Inject
  private Provider<XyzAction> xyzProvider;
  public void setUpResources() throws Exception {
   addResource(new AbcResource(xyzProvider));
  }
  @Test
  public void doTesting() {
  }
}

This method worked just fine, Guice introduces all the dependency, and the resource will be initialized just fine.

But in DW 7, the syntax for writing a resource test has changed to the following

public class ResourceTest {
 PersonDao personDao = mock(PersonDao.class);
 @Rule public ResourceTestRule resources = ResourceTestRule
      .builder()
      .addResource(new Resource(personDao))
      .build();
}

This is an example from the dw documentation and works great. But if instead of mocking PersonDao, if I try to enter something like this:

@RunWith(JukitoRunner.class)
public class AbcResourceTest {
  @Inject
  private Provider<XyzAction> xyzProvider;
 @Rule public ResourceTestRule resources = ResourceTestRule
      .builder()
      .addResource((new AbcResource(xyzProvider))
      .build();
  @Test
  public void doTesting() {
  }
}

xyzProvider. Guice xyzProvider, , @Rule . , , Guice , @Rule. ?

+4
1

, JukitoRunner , @Rule. , , . , - ( Java 8):

@Inject
private XyzAction xyz;

@Rule
public ResourceTestRule resources = ResourceTestRule
        .builder()
        .addResource(new AbcResource(() -> xyz))
        .build();
+3

All Articles