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

Test Application's onCreate method called for every test #3108

Closed
apperside opened this issue May 19, 2017 · 10 comments
Closed

Test Application's onCreate method called for every test #3108

apperside opened this issue May 19, 2017 · 10 comments

Comments

@apperside
Copy link

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

@jongerrish
Copy link
Contributor

jongerrish commented May 19, 2017 via email

@hoisie
Copy link
Contributor

hoisie commented May 19, 2017

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.

@slott
Copy link

slott commented Oct 11, 2018

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

public class MyTestApplication extends MyApplication {
	private static boolean onCreateCalled;

	@Override
	public void onCreate() {
		if(!onCreateCalled) {
			onCreateCalled = true;
			super.onCreate();
		}
	}
}

@jongerrish
Copy link
Contributor

jongerrish commented Oct 11, 2018 via email

@Egorand
Copy link

Egorand commented Nov 12, 2018

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 Application in the same process, which would never happen in the real world - if the application gets torn down the system will spawn another process and create a new Application instance inside it. If Robolectric's goal is to offer possibility to run tests on the real application code, in order to provide seamless switching between local JVM tests and on-device integration tests, then this is a pretty major contract to break.

@dri94
Copy link

dri94 commented Nov 26, 2018

This issue should be reopened. This is happening with for me also with Segment which can only have on initialization

@jongerrish
Copy link
Contributor

jongerrish commented Nov 26, 2018 via email

@dri94
Copy link

dri94 commented Nov 26, 2018

@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.

@jongerrish
Copy link
Contributor

jongerrish commented Nov 26, 2018 via email

@bio007
Copy link

bio007 commented Feb 1, 2020

Facing the same problem with Koin.

Calling startKoin in Application causing org.koin.core.error.KoinAppAlreadyStartedException: A Koin Application has already been started exception during tests

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

8 participants