Java 使用Mockito';何时';控制器内测试
我正在尝试在Play framework 2.4.6中测试我的控制器方法。 在我的控制器方法中,我有以下代码:Java 使用Mockito';何时';控制器内测试,java,junit,mockito,guice,playframework-2.4,Java,Junit,Mockito,Guice,Playframework 2.4,我正在尝试在Play framework 2.4.6中测试我的控制器方法。 在我的控制器方法中,我有以下代码: User user = accountService.getUserByEmail(email); if (user == null) { //Path A } //Path B 运行测试时,user将为空。因此,我无法测试路径B。我尝试在时使用Mockito返回一个用户,但也不起作用。还有别的办法吗 下面是我的测试代码: RequestBuilder request =
User user = accountService.getUserByEmail(email);
if (user == null) {
//Path A
}
//Path B
运行测试时,user
将为空。因此,我无法测试路径B。我尝试在时使用Mockito返回一个用户,但也不起作用。还有别的办法吗
下面是我的测试代码:
RequestBuilder request = new RequestBuilder()
.method("POST")
.bodyForm(ImmutableMap.of("email", "test@test.com"))
.uri(controllers.routes.ResetPasswordController.postResetPassword().url());
when(accountService.getUserByEmail(anyString())).thenReturn(new User());
assertEquals(OK, route(request).status());
感谢@Andriy为我指明了依赖注入的正确方向。
我通过以下设置解决了这个问题
测试:
public class TestClass {
@Inject
Application application;
final AccountService accountServiceMock = mock(AccountService.class);
@Before
public void setup() {
Module testModule = new AbstractModule() {
@Override
public void configure() {
bind(AccountService.class).toInstance(accountServiceMock);
}
};
GuiceApplicationBuilder builder = new GuiceApplicationLoader()
.builder(new ApplicationLoader.Context(Environment.simple()))
.overrides(testModule);
Guice.createInjector(builder.applicationModule()).injectMembers(this);
Helpers.start(application);
}
@Test
public void testMethod() throws Exception {
RequestBuilder request = new RequestBuilder()
.session("userId", "1")
.uri(controllers.routes.AccountController.addAccount().url());
running(application, () -> {
when(accountServiceMock.addAccount().thenReturn(true);
assertEquals(OK, route(request).status());
});
}
@Singleton
public class AccountController extends Controller {
private AccountService accountService;
@Inject
public Controller(AccountService a) {
accountService = a;
}
public Result addAccount() {
boolean success = accountService.addAccount();
}
}
@ImplementedBy(AccountServiceImpl.class)
public interface AccountService {
boolean addAccount();
}
public class AccountServiceImpl implements AccountService {
@Override
public boolean addAccount() {
}
}
控制器:
public class TestClass {
@Inject
Application application;
final AccountService accountServiceMock = mock(AccountService.class);
@Before
public void setup() {
Module testModule = new AbstractModule() {
@Override
public void configure() {
bind(AccountService.class).toInstance(accountServiceMock);
}
};
GuiceApplicationBuilder builder = new GuiceApplicationLoader()
.builder(new ApplicationLoader.Context(Environment.simple()))
.overrides(testModule);
Guice.createInjector(builder.applicationModule()).injectMembers(this);
Helpers.start(application);
}
@Test
public void testMethod() throws Exception {
RequestBuilder request = new RequestBuilder()
.session("userId", "1")
.uri(controllers.routes.AccountController.addAccount().url());
running(application, () -> {
when(accountServiceMock.addAccount().thenReturn(true);
assertEquals(OK, route(request).status());
});
}
@Singleton
public class AccountController extends Controller {
private AccountService accountService;
@Inject
public Controller(AccountService a) {
accountService = a;
}
public Result addAccount() {
boolean success = accountService.addAccount();
}
}
@ImplementedBy(AccountServiceImpl.class)
public interface AccountService {
boolean addAccount();
}
public class AccountServiceImpl implements AccountService {
@Override
public boolean addAccount() {
}
}
界面:
public class TestClass {
@Inject
Application application;
final AccountService accountServiceMock = mock(AccountService.class);
@Before
public void setup() {
Module testModule = new AbstractModule() {
@Override
public void configure() {
bind(AccountService.class).toInstance(accountServiceMock);
}
};
GuiceApplicationBuilder builder = new GuiceApplicationLoader()
.builder(new ApplicationLoader.Context(Environment.simple()))
.overrides(testModule);
Guice.createInjector(builder.applicationModule()).injectMembers(this);
Helpers.start(application);
}
@Test
public void testMethod() throws Exception {
RequestBuilder request = new RequestBuilder()
.session("userId", "1")
.uri(controllers.routes.AccountController.addAccount().url());
running(application, () -> {
when(accountServiceMock.addAccount().thenReturn(true);
assertEquals(OK, route(request).status());
});
}
@Singleton
public class AccountController extends Controller {
private AccountService accountService;
@Inject
public Controller(AccountService a) {
accountService = a;
}
public Result addAccount() {
boolean success = accountService.addAccount();
}
}
@ImplementedBy(AccountServiceImpl.class)
public interface AccountService {
boolean addAccount();
}
public class AccountServiceImpl implements AccountService {
@Override
public boolean addAccount() {
}
}
实施:
public class TestClass {
@Inject
Application application;
final AccountService accountServiceMock = mock(AccountService.class);
@Before
public void setup() {
Module testModule = new AbstractModule() {
@Override
public void configure() {
bind(AccountService.class).toInstance(accountServiceMock);
}
};
GuiceApplicationBuilder builder = new GuiceApplicationLoader()
.builder(new ApplicationLoader.Context(Environment.simple()))
.overrides(testModule);
Guice.createInjector(builder.applicationModule()).injectMembers(this);
Helpers.start(application);
}
@Test
public void testMethod() throws Exception {
RequestBuilder request = new RequestBuilder()
.session("userId", "1")
.uri(controllers.routes.AccountController.addAccount().url());
running(application, () -> {
when(accountServiceMock.addAccount().thenReturn(true);
assertEquals(OK, route(request).status());
});
}
@Singleton
public class AccountController extends Controller {
private AccountService accountService;
@Inject
public Controller(AccountService a) {
accountService = a;
}
public Result addAccount() {
boolean success = accountService.addAccount();
}
}
@ImplementedBy(AccountServiceImpl.class)
public interface AccountService {
boolean addAccount();
}
public class AccountServiceImpl implements AccountService {
@Override
public boolean addAccount() {
}
}
我对这里的概念知之甚少,但大致上:
- 控制器是无状态的,就像HTML一样,因此您需要运行时依赖项注入来识别模拟对象
有用的文档:
您的accountService
模拟实例必须由Play framework知道。E.E.注册为播放服务。@AndriyKryvtsun抱歉,请详细说明注册为播放服务
?我在谷歌上找不到任何有意义的结果。