これは私のクラスとそのコンストラクタと依存関係です。コントローラテストに依存関係を挿入するにはどうすればよいですか?
public class FavouriteProfilesController extends BaseController implements CurrentUser, JsonHelper {
private final UserProvider userProvider;
private MessagesApi msg;
@javax.inject.Inject
public FavouriteProfilesController(
UserProvider userProvider,
MessagesApi msgApi) {
this.userProvider = userProvider;
this.msg = msgApi;
}
// methods etc...
これは私がちょうどドキュメントからコピーしたテストコードです:
public class FavouriteProfilesControllerTest extends WithApplication {
@Override
protected Application provideApplication() {
return new GuiceApplicationBuilder()
.configure("play.http.router", "javaguide.tests.Routes")
.build();
}
@Test
public void testIndex() {
Result result = new FavouriteProfilesController().index(); // Inject dependencies here
assertEquals(OK, result.status());
assertEquals("text/html", result.contentType().get());
assertEquals("utf-8", result.charset().get());
assertTrue(contentAsString(result).contains("Welcome"));
}
}
コントローラは2つの依存関係、UserProviderとMessagesApiを持って、どのように私は、コントローラのテストにそれらをあざけり/注入していますか?
優秀、ありがとう! –