I need to mock a static method on an android instrumentation test.
If i need to mock a static method, the test class needs to be #RunWith(PowerMockRunner.class) . But my instrumentation test are required to run with AndroidJUnit4.class .
Is it possible to have two runnable? or is there any other way i can use power mock to mock static methods? or any other options to mock static classes?
To handle cases like this since version 1.4 it's possible to bootstrap PowerMock using a JUnit Rule instead of the runner. Something looking like this:
#RunWith(AndroidJUnit4.class)
#PrepareForTest(X.class);
public class MyTest {
#Rule
PowerMockRule rule = new PowerMockRule();
// Tests goes here
...
}
But be aware that PowerMock is using byte-code manipulation which someone needs to convert to dalvikVM dex. And currently there are no tools to support that (https://groups.google.com/forum/#!topic/powermock/9kwPaWoZ_14 , https://stackoverflow.com/a/27956309/624706)
Related
I am using Mockito to run tests in an android module.
My tests works fine before adding the configuration for mock-maker-inline in src/test/resources/mockito-extensions file. But after adding this configuration some of my tests are not working any more.
Here is the code I'm using to mock my class.
private static Fragment createMock() {
final Fragment stubFragment =
mock(Fragment.class, withSettings().extraInterfaces(MyInterface.class));
when(((MyInterface) stubFragment).getFragment()).thenReturn(stubFragment);
return stubFragment;
}
And this is the exception
org.mockito.exceptions.misusing.MissingMethodInvocationException
I don't need Mockito inline for this class as it works without it but I need it for some other classes in the same module. Any help or suggestions will be appreciated
I used to work in MVP and I usually test my presenters using a plain Junit (Not the Instrumentation !) , since Presenters only have the business logic and no references to Android internals whatsoever.
Now by switching to Dagger 2 , I understood that I have a problem setting up a "TestModule" for my app component.
Creating a component will not work from within a test class (probably because "apt" is not running there)
Didn't find any examples for using Dagger with a standard Junit testing. Every example I have found only relies on Instrumentation testing or Roboelectric (which basically mocks Activities and other Android related stuff) , but this is just a UI testing for me , and I don't need that.
Just to make things clear , I am talking about the tests that are located at app->src->test folder not the app->src->androidTest !
So do I do something wrong ? Or missing something ? Can anyone explain or give examples on how to use Dagger 2 in normal unit tests ?
I'm not sure if my solution will work for you but I see no reason it shouldn't.
First I created testInjectionComponent
#Singleton
#Component(modules = {MockNetworkModule.class})
public interface MockInjectionComponent extends InjectionComponent {
void inject(DaggerUnitTest daggerUnitTest);
}
Then my Unit Tests I add injection in the before method. like so:
#Before
public void setUp() throws Exception {
MockInjectionComponent mockInjectionComponent = DaggerMockInjectionComponent
.builder()
.mockNetworkModule(new MockNetworkModule())
.build();
mockInjectionComponent.inject(this);
}
Then I just Annotate my Injected Object.
EDIT :
Do not forget to add testApt "com.google.dagger:dagger-compiler:$daggerVersion" at your app.gradle file .
As mentioned by the accepted answer. Do not forget to add :
For Java
Android Test
androidTestAnnotationProcessor 'com.google.dagger:dagger-compiler:$dagger_version'
JUnit test
testAnnotationProcessor 'com.google.dagger:dagger-compiler:$dagger_version'
For Kotlin
Android Test
kaptAndroidTest 'com.google.dagger:dagger-compiler:$dagger_version'
JUnit test
kaptTest 'com.google.dagger:dagger-compiler:$dagger_version'
You don't need any dagger to test your presenter. Dagger's job is it to fullfill the dependencies of your classes (dependency injection).
For example you have this Presenter:
public class MyPresenter {
Database database;
ApiService apiService;
#Inject
public MyPresenter(final Database database, final ApiService apiService) {
this.database = database;
this.apiService = apiService;
}
}
Dagger will provide your Presenter with the database and apiService objects for your presenter to use them. When running the actual app (not a test) these will be real objects with real functionality.
When testing the presenter, you want to test only the presenter, everything else should be mocked.
So when you create the presenter in your PresenterTest, you create it with mocked versions of database and apiService.
You can then test how your presenter interacts with these object by
a. mocking the objects behaviour like
when(database.getSomething()).thenReturn(something)
b. verify your presenter does what you want it to do with these objects like
verify(database).saveSomething()
(pseudo code)
Standard way to mock would be Mockito.
You can swap out real modules with fake modules in two ways: do it at compile time using flavors as recommended by google architecture samples or at runtime by creating an abstract method which injects the real thing in production code and fake dependencies in test code. In the second case your test has to subclass the class that you want to mock and build the component from scrach
I have to build an app with sqlite usage. Now I want to write my unit tests. These unit tests should test my class SQLiteBridge. SQLiteBridge provides DAOs for every child class of Model.
Now I got the problem that I need a context to create my SQLiteBridge. SQLiteBridge creates and handles a SQLite database on the system..
Where to get the Context-Object from?
My setup is like here (so I'm using Junit4 [thanks god]):
http://tools.android.com/tech-docs/unit-testing-support
EDIT: I hope there is a way like the old AndroidTestCase to extend without losing Junit4. :)
As described here: https://code.google.com/p/android-test-kit/wiki/AndroidJUnitRunnerUserGuide
Use the InstrumentationRegistry to obtain the context.
However if you call InstrumentationRegistry.getContext() directly you may get an exception opening your database. I believe this is because the context returned by getContext() points to the instrumentation's context rather than that of your application / unit test. Instead use InstrumentationRegistry.getInstrumentation().getTargetContext()
For example:
#RunWith(AndroidJUnit4.class)
public class SqliteTest {
Context mMockContext;
#Before
public void setUp() {
mMockContext = new RenamingDelegatingContext(InstrumentationRegistry.getTargetContext(), "test_");
}
}
The RenamingDelegatingContext simply prefixes the file/database names with test_ to prevent you from overwriting data that you may have in the same simulator.
jUnit 4 (and perhaps other versions of jUnit) and androidx use:
ApplicationProvider.getApplicationContext();
See: Android Documentation
In my unit test, I've tried the following:
import org.jmock.Mockery;
import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser;
public class MyActivityTest extends ActivityUnitTestCase<MyActivity> {
private Mockery context = new Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
...
}
My intended use is to mock my project's Application subclass. However, when I run my tests, I get an java.lang.ExceptionInInitializerError. Can I not use the ClassImposteriser extension for running Android unit tests?
This is something I looked into extensively several months ago. Unfortunately the dalvik VM currently does not support the bytecode manipulations that are required to mock concrete classes.
So you won't be able to use any mocking library to mock a class. You'll have to extract an interface for each class you want to mock in your android tests and mock the interface instead.
Some further reading about the davlik limitations:
http://code.google.com/p/mockito/issues/detail?id=57
https://sites.google.com/site/androiddevtesting/
I have written two test cases in a package com.app.myapp.test
When I try to run them both of them are not getting executed, only one test case gets executed and stops.
I have written the following testsuite in the same package
AllTests.java
public class AllTests extends TestSuite {
public static Test suite() {
return new TestSuiteBuilder(AllTests.class).includePackages("./src/com.ni.mypaint.test","./src/com.ni.mpaint.test").build();
/* .includeAllPackagesUnderHere()
.build();*/
}
Is the code and location for this testsuite is correct?
Well, certainly leave off the '/src/' portion of the package listing for that invocation. Either way, the easiest and most flexible way to run your tests this is to make sure all your tests are in a subpackage of where AllTests is (e.g. com.app.myapp.test.tests) and use this for the suite:
public static Test suite() {
return new TestSuiteBuilder(AllTests.class)
.includeAllPackagesUnderHere().build();
}
Make sure your tests run individually, too, without the suite runner -- the suite won't pick up your tests if they're set up wrong to begin with.
(This is better than explicitly listing the package name since it's more portable -- you can rename your test package without breaking it, for example.)