我已经检查了所有关于这方面的堆栈溢出问题。但我找不到合适的解决方案。
public class TestAuthenticate {
private RestService rs;
private String token_actual = token1;
private Mockery context;
private Authenticate authenticate_object;
@Before
public void setup(){
context = new JUnit4Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
rs = new RestService();
}
@Test
public final void testAuthenticate() {
authenticate_object = context.mock(Authenticate.class);
context.checking(new Expectations() {
{
oneOf(authenticate_object).authenticate_method("username", "password");
will(returnValue(token1));
}
});
String token = rs.authenticate("username", "password");
System.out.println(token);
assertEquals(token_actual, token);
context.assertIsSatisfied();
}}
这是调用实际的身份验证方法,而不是模拟身份验证类。谁能告诉我我哪里做错了?
public class RestService {
public string authenticate(String user, String pass){
Authenticate auth = new Authenticate();
String res = auth.authenticate(user,pass);
}
return res;
}发布于 2015-04-06 12:08:35
通常,您将使用某种形式的依赖项注入将模拟AuthenticationService注入到RestService中。试着重写你的RestService,比如
public class RestService {
private final AuthenticateService auth;
public RestService(AuthenticateService auth) {
this.auth = auth;
}
public string authenticate(String user, String pass){
return auth.authenticate(user,pass);
}
}然后在你的测试中
rs = new RestService(authenticate_object); https://stackoverflow.com/questions/29464150
复制相似问题