【发布时间】:2015-01-17 20:42:53
【问题描述】:
我有这门课
public class AuthenticationModule {
String userName = "foo";
String password = "bar";
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password ) {
this.password = password ;
}
AuthenticationServicePort authenticationServicePort;
AuthenticationService port;
private boolean authenicate(String userName, String password) {
authenticationServicePort = new AuthenticationServicePort();
port = authenticationServicePort.getAuthenticationServiceProxy();
return port.login(userName, password);
}
public boolean validateUser() {
return authenicate(userName, password);
}
}
和AuthenticationServicePort 返回一个 WSDL 端口
我想用 Mock AuthenticationServicePort 创建一个简单的测试用例,它将返回一个“真/假”值
如何在不更改 java 代码的情况下注入我自己的 MockObject?
或者更糟糕的情况,什么是最简单的改变方法,使其更容易测试。
【问题讨论】:
-
请使用 Easymock-powermock 并先尝试一下,如果遇到问题,这里的人可以随时提供帮助
-
AuthenticationServicePort是您自己的课程,还是您无法更改的库中的内容?如果是后者,它是在接口中实现还是扩展抽象类,以便您可以创建另一个具有相同方法的类来实现/扩展相同的接口/抽象类? -
它来自一个库,它不扩展或实现类/接口。 AuthenicationService 是一个我希望模拟的接口,但我的问题似乎是硬编码的 'authenticationServicePort = new AuthenticationServicePort();'
-
那么您可能需要创建一个实现接口的“包装器”类。包装类将包含一个
AuthenticationServicePort作为私有字段,并且该类的方法可以基本上只是调用AuthenticationServicePort上的相同方法(没有理由接口必须完全不过一样)。实现相同接口的另一个类将实现与模拟相同的方法。然后,您将声明authenticationServicePort为您的新接口,并注入该接口的一个实例。
标签: java unit-testing junit mocking jmockit