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

Suggestions for porting InstantTaskExecutorRule? #66

Closed
autonomousapps opened this issue Mar 14, 2018 · 3 comments
Closed

Suggestions for porting InstantTaskExecutorRule? #66

autonomousapps opened this issue Mar 14, 2018 · 3 comments

Comments

@autonomousapps
Copy link

JUnit5 does not support Rules. Yet some tests, particularly Android tests, absolutely require Rules. One example is the InstantTaskExecutorRule required for unit testing ViewModels. I'm interested in writing a custom Extesnsion, but I don't really know where to start. Any ideas? Does this already exist?

@autonomousapps
Copy link
Author

Actually, turns out it wasn't so hard to replace that particular rule. Here it is:

class InstantTaskExecutorExtension : BeforeEachCallback, AfterEachCallback {

    override fun beforeEach(context: ExtensionContext) {
        ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() {
            override fun executeOnDiskIO(runnable: Runnable) {
                runnable.run()
            }

            override fun postToMainThread(runnable: Runnable) {
                runnable.run()
            }

            override fun isMainThread(): Boolean {
                return true
            }
        })
    }

    override fun afterEach(context: ExtensionContext?) {
        ArchTaskExecutor.getInstance().setDelegate(null)
    }
}

@mannodermaus
Copy link
Owner

mannodermaus commented Mar 15, 2018

Great to see you dived into the Extension sphere yourself already! With the instrumentation test support, I intend to provide replacements for core JUnit 4 APIs that evolve to become Extensions. Since this particular class is tied to Architecture Components, I won't include it into the core instrumentation-test library. However, this could become its own library for people to plug into their project eventually. For now, I'll consider to link to this for the upcoming wiki on usage with Instrumentation Tests. Thank you!

@autonomousapps
Copy link
Author

I can confirm that extension works perfectly in my unit test suite. I'll keep an eye on this project and contribute any other extensions I come across (actually, I already have one to enable the @Mock annotation, but I found that elsewhere). I'm sure some way or other we'll end up with some test libs to share!

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

No branches or pull requests

2 participants