我正在使用 Twilio 库,特别是我试图在测试时注入 TwilioRestClient 的模拟。
它没有实现任何接口。我不能添加一个。
我不能从它继承,它在我不喜欢的构造函数中做了一些事情。
我需要在模拟中有几个方法来覆盖或替换 TwilioRestClient 的某些行为。
我该怎么做呢?
我试过内部匿名类无济于事。我已经尝试过子类化,但显然不起作用。那里有 Java 大师吗?
编辑,twilio sdk 版本;
public class TwilioRestClient {
/** The Constant VERSION. */
private static final String VERSION = "3.3.15";
构造函数:
/**
* Explcitly construct a TwilioRestClient with the given API credentials.
*
* @param accountSid
* the 34 character Account identifier (starting with 'AC'). This
* can be found on your Twilio dashboard page.
* @param authToken
* the 32 character AuthToken. This can be found on your Twilio
* dashboard page.
*
*/
public TwilioRestClient(String accountSid, String authToken) {
this(accountSid, authToken, null);
}
/**
* Explcitly construct a TwilioRestClient with the given API credentials and
* endpoint.
*
* @param accountSid
* the 34 character Account identifier (starting with 'AC'). This
* can be found on your Twilio dashboard page.
* @param authToken
* the 32 character AuthToken. This can be found on your Twilio
* dashboard page.
* @param endpoint
* the url of API endpoint you wish to use. (e.g. -
* 'https://api.twilio.com')
*/
public TwilioRestClient(String accountSid, String authToken, String endpoint) {
validateAccountSid(accountSid);
validateAuthToken(authToken);
this.accountSid = accountSid;
this.authToken = authToken;
if ((endpoint != null) && (!endpoint.equals(""))) {
this.endpoint = endpoint;
}
//Grab the proper connection manager, based on runtime environment
ClientConnectionManager mgr = null;
try {
Class.forName("com.google.appengine.api.urlfetch.HTTPRequest");
mgr = new AppEngineClientConnectionManager();
} catch (ClassNotFoundException e) {
//Not GAE
mgr = new ThreadSafeClientConnManager();
((ThreadSafeClientConnManager) mgr).setDefaultMaxPerRoute(10);
}
setHttpclient(new DefaultHttpClient(mgr));
httpclient.getParams().setParameter("http.protocol.version",
HttpVersion.HTTP_1_1);
httpclient.getParams().setParameter("http.socket.timeout",
new Integer(CONNECTION_TIMEOUT));
httpclient.getParams().setParameter("http.connection.timeout",
new Integer(CONNECTION_TIMEOUT));
httpclient.getParams().setParameter("http.protocol.content-charset",
"UTF-8");
this.authAccount = new Account(this);
this.authAccount.setSid(this.accountSid);
this.authAccount.setAuthToken(this.authToken);
}