12

I'm starting to work with unit tests in android I have trying to test a method that uses:

android.util.Patterns.EMAIL_ADDRESS.matcher(validEmail).matches()

it should return true as I added this in my build.gradle

testOptions { 
    unitTests {
        returnDefaultValues = true
    }
} 

Still the test throws a NullPointerException.

I have two questions: 1- How do I fix this? 2- Should I reconsider my design and remove the android dependency to a mock object.

// @ LoginPresenterTest
@Test
public void clickOnLogin_loginSuccess(){
    loginPresenter.login(validEmail, validPassword);
    verify(loginView).setLoginButton(false);
}
// LoginPresenter
public void login(String email, String password) {

    loginView.setLoginButton(false);
    if(!isValid(email, password)){
        loginView.setLoginButton(true);
        return;
    }
}
// Validation
public static boolean isEmailValid(String email){
    return !(email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches());
}

When android.utills is commented their will be no exception.

Abdelrhman Talat
  • 1,205
  • 2
  • 13
  • 25
  • 1
    I am not your dude. And it can help to know what a NPE is. Now you have to use a debuggeer, to find out what value is null – Jens Aug 01 '16 at 07:31
  • 2
    I am facing the same problem. You would usually need to mock the Pattern in order to try and match against it. However you cant easily do this. This tutorial is pretty good: (http://bytes.babbel.com/en/articles/2016-05-20-tdd-in-android.html) and show step by step how to get it working – Lauren Grimes Aug 02 '16 at 03:32
  • 2
    This question clearly has nothing to do with the "Duplicate" that is has been marked as being. – azza50 Mar 15 '18 at 01:24
  • 1
    "it should return true" is where you're getting confused. `unitTests.returnDefaultValues = true` does not mean that android framework methods will return true. It means they will return default values. `True` is not the default value for matcher(), the default value for methods that return an object is `null`, that's why you get an NPE. – Niall Jan 08 '19 at 15:09
  • https://stackoverflow.com/questions/41518223/mock-android-patterns-with-mockito/58902275#58902275 – Saku Nov 17 '19 at 15:51
  • 1
    It's easier to just use PatternsCompat.EMAIL_ADDRESS instead, no need to mock anything then. – Hylke Mar 01 '22 at 09:10

1 Answers1

4

static methods are not mocked. You need to use some mocking framework like mockito or powermock.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
  • shouldn't this always return true http://tools.android.com/tech-docs/unit-testing-support Last section – Abdelrhman Talat Aug 01 '16 at 07:32
  • @AbdelrhmanTalat `unitTests.returnDefaultValues = true` means that android framework mocked methods will return default values, not that the value they should return is `true`. The default value for methods that return an object is `null`, that's why you get an NPE. – Niall Jan 08 '19 at 15:05
  • To expand on this answer, _no_ Android framework methods are mocked by default, you have to mock them yourself. You can't mock static methods with Mockito (yet, although they seem to be working on it). [You can do it with PowerMock](https://github.com/powermock/powermock/wiki/mockstatic). – Niall Jan 08 '19 at 15:14