-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fixes: #3343 TextUtils.isEmpty creates problems when unit testing with Mockito #3344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maskaravivek
merged 3 commits into
commons-app:master
from
kbhardwaj123:issue-with-mockito
Jan 28, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package android.text; | ||
|
||
|
||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* This Class Mocks TextUtils for the purpose of testing. | ||
* NOTE: This class would not change the function of the TextUtils used in app | ||
* it onlt mocks it for the unit tests | ||
* | ||
*/ | ||
public class TextUtils { | ||
/** | ||
* mocks TextUtils.isEmpty | ||
*/ | ||
public static boolean isEmpty(@Nullable CharSequence str) { | ||
return str == null || str.length() == 0; | ||
} | ||
|
||
/** | ||
* mocks TextUtils.equals | ||
*/ | ||
public static boolean equals(CharSequence a, CharSequence b) { | ||
if (a == b) return true; | ||
int length; | ||
if (a != null && b != null && (length = a.length()) == b.length()) { | ||
if (a instanceof String && b instanceof String) { | ||
return a.equals(b); | ||
} else { | ||
for (int i = 0; i < length; i++) { | ||
if (a.charAt(i) != b.charAt(i)) return false; | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* mocks TextUtils.isDigitsOnly | ||
*/ | ||
public static boolean isDigitsOnly(CharSequence str) { | ||
final int len = str.length(); | ||
for (int cp, i = 0; i < len; i += Character.charCount(cp)) { | ||
cp = Character.codePointAt(str, i); | ||
if (!Character.isDigit(cp)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* mocks TextUtils.isNewline | ||
*/ | ||
private static boolean isNewline(int codePoint) { | ||
int type = Character.getType(codePoint); | ||
return type == Character.PARAGRAPH_SEPARATOR || type == Character.LINE_SEPARATOR | ||
|| codePoint == 10; | ||
} | ||
|
||
/** | ||
* Returns whether the given CharSequence contains any printable characters. | ||
*/ | ||
public static boolean isGraphic(CharSequence str) { | ||
final int len = str.length(); | ||
for (int cp, i=0; i<len; i+=Character.charCount(cp)) { | ||
cp = Character.codePointAt(str, i); | ||
int gc = Character.getType(cp); | ||
if (gc != Character.CONTROL | ||
&& gc != Character.FORMAT | ||
&& gc != Character.SURROGATE | ||
&& gc != Character.UNASSIGNED | ||
&& gc != Character.LINE_SEPARATOR | ||
&& gc != Character.PARAGRAPH_SEPARATOR | ||
&& gc != Character.SPACE_SEPARATOR) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kbhardwaj123 Where is this class getting used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In short, take any piece of code containing
TextUtils.isEmpty()
it will always return false even if you pass null, you can see that in the screenshot above, this class mocks that and gives perfect result again refer to second screenshot aboveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discovered this while i was unit testing on
CategoryModel
in other PR (which is also ready for you to review :) )There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't understand, where is this class used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It goes like this:
Mockito is not capable of mocking TextUtils.isEmpty
So inside a unit test
TextUtils.isEmpty(null)
returnsfalse
which is Wrong!See the first screenshot I posted above,
This Class mocks the TextUtils class Correctly, please refer to my second screenshot
So This Class will be used by every unit test which tries to test a piece of code which contains
TextUtils.isEmpty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ashishkumar468 my solution takes inspiration from:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The startUpload() inside UploadControllerTest.kt tests
uploadController.startUpload
at line 119 in UploadController.java, that's one of the instances where this class will be used