Skip to content
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

Prevent DebugLog logs messages when running code under JUnit. #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Prevent DebugLog logs messages when running code under JUnit. #30

wants to merge 1 commit into from

Conversation

VictorAlbertos
Copy link

Hi.

I've been getting the next exception when running a JUnit test which calls methods annotated with @RxLogObservable

java.lang.RuntimeException: Method d in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details.

    at android.util.Log.d(Log.java)
    at com.fernandocejas.frodo.internal.DebugLog.log(DebugLog.java:21)
    at com.fernandocejas.frodo.internal.MessageManager.printMessage(MessageManager.java:20)
    at com.fernandocejas.frodo.internal.MessageManager.printObservableInfo(MessageManager.java:25)
    at com.fernandocejas.frodo.internal.observable.FrodoObservable.getObservable(FrodoObservable.java:28)
    at com.fernandocejas.frodo.aspect.LogObservable.weaveAroundJoinPoint(LogObservable.java:39)
    at app.data.sections.chat.ChatRepository.getChats(ChatRepository.java:30)
    at app.data.sections.chat.ChatRepositoryTest.When_Remove_Chat_Then_Remove_Chat(ChatRepositoryTest.java:29)
....

I've been able to fix the error by adding the next configuration test to gradle:

android {
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

This pull request tries to fix that. I've been able to compile the project and run successfully the unit tests of "frodo-runtime" module. But it seems that DebugLog class isn't covered by any of them (probably not need for that until now).

Also, I don't know how can I test this new functionality from the android sample app. I couldn't connect the "frodo-plugin" gradle locally with the sample app. Indeed, I don't even know if that's possible (I have a very limited knowledge about gradle plugins).

@@ -18,6 +21,28 @@
* @param message The message you would like logged.
*/
public void log(String tag, String message) {
Log.d(tag, message);
if (!runningJUnit())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it would be better to just catch all Exceptions as it won't slow down execution of unit tests as much as checking the stack trace. Moreover, this implementation is tied to JUnit. What if we are just running our code on desktop?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to catch the exception but I think this kind of exception is not possible to catch.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should just work:

try {
    Log.d(tag, message);
} catch (Exception e) {
    // Ignore
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't work when I tried it. It was my first approach, actually. But you can try it and let me know, maybe I missed something I don't know ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants