java.lang.RuntimeException: Could not launch activity - android

In my test I need to start activity as singleton. So I use flag FLAG_ACTIVITY_SINGLE_TOP
Here my Espresso's test:
#RunWith(AndroidJUnit4::class)
class TradersActivityTest {
private var intent = Intent()
#Rule
#JvmField
var tradersIntentTestRule = IntentsTestRule(TradersActivity::class.java, false, false)
#Before
fun setup() {
mockServer = MockWebServer()
mockServer.start(8081)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
}
#Test
fun network_clientError_showToast() {
mockServer.enqueue(MockResponse()
.setResponseCode(400))
tradersIntentTestRule.launchActivity(intent) // error here
onView(withText(R.string.client_error)).inRoot(ToastMatcher()).check(matches(isDisplayed()))
}
But when I start test network_clientError_showToast I get error:
java.lang.RuntimeException: Could not launch activity
at androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:473)
at androidx.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:358)
at com.myproject.activity.radersActivityTest.network_clientError_showToast(TradersActivityTest.kt:112)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:672)
at android.app.ContextImpl.startActivity(ContextImpl.java:659)
at android.app.Instrumentation.startActivitySync(Instrumentation.java:405)
at androidx.test.runner.MonitoringInstrumentation.access$201(MonitoringInstrumentation.java:99)
at androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:449)
at androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:446)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

You're starting your activity outside of an activity context, so you have to use FLAG_ACTIVITY_NEW_TASK along with your other flags:
#Before
fun setup() {
mockServer = MockWebServer()
mockServer.start(8081)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK)
}

Related

RecyclerView Adapter instrumentation test with ViewBinding

I have a RecyclerView Adapter with multiple ViewHolders. Each ViewHolder uses a ViewBinding Class to inflate the view. I am trying to write some test for the adapter class. Here is my test class.
#RunWith(AndroidJUnit4ClassRunner::class)
class HomeTest {
private lateinit var adapter: Adapterr
#Before
fun setup() {
adapter = Adapterr()
}
#Test
fun createViewHolder() {
adapter.updateAdapter(getDummyAdapterItems())
//Error happens on this line.
val p = RecyclerView(InstrumentationRegistry.getInstrumentation().context)
p.adapter = adapter
val viewHolder = adapter.onCreateViewHolder(p, 0)
assertThat(viewHolder, notNullValue())
}
I get the following error.
java.lang.NoSuchMethodException: android.view.View.retrieveExplicitStyle [class android.content.res.Resources$Theme, interface android.util.AttributeSet]
at java.lang.Class.getMethod(Class.java:2072)
at java.lang.Class.getDeclaredMethod(Class.java:2050)
at io.mockk.proxy.android.MethodDescriptor.getMethod(MethodDescriptor.kt:22)
at io.mockk.proxy.android.advice.Advice.getOrigin(Advice.kt:37)
at java.lang.reflect.Method.invoke(Native Method)
at io.mockk.proxy.android.AndroidMockKDispatcher.getOrigin(AndroidMockKDispatcher.java:117)
at android.view.View.retrieveExplicitStyle(Unknown Source:14)
at android.view.View.<init>(View.java:5474)
at android.view.ViewGroup.<init>(ViewGroup.java:697)
at android.view.ViewGroup.<init>(ViewGroup.java:693)
at androidx.recyclerview.widget.RecyclerView.<init>(RecyclerView.java:654)
at androidx.recyclerview.widget.RecyclerView.<init>(RecyclerView.java:650)
at androidx.recyclerview.widget.RecyclerView.<init>(RecyclerView.java:646)
at com.example.home.HomeTest.createViewHolder(HomeTest.kt:57)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2205)
I even tried running it with a MockViewGroup Object, but I get the same error.

JUnit assert that an Activity is implementation of an interface

I have an Android Activity that implements an delegate interface which is called IMeetingRoomDelegate
interface IMeetingRoomDelegate {
fun onMeetingRoomFragmentClicked(homeFragment: MeetingRoomHomeFragment, meetingRoom: ParcelableMeetingRoomData)
}
I want to always assure that my Activity is always an implementation of IMeetingRoomDelegate.
How would I check this in Kotlin? I so far have this:
#RunWith(AndroidJUnit4::class)
class GivenTheMainActivityIsLoaded {
#get:Rule
val activityRule = ActivityTestRule<MainActivity>(MainActivity::class.java)
private lateinit var mainActivity: MainActivity
#Before
fun setup() {
this.mainActivity = activityRule.activity
}
#Test
fun thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent() {
assertTrue(implementsInterface(this.mainActivity::class.java))
}
private fun implementsInterface(interf: Class<*>): Boolean {
return interf is IMeetingRoomDelegate
}
}
I've tried seeing what the issue is myself but the test runner is barely giving anything substantial away.
The error
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at activities.GivenTheMainActivityIsLoaded.thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent(MainActivitySpec.kt:54)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
Tests ran to completion.
You could use the reified generics instead of asserting the reflections:
#Test
fun thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent() {
assertTrue(this.mainActivity.implementsInterface())
}
private inline fun <reified T> T.implementsInterface(): Boolean {
return this is IMeetingRoomDelegate
}

Android unit testing if an activity starts with a timeout or after a delay is not working

I am developing an Android application using Kotlin programming language. I am adding instrumented tests into my project. Now, I am having a problem with testing if an activity is started after a delay. I am using Espresso for instrumented tests.
This is my activity class
class MainActivity : AppCompatActivity() {
companion object {
val LAUNCH_DELAY: Long = 2000
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Handler().postDelayed({
this.startLoginActivity()
}, LAUNCH_DELAY)
}
protected fun startLoginActivity()
{
startActivity(Intent(this, LoginActivity::class.java))
}
}
I tried this
#RunWith(AndroidJUnit4::class)
class MainActivityTest {
#Rule #JvmField
val mainActivityRule: ActivityTestRule<MainActivity> = ActivityTestRule<MainActivity>(MainActivity::class.java)
#Test
fun itStartsLoginActivityAfterDelay() {
Intents.init()
mainActivityRule.launchActivity(Intent())
Handler().postDelayed({
Intents.intended(hasComponent(LoginActivity::class.java.name))
Intents.release()
}, 2000);
}
}
I got this error
E/TestRunner: failed: itStartsLoginActivityAfterDelay(com.example.memento.MainActivityTest)
----- begin exception -----
E/TestRunner: java.lang.RuntimeException: Can't create handler inside thread Thread[Instr: com.example.memento.MockTestRunner,5,main] that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:205)
at android.os.Handler.<init>(Handler.java:118)
at com.example.memento.MainActivityTest.itStartsLoginActivityAfterDelay(MainActivityTest.kt:37)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
----- end exception -----
I also tried this
#Test
fun itStartsLoginActivityAfterDelay() {
Intents.init()
mainActivityRule.launchActivity(Intent())
Thread.sleep(2000);
Intents.intended(hasComponent(LoginActivity::class.java.name))
Intents.release()
}
This time I got this error
E/TestRunner: failed: itStartsLoginActivityAfterDelay(com.example.memento.MainActivityTest)
E/TestRunner: ----- begin exception -----
E/TestRunner: androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 2 intents.
IntentMatcher: has component: has component with: class name: is "com.example.memento.LoginActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String
Matched intents:
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
Recorded intents:
-Intent { flg=0x10000000 cmp=com.example.memento/.MainActivity } handling packages:[[com.example.memento]])
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1538)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:94)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:57)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:318)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:300)
at androidx.test.espresso.intent.Intents.intended(Intents.java:189)
at androidx.test.espresso.intent.Intents.intended(Intents.java:170)
at com.example.memento.MainActivityTest.itStartsLoginActivityAfterDelay(MainActivityTest.kt:47)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 2 intents.
IntentMatcher: has component: has component with: class name: is "com.example.memento.LoginActivity" package name: an instance of java.lang.String short class nam
----- end exception -----
I/TestRunner: finished: itStartsLoginActivityAfterDelay(com.example.memento.MainActivityTest)
I/MonitoringInstr: Activities that are still in CREATED to STOPPED: 4
Finishing activity: com.example.memento.LoginActivity#97c0733
I/MonitoringInstr: Finishing activity: com.example.memento.LoginActivity#65a941b
I/MonitoringInstr: Finishing activity: com.example.memento.MainActivity#39cc00e
androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 2 intents.
IntentMatcher: has component: has component with: class name: is "com.example.memento.LoginActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String
Matched intents:
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
Recorded intents:
-Intent { flg=0x10000000 cmp=com.example.memento/.MainActivity } handling packages:[[com.example.memento]])
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1538)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:94)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:57)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:318)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:300)
at androidx.test.espresso.intent.Intents.intended(Intents.java:189)
at androidx.test.espresso.intent.Intents.intended(Intents.java:170)
at com.example.memento.MainActivityTest.itStartsLoginActivityAfterDelay(MainActivityTest.kt:47)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:388)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 2 intents.
IntentMatcher: has component: has component with: class name: is "com.example.memento.LoginActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String
Matched intents:
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
Recorded intents:
-Intent { flg=0x10000000 cmp=com.example.memento/.MainActivity } handling packages:[[com.example.memento]])
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
-Intent { cmp=com.example.memento/.LoginActivity } handling packages:[[com.example.memento]])
at junit.framework.Assert.fail(Assert.java:50)
at androidx.test.espresso.intent.VerificationModes$Times.verify(VerificationModes.java:80)
at androidx.test.espresso.intent.Intents.internalIntended(Intents.java:346)
at androidx.test.espresso.intent.Intents$2.check(Intents.java:193)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:419)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:282)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:268)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
That is not working as well. How can I test it?
The problem is that you have multiple matches for the target intent: Wanted to match 1 intents. Actually matched 2 intents
What you can do is to modify your matcher code like this:
intended(hasComponent(LoginActivity::class.java.name), Intents.times(2))
UPDATE:
The actual problem is in your test.
You call mainActivityRule.launchActivity(Intent()) in your test method and that line starts another MainAcivity which launches another LoginActivity for you (that's why you have two in your intent stack).
You don't have to start the activity from your test because the mainActivityRule: ActivityTestRule<MainActivity> you defined in your class does that for each of your test method.
This should work:
#Test
fun itStartsLoginActivityAfterDelay() {
Intents.init()
Thread.sleep(2000);
Intents.intended(hasComponent(LoginActivity::class.java.name))
Intents.release()
}

Android Rest API Automation Test

I have done API testing as sample application as shown in bellow code with using wiremock. but i have get numberFormat exception and invalide int as shown in bellow this error please give me any suggestion
#RunWith(AndroidJUnit4.class)
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public MainActivityTest()
{
super(MainActivity.class);
}
#Override
#Before
public void setUp() throws Exception
{
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
getActivity();
}
private static void stubServerCall() throws Exception
{
//WireMock.configureFor(Integer.parseInt(serverURL));
WireMock.configureFor(Integer.parseInt("http://androidexample.com/media/webservice/JsonReturn.php"));
stubFor(get(urlEqualTo("/tree/getLocalePack"))
.willReturn(aResponse()
.withStatus(HttpStatus.ORDINAL_200_OK)
.withHeader("Content-Type", "text/xml")
.withBody("<response>This is mock response</response>")));
}
#Test
public void testSendRequestEspresso() throws Exception
{
stubServerCall();
onView(withId(R.id.returnStringLbl)).check(matches(withText("Nothing yet returned")));
onView(withId(R.id.sendRequest)).perform(click());
}
}
java.lang.NumberFormatException: Invalid int: "http://androidexample.com/media/webservice/JsonReturn.php"
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parse(Integer.java:374)
at java.lang.Integer.parseInt(Integer.java:365)
at java.lang.Integer.parseInt(Integer.java:331)
at com.example.prashant.prashant.MainActivityTest.testSendRequestEspresso(MainActivityTest.java:80)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1710)
Tests ran to completion.

android UI Test with Espresso java.lang.NoClassDefFoundError:

I do UITest with Espresso, and test login function in LoginActivity.java .
when running LoginScreenTest.java ,some exception got,the follow is exception message. I don't konw why LoginPresenter can be found but LoadIdlingResource cannot be found ,why and how to do.thanks very much!!!
the following is exception message.
java.lang.NoClassDefFoundError: bz/sunlight/soubei/util/LoadIdlingResource
atbz.sunlight.soubei.login.view.activity.LoginActivity.getCountingIdlingResource(LoginActivity.java:149)
at bz.sunlight.soubei.login.LoginScreenTest.setUp(LoginScreenTest.java:46)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1739)
Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
at dalvik.system.DexFile.defineClass(Native Method)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:211)
at dalvik.system.DexPathList.findClass(DexPathList.java:313)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:51)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
... 34 more
there are code :
#RunWith(AndroidJUnit4.class)
#LargeTest
public class LoginScreenTest {
public LoginPresenter loginPresenter;
#Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActiv`enter herecode `ity.class);
#Before
public void setUp() throws Exception {
loginPresenter = new LoginPresenter(mActivityRule.getActivity(), mActivityRule.getActivity());
Espresso.registerIdlingResources(mActivityRule.getActivity().getCountingIdlingResource());
}
}
public class LoginActivity extends BaseActivity {
private LoginPresenter loginPresenter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
loginPresenter = new LoginPresenter(this, this);
loginPresenter.showLastUserName();
}
#VisibleForTesting
public IdlingResource getCountingIdlingResource() {
LoadIdlingResource loadIdlingResource = new LoadIdlingResource(this);
return loadIdlingResource;
}
}

Categories

Resources