我有一些單元測試我的驗證方法的問題。這是我的方法:單元測試基本驗證
public int basicAuth(String username, String password) throws IOException {
connection = (HttpURLConnection)new URL("URL").openConnection();
connection.setRequestMethod("GET");
String basicAuth = username + ":" + password;
String wrap = "Basic " + new String(Base64.encode(basicAuth.getBytes(),Base64.NO_WRAP));
connection.setRequestProperty("Authorization",basicAuth);
connection.connect();
//InputStream in = connection.getInputStream();
int response = connection.getResponseCode();
Log.v("Reponse HTTP : ", String.valueOf(response));
responseCode_ = connection.getResponseCode();
return responseCode_;
}
這是我的測試代碼:
@Test
public void basicAuth() throws Exception {
PowerMockito.mockStatic(Base64.class);
String username = "username";
String password = "password";
int responseCode = 200;
int integer = auth.basicAuth(username, password);
assertEquals(responseCode, integer);
}
我使用Powermock因爲我有Base64編碼的錯誤,不嘲笑。而現在,當我使用Powermock我有這樣的錯誤:在java.lang.String中
顯示java.lang.NullPointerException (String.java:566) 在com.example.local.app_android.Authentification。 basic.Auth(Authentification.java:96) at com.example.local.app_android.AuthentificationTest.basicAuth(AuthentificationTest.java:34) /.../ at java.lang.reflect.Method.invoke(Method.java: 498) 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
(我已經削減錯誤的中間部分)
有人知道錯誤來自哪裏嗎?
編輯N°1
testCompile 'org.powermock:powermock-api-mockito:1.6.1'
testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.1'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1'
testCompile 'org.powermock:powermock-module-junit4:1.6.1'
testCompile 'org.mockito:mockito-core:1.10.19'
你可以只是做一些調試來找出什麼是空的。但提示 - 嘲弄某事意味着它的任何方法都不會做任何事情...... –
看來我的錯誤來自「basicAuth =」Basic「+ new String(Base64.encode(basicAuth.getBytes(),Base64)。NO_WRAP));「 我真的不是專家;是否意味着我不應該使用模擬? 因爲當我不這樣做時,它不起作用 –