I am trying to run local tests to test an algorithm I have for detecting certain URL's from a source string.
I have a class, CalendarUtil, that interfaces with the users calendar and parses their meeting information from the title, location and description fields.
Instead of doing an integration test, I decided to go local since I just want to test this algorithm with hard-coded input to make sure I am getting expected results without relying on the other Android API's.
CalendarUtil acts like a model class in that it has references to the meetings 3 fields mentioned above, so in my @Before setUp() method I create an object and only set those three fields with appropriate strings.
I then created a @Test function for parsing URL's and this is the stack trace of the error:
java.lang.NullPointerException
at com.myapp.utils.StringUtil.getStringMatches(StringUtil.java:94)
at com.myapp.utils.StringUtil.extractUrls(StringUtil.java:26)
at com.myapp.utils.CalendarUtil.parseAndRemoveUrls(CalendarUtil.java:186)
at com.myapp.MeetingDetectionTests.parseWebexUrl(MeetingDetectionTests.java:42)
At line 94 inside StringUtil this is the method:
/**
* Generic function that will take in a source that will be parsed according to a provided Regex pattern
*
* @param source The string to parse
* @param pattern The regex pattern to use
* @return A HashMap with the key being the match from the source and the values being the NSRange of where the match was found in the source.
*/
private static HashMap<String, NSRange> getStringMatches(final String source, final Pattern pattern) {
Matcher matcher = pattern.matcher(source); //Line 94, NullPointerException
//The key is our match in the source; the values is an array of integers representing the start and end index of the match
HashMap<String, NSRange> retVal = new HashMap<>();
while (matcher.find())
//Add the match as a key to our hashmap with an NSRange indicated where in the string it was found
retVal.put(matcher.group(), new NSRange(matcher.start(), matcher.end() - matcher.start()));
return retVal;
}
The passed value for the pattern is Patterns.WEB_URL which is an android provided regex for URL's. It is showing up as null in my test, but works when running my app.
Any ideas what is happening?