Reputation: 51
I have some problems to unit testing my authentication method. This is my method :
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_;
}
This is my testing code :
@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);
}
I'm using Powermock because i have an error with Base64 which is not mocked. And now, when i use Powermock i have this error :
java.lang.NullPointerException at java.lang.String.(String.java:566) at com.example.local.app_android.Authentification.basicAuth(Authentification.java:96) at com.example.local.app_android.AuthentificationTest.basicAuth(AuthentificationTest.java:34) /.../ at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
(I have cut the middle of the error)
Do someone have an idea of where the error comes from?
EDIT 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'
Upvotes: 0
Views: 2193
Reputation: 1214
Actually you are missing the expect so:
Base64.encode(basicAuth.getBytes(),Base64.NO_WRAP)
Return null, which explain why the constructor of String throw a NPE.
You should also look at the documentation especially since you should be using replay and verify when you use mock and you are not (which means you are not testing that the mocks are called): https://github.com/powermock/powermock/wiki/MockStatic
Upvotes: 0
Reputation: 140505
The real answer is: you shouldn't write code that is so hard that you need PowerMock in order to test it.
Meaning: by using static, you created hard-to-test code. Instead: learn how to write code that is easy to test (start here for example).
Then refactor your code, to avoid the static call; for example by wrapping that encode() methods into a tiny wrapper interface (you can still implement the interface using those static methods by the way).
As said; the real answer is to avoid the need to mock static calls; and thus avoid the need for PowerMock. Beyond that; mocking static calls with PowerMock works fine; you simply have to follow the exact recipe as outlined by the PowerMock folks.
And to be clear about that: you still have to look into the exact details of what your solution; as user Charlesworth already predicts the next NPE problem in your code (and he is typically spot on).
Upvotes: 1