I would like to have the ability to inject dependencies into JUnit tests with Dagger 2 (I'm new to this framework). Coming from Spring, there you can do something like this:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class MyTestClass {
@Autowired
private MyService service;
@Test
public void testMySerivce() { /* ... */ }
}
... but with Dagger 2 I have not yet found a solution that does not rely on an explicit DaggerMyComponent.builder().build().myService()
.
Ideally, I would imagine the solution to look something like the following:
// tell JUnit that dagger needs to do some post processing
@RunWith(DaggerJUnit4Runner.class)
// tell dagger which component classes to use for injection
@Components(MyComponent.class)
public class MyTestClass {
@Inject
private MyService service;
@Test
public void testMySerivce() { /* ... */ }
}
Unfortunately, there is no DaggerJunit4ClassRunner
.
Any hints on how this could be accomplished would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire