android-espresso


Espresso Test Failed: Wanted to match 1 intents. Actually matched 0 intents


I am trying to test that my SplashActivity correctly launches HomeActivity if the user is logged in.
I have looked at related questions on StackOverflow, this seems to be a common issue, but I just cannot seem to get anything to work.
I have watched the test execute on my device and visually verified that SplashActivity is indeed launching HomeActivity.
HomeActivity expects a plain intent with no data.
Here is the full error:
IntentMatcher: has component: has component with: class name: is "com.shoeboxed.fetch.presentation.ui.activities.HomeActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String
Initial Attempt:
public class SplashActivityTest extends EspressoIntegrationTest {
#Mock
UserRepository userRepository;
#Rule
public IntentsTestRule<SplashActivity> activityRule = new IntentsTestRule<>(SplashActivity.class, true, false);
#Test
public void loggedInUser() {
User user = Fakes.user();
when(userRepository.getUser()).thenReturn(user);
doNothing().when(userRepository).refreshTeam();
activityRule.launchActivity(new Intent());
intended(hasComponent(HomeActivity.class.getName()));
verify(userRepository, times(1)).getUser();
verify(userRepository, times(1)).refreshTeam();
}
}
Second Attempt:
(Trying different matching syntax)
public class SplashActivityTest extends EspressoIntegrationTest {
#Mock
UserRepository userRepository;
#Rule
public IntentsTestRule<SplashActivity> activityRule = new IntentsTestRule<>(SplashActivity.class, true, false);
#Test
public void loggedInUser() {
User user = Fakes.user();
when(userRepository.getUser()).thenReturn(user);
doNothing().when(userRepository).refreshTeam();
activityRule.launchActivity(new Intent());
intended(hasComponent(new ComponentName(getTargetContext(), HomeActivity.class)));
verify(userRepository, times(1)).getUser();
verify(userRepository, times(1)).refreshTeam();
}
}
Third Attempt
(Adding an Idling Resource on the destination activity)
public class SplashActivityTest extends EspressoIntegrationTest {
#Mock
UserRepository userRepository;
#Rule
public IntentsTestRule<SplashActivity> activityRule = new IntentsTestRule<>(SplashActivity.class, true, false);
#Test
public void loggedInUser() {
User user = Fakes.user();
when(userRepository.getUser()).thenReturn(user);
doNothing().when(userRepository).refreshTeam();
WaitActivityIsResumedIdlingResource resource = new WaitActivityIsResumedIdlingResource(HomeActivity.class.getName());
Espresso.registerIdlingResources(resource);
activityRule.launchActivity(new Intent());
intended(hasComponent(new ComponentName(getTargetContext(), HomeActivity.class)));
verify(userRepository, times(1)).getUser();
verify(userRepository, times(1)).refreshTeam();
Espresso.unregisterIdlingResources(resource);
}
private static class WaitActivityIsResumedIdlingResource implements IdlingResource {
private final ActivityLifecycleMonitor instance;
private final String activityToWaitClassName;
private volatile ResourceCallback resourceCallback;
boolean resumed = false;
public WaitActivityIsResumedIdlingResource(String activityToWaitClassName) {
instance = ActivityLifecycleMonitorRegistry.getInstance();
this.activityToWaitClassName = activityToWaitClassName;
}
#Override
public String getName() {
return this.getClass().getName();
}
#Override
public boolean isIdleNow() {
resumed = isActivityLaunched();
if(resumed && resourceCallback != null) {
resourceCallback.onTransitionToIdle();
}
return resumed;
}
private boolean isActivityLaunched() {
Collection<Activity> activitiesInStage = instance.getActivitiesInStage(Stage.RESUMED);
for (Activity activity : activitiesInStage) {
if(activity.getClass().getName().equals(activityToWaitClassName)){
return true;
}
}
return false;
}
#Override
public void registerIdleTransitionCallback(IdlingResource.ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}
}
Here is my base test. It registers an idling resource on my background processes (clean architecture use cases):
public class EspressoIntegrationTest {
private static IdlingResource idlingThreadPool;
private AppComponent oldComponent = app().appComponent();
#Rule
public DaggerMockRule<AppComponent> daggerRule = new DaggerMockRule<>(AppComponent.class, new AppModule(app()))
.set(component -> {
oldComponent = app().appComponent();
app().setAppComponent(component);
});
#BeforeClass
public static void registerResources() {
idlingThreadPool = getIdlingThreadExecutor();
Espresso.registerIdlingResources(idlingThreadPool);
}
#AfterClass
public static void deregister() {
Espresso.unregisterIdlingResources(idlingThreadPool);
}
#After
public void resetApp() {
app().setAppComponent(oldComponent);
}
private static IdlingResource getIdlingThreadExecutor() {
return (IdlingResource) jobExecutor().getThreadPoolExecutor();
}
private static JobExecutor jobExecutor() {
return ((JobExecutor) app().appComponent().threadExecutor());
}
private static App app() {
return (App) getInstrumentation().getTargetContext().getApplicationContext();
}
}
Asserts that the given matcher matches one and only one intent sent by the application under test. This is an equivalent of verify(mock, times(1)) in Mockito. Verification does not have to occur in the same order as the intents were sent. Intents are recorded from the time that Intents.init is called
See documentation
When you use IntentsTestRule Intents.init() will complated after activity created. As far as I understand, you starting HomeActivity in SplashActivity.onCreate and finish SplashActivity.
So you can use ActivityTestRule and call Intents.init() before launching activity, like this:
public class SplashActivityTest extends EspressoIntegrationTest {
#Mock
UserRepository userRepository;
#Rule
public ActivityTestRule<SplashActivity> activityRule = new ActivityTestRule<>(SplashActivity.class, true, false);
#Before
public void setUp() throws Exception{
Intents.init();
}
#Test
public void loggedInUser() {
User user = Fakes.user();
when(userRepository.getUser()).thenReturn(user);
doNothing().when(userRepository).refreshTeam();
activityRule.launchActivity(new Intent());
intended(hasComponent(HomeActivity.class.getName()));
verify(userRepository, times(1)).getUser();
verify(userRepository, times(1)).refreshTeam();
}
#After
public void tearDown() throws Exception{
Intents.release();
}
}

Related Links

Android, Espresso, ListView: How scroll to item and check this item?
Android, multiDexEnabled = true --> not start Espresso tests (NoClassDefFoundError)
Android, Espresso. How check is soft keyboard is under view?
How to validate whether opened correct activity - Espresso
Espresso Test Failed: Wanted to match 1 intents. Actually matched 0 intents
How test onData() column value is not null?
Android, Espresso: Is one view is under another view?
Espresso: How fast show new activity?
Android, Espresso: Show/Hide view depends of item content
Android. Unit test. Check is text is show in correct language
Cucumber Espresso and Android : How to use view matchers to find the particular view using resource id?
how to re-run failed tests using AndroidJunit4?
Espresso: typeText - not work in cyrilic
Espresso test - not recognize ad
Espresso: How do i assert if a particular activity is launched when i click on a item using espresso
Android, Espresso: How check if tag value contains some string?

Categories

HOME
webview
android-studio
macos-sierra
jms
softlayer
swi-prolog
hdfs
ios-simulator
informatica
cryptography
google-sheets-query
hyperledger
clone
ui-automation
jpa-criteria
swarm
yahoo
driver
amazon-emr
spring-amqp
live-streaming
squirrel-sql
jpa-2.0
corona
google-cloud-logging
rest-assured
asciimath
exe
hidden
gravity
string-formatting
cloudsim
social-tables
bsd
restier
firemonkey-style
math.js
annotation-processing
laravel-4.2
tilemill
premake
argv
sca
gsp
cloudera-manager
java-stream
polyml
bean-validation
nppexec
cordys-opentext
strapi
unification
text-decorations
praat
pyttsx
sonarqube5.2
directwrite
webalizer
django-static-precompiler
password-hash
nusoap
openstack-glance
redisson
android-sharing
android-webservice
mura
stateless-session-bean
google-shopping-api
petsc
oracle-fusion-apps
metabase
nclam
odp.net-managed
highlighting
strpos
flipclock
cortana
apple
kefir.js
phpredis
com0com
flow-js
shoes
rfc5545
python-c-extension
jcheckbox
operands
nesper
aho-corasick
incognito-mode
sqlbindparameter
xmi
mft
yii-booster
stty
ldif
acceptance-testing
void-pointers
notin
ui-design
reentrancy
oembed
jsplitpane
gprof
lynx
multiscaleimage
pageheap
mvcrecaptcha
motodev-studio
net-use
canonicalization
libxslt
ccnet-config
fgetc
rawcontacts

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App