Maybe this is the most ridiculous question ever, or I just can’t think straight anymore – but I have experienced a very strange behaviour when using the URLUtil.isHttpsUrl()
from android.webkit
.
I was writing some Unittests for my project and detected that a method returned an unexpected value, because the URL was not considered valid.
Trying to narrow things down, I ended up with a simple testcase like this:
@Test public void testSimpleHttpsCheck() { String url = "https://www.this.ishttps.com"; Assert.assertTrue(isHttpsUrl(url)); // returns true Assert.assertTrue(URLUtil.isHttpsUrl(url)); // returns false } // this is a local copy of isHttpsUrl: // it is simple copied from the original URLUtil (SDK 25) private boolean isHttpsUrl(String url) { return (null != url) && (url.length() > 7) && url.substring(0, 8).equalsIgnoreCase("https://"); }
As you can see, I have simply copied the URLUtil.isHttpsUrl()
from the android.webkit
package.
I have used it with compileSdkVersion 25
and compileSdkVersion 24
.
Same happens with `URLUtil.isHttpUrl()
I already tried to clean + rebuild the project, restarted + invalidated cache of AS.
Can someone explain what is going on? Am I doing something wrong?
Or could there be a problem with the Unittest class?
It is a simple a JUnit Test and I have set the returnDefaultValues
in the gradle like this:
android { ... ... testOptions { unitTests.returnDefaultValues = true } }
Although, I would have assumed that “context-related” calls would get default values, but the URLUtil.isHttpsUrl()
is static and does not involve any context.
Advertisement
Answer
Ok, it seems like even static Util classes need an Instrumented Unit Test. I just tested it without the testOptions
and now it says Method isHttpsUrl in android.webkit.URLUtil not mocked.
Sorry for bothering you, I’ve just realized that it could have something to do with the testOptions
when I was ready to post my question. After 2 hours of debugging and nearly losing my faith in my programming skills.
Hope someone can use this answer though and don’t waste so much time on a simple Unittest case.