I tried to create unit test for my project. Inside my unit test class I have two methods testCalculate() and testLogin(). Method testCalculate() running fine, meaning that test passed and I got correct testing result.
But problem is in testLogin(), I expect that my code will be print something inside the listener, but it never printed.Meaning that I never get this line
System.out.println("Login success ======= " + response.getResponseObject());
My login method that I want to test itself running fine, meaning if I use it inside my real app, it will login successfully and return some datas that I got from server.
Kindly advise what is possible cause that make my listener is not working in unit test. Really appreciate for any kind help.
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class LoginManagerTest {
private static final String TAG = LoginManagerTest.class.getCanonicalName();
private String usernameStr = "androidtest@gmail.com";
private String passwordStr = "********";
private LoginManager loginManager;
@Test
public void testLogin() throws Exception {
ResponseHandlerListener<String> listener = new ResponseHandlerListener<String>() {
@Override
public void onReceive(AxonResponse<String> response) {
System.out.println("Login success ======= " + response.getResponseObject());
String loginSuccess = Constants.LOGIN_SUCCESS;
assertEquals(TAG, loginSuccess, response.getResponseObject());
}
};
Context context = Robolectric.getShadowApplication().getApplicationContext();
loginManager = new LoginManager(context);
loginManager.login(usernameStr, passwordStr, null, listener);
}
@Test
public void testCalculate() throws Exception {
Context context = Robolectric.getShadowApplication().getApplicationContext();
loginManager = new LoginManager(context);
int num = 5;
int sum = loginManager.calculate(num);
System.out.println("sum = " + sum);
assertEquals(10, sum);
}
}