Wednesday, 30 October 2019

Android Unit Testing with Mockito

I have a ViewModel in which there is a method which has the following line of code:

billDate.set(!TextUtils.isEmpty(SampleApp.getInstance().getAccountManager().getDueDate()) ?
            String.format(SampleApp.getInstance().getApplicationContext().getString(R.string.due),
                    SampleApp.getInstance().getAccountManager().getBillingDueDate()) :
            SampleApp.getInstance().getApplicationContext().getString(R.string.missing_due_date));

I have a test class using Mockito to test the different methods in ViewModel. But it is failing with NullPointerException at this line

String.format(SampleApp.getInstance().getApplicationContext().getString(R.string.due),

Below is the log:

java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Matcher.java:1283)
at java.util.regex.Matcher.reset(Matcher.java:309)
at java.util.regex.Matcher.<init>(Matcher.java:229)
at java.util.regex.Pattern.matcher(Pattern.java:1093)
at java.util.Formatter.parse(Formatter.java:2547)
at java.util.Formatter.format(Formatter.java:2501)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)

While running test case, I see the log showing some error related to Pattern Can somebody suggest, how to test the String.format method?



from Android Unit Testing with Mockito

No comments:

Post a Comment