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
Test Application's onCreate method called for every test #3108
Comments
This is expected behavior as you don't want to share state across tests.
I'm assuming that TrustKit uses some static values which is causing the
pollution.
Two things I can think of:-
1) Make your test application implement TestLifeCycle where you have an
afterTest() method you can use to reset that static state.
2) Have a test application that doesn't use TrustKit. At least, when your
unit testing it's better to just use a bare bones test application rather
than incur high test startup costs due to initialization that may not be
needed for that test.
J
On May 19, 2017 8:44 AM, "apperside" <notifications@github.com> wrote:
Description
Hi, I configured Robolectric 3.1.4 to use a custom Application class for
tests, and it works.
The problem is that the onCreate() method of such class is called for every
test.
It could be an accetable behaviour if everything related to the Application
was killed, but I am using for example TrustKit, which initialization code
is run in the Application's onCreate method, and when it's called for the
second time (when the second test runs) it complains abut the fact that it
has already been initialized, so this should mean that not all the things
related to the application lifecycle are released.
Is this an expected behaviour or is it a bug? Shouldn't the onCreate method
of the Application class called only once?
Thank you
Steps to Reproduce
Configure Robolectric to use a custom application class using the following
code
@config(constants = BuildConfig.class, manifest = "../app/AndroidManifest.xml",
packageName = BuildConfig.APPLICATION_ID, application =
MyTestApplicationClass.class, sdk = Build.VERSION_CODES.LOLLIPOP)
public abstract class ApplicationTestCase
Run a test suite containing more than one test
The onCreate() method of MyTestApplicationClass is called for every test
Robolectric & Android Version
Robolectric 3.1.4
Android Studio 2.3.2
buildToolsVersion "25.0.2"
gradle 3.3
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#3108>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGBaeJUpC3ZNnYwvWEfH5gVbWC9Y7Q9wks5r7bjggaJpZM4NgsTO>
.
|
Another option is to add a Shadow for TrustKit and make the problematic method calls no-ops. I haven't use TrustKit specifically so I don't know how feasible this is. |
It's a little bizarre that I'm facing this problem now after years of working with Roboelectric. Tests have been working fine until recently... Worked my way around this like this
|
You actually get a new Application object for each test because tests
should be hermetic so it sounds like you have some static state somewhere
that is not being torn down.
…On Thu, Oct 11, 2018, 3:47 AM Morten Slott Hansen ***@***.*** wrote:
It's a little bizarre that I'm facing this problem *now* after years of
working with Roboelectric. Tests have been working fine until recently...
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3108 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGBaePdviXkAhcxhNxHkkrqUTiYUZdoPks5ujyHKgaJpZM4NgsTO>
.
|
Facing the same problem right now in the process of migrating to Robolectric 4.0.2. The problem arises from Robolectric creating multiple instances of |
This issue should be reopened. This is happening with for me also with Segment which can only have on initialization |
The problem is, tests should be isolated, if you share state between tests,
ie. Through the application class you're likely to get test pollution.
If your application does lots of stuff that isn't necessary for unit
testing I'd recommend using a test application that is more lightweight.
You can specify this though your test manifest, or @config(application =
Mytest application.class) or through the equivalent in properties config
file.
…On Mon, Nov 26, 2018, 11:17 AM Adrian Devezin ***@***.*** wrote:
This issue should be reopened. This is happening with for me also with
Segment which can only have on initialization
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3108 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGBaeI2TRVC8bUvUcasAFV17Dt1wbfGNks5uzCIQgaJpZM4NgsTO>
.
|
@jongerrish That doesn't help when you want segment to be in your test but cant have it initialized more than once. And the other classes people here are using. I am using @config(application =Mytestapplication.class) on my class. Im not disagreeing your comments about the test pollution, however then everything should be properly torn down by robolectric (or a new process) if that is the case. |
So Segment has a static initializer that cannot be called twice and it
doesn't have it's own "reset for testing" method?
You could use a custom shadow to reset it, although this could be brittle
as it would end up depending on the internals of that library.
If we did run each test without reusing the SandboxClassloader to get a
more isolated test environment I wonder what cost that would incur on test
runtime, given the number of Android classes that are loaded and the loss
of JIT.
…On Mon, Nov 26, 2018, 11:26 AM Adrian Devezin ***@***.*** wrote:
@jongerrish <https://github.com/jongerrish> That doesn't help when you
want segment to be in your test but cant have it initialized more than
once. And the other classes people here are using. I am using @config
<https://github.com/config>(application =Mytestapplication.class) on my
class.
Im not disagreeing your comments about the test pollution, however then
everything should be properly torn down by robolectric (or a new process)
if that is the case.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3108 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGBaeBQ_gT0aOFeE40ESYzGevpAByJRIks5uzCRLgaJpZM4NgsTO>
.
|
Facing the same problem with Koin. Calling startKoin in Application causing |
Description
Hi, I configured Robolectric 3.1.4 to use a custom Application class for tests, and it works.
The problem is that the
onCreate()
method of such class is called for every test.It could be an accetable behaviour if everything related to the Application was killed, but I am using for example TrustKit, which initialization code is run in the Application's onCreate method, and when it's called for the second time (when the second test runs) it complains abut the fact that it has already been initialized, so this should mean that not all the things related to the application lifecycle are released.
Is this an expected behaviour or is it a bug? Shouldn't the onCreate method of the Application class called only once?
Thank you
Steps to Reproduce
Configure Robolectric to use a custom application class using the following code
Run a test suite containing more than one test
The onCreate() method of MyTestApplicationClass is called for every test
Robolectric & Android Version
Robolectric 3.1.4
Android Studio 2.3.2
buildToolsVersion "25.0.2"
gradle 3.3
The text was updated successfully, but these errors were encountered: