简体   繁体   English

是否可以使用 Mockito 运行 Espresso 测试(不带匕首)?

[英]Is running Espresso tests with Mockito possible (without dagger)?

Is it possible to use Espresso with Mockito?是否可以将 Espresso 与 Mockito 一起使用? I have an App where we call the API 2 times appart from authentication and we are trying to use Espresso with Mockito, to mock several methods close to UI.我有一个应用程序,我们从身份验证中调用 API 2 次,我们正在尝试将 Espresso 与 Mockito 一起使用,以模拟接近 UI 的几种方法。 There is no dagger or any other dependency injection tool in the project and also, no unit tests.项目中没有 dagger 或任何其他依赖注入工具,也没有单元测试。

I have already worked with Espresso on another, where we mocked the API with WebMockServer.我已经在另一个上使用 Espresso,我们使用 WebMockServer 模拟了 API。 The App had a lot of networking and this was great, we were able to test everything.该应用程序有很多网络,这很棒,我们能够测试一切。 Now, I do not get to mock a single thing.现在,我不会嘲笑任何一件事。

Espresso is a UI testing framework, Mockito is a mocking framework. Espresso是一个UI测试框架,Mockito是一个mocking框架。 They work fine together.他们一起工作得很好。

I think the question you're asking depends more on the difference between white box testing and black box testing .我认为您要问的问题更多地取决于白盒测试和黑盒测试之间的区别 In working with the application UI, Espresso is very capable of both white-box testing and black-box testing;在使用应用程序 UI 时,Espresso 非常有能力进行白盒测试和黑盒测试; you can write Espresso tests against an application without editing or accessing its internals (black box), or you can interact with the UI while being aware of the internals and reconfiguring them for testing purposes (white box).您可以针对应用程序编写 Espresso 测试,而无需编辑或访问其内部(黑框),或者您可以与 UI 交互同时了解内部并重新配置它们以进行测试(白框)。 By contrast, Mockito provides mock implementations of your classes and interfaces, so in order for you to test with a Mockito-written implementation you'd need to reconfigure your application to consume it as in a white-box test.相比之下,Mockito 提供了类和接口的模拟实现,因此为了让您使用 Mockito 编写的实现进行测试,您需要重新配置应用程序以像在白盒测试中一样使用它。

You've described your application as lacking dependency injection and unit tests, which suggests that your components might not have been written with the configurability or testing seams in mind.您已将您的应用程序描述为缺少依赖项注入和单元测试,这表明您的组件可能没有考虑到可配置性或测试接缝。 You would need a seam like that to allow replacing your implementation with WebMockServer or a Mockito-provided implementation.您将需要这样的接缝来允许用 WebMockServer 或 Mockito 提供的实现替换您的实现。

For a quick and hacky solution testing seam, you could simply make a public field on the Activity, Fragment, or View that you're trying to test, allowing you to replace that implementation from your test.对于一个快速和 hacky 的解决方案测试接缝,您可以简单地在您尝试测试的 Activity、Fragment 或 View 上创建一个公共字段,从而允许您从测试中替换该实现。 You would need to change your code to accept the test-double value in the field rather than creating its own implementation.您需要更改代码以接受该字段中的 test-double 值,而不是创建自己的实现。 For an Activity or Fragment, you could get to the field using your own implementation of ActivityScenario.ActivityAction and then performing that onActivity as in the sample ;对于 Activity 或 Fragment,您可以使用自己的ActivityScenario.ActivityAction实现进入现场,然后像示例中那样执行onActivity for views you could similarly write a ViewAction and then perform it onView the same way.对于视图,您可以类似地编写一个ViewAction ,然后以相同的方式在onView上执行它。 Replace the field with your own implementation and you're good to go.用您自己的实现替换该字段,您就可以使用 go。

Of course, there are hazards with this approach: From a technical standpoint, the Activity can be recreated at will, so your implementation replacement may be short-lived.当然,这种方法也存在隐患:从技术角度来看,Activity 可以随意重新创建,因此您的实现替换可能是短暂的。 Furthermore, this may look less clean, because you're violating encapsulation to some degree: Your components are now allowing anyone to edit one of their fields, which is correct, because that's exactly what your test would be doing.此外,这可能看起来不太干净,因为您在某种程度上违反了封装:您的组件现在允许任何人编辑他们的字段之一,这是正确的,因为这正是您的测试要做的。 This too is solvable: If the above hacky technique proves useful you might start abstracting away some of the view logic or activity logic to a replaceable implementation in a global singleton-style service, or from a field on the Application itself: in a broad sense, even without a formal framework, you'd be injecting a dependency .这也是可以解决的:如果上述 hacky 技术被证明是有用的,您可能会开始将一些视图逻辑或活动逻辑抽象为全局单例样式服务中的可替换实现,或者从应用程序本身的字段:广义上,即使没有正式的框架,您也会注入依赖项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中运行espresso测试时出现OutOfMemory问题 - OutOfMemory issue while running espresso tests in Android 在Dalvik运行时设备上运行Espresso仪器测试的问题 - Issues running Espresso instrument tests on Dalvik runtime devices 是否可以在 @BeforeClass 注释方法中运行 Android Espresso 单元测试? - Is it possible to run Android Espresso unit tests in @BeforeClass annotated methods? 使用Espresso for APK编写UI测试,无需源代码 - Writing UI tests using Espresso for APK without source code Gradld Build - 使用Mockito创建的模拟对象来运行单元测试 - Gradld Build - Use Mock Objects Created by Mockito for running unit tests JUnit参数化测试未与Mockito,Spring,Reflection和AspectJ隔离运行 - JUnit Parameterized tests not running in isolation with Mockito, Spring, Reflection and AspectJ 浓咖啡测试上的NoClassDefFoundError - NoClassDefFoundError on Espresso Tests 在没有 mvn 命令的情况下使用电源 mockito 验证 jacoco covarage 测试 - Verify jacoco covarage tests using power mockito without mvn command 没有注释的Mockito模拟如何在我的测试中起作用 - How come Mockito mocks worked in my tests without annotation Android单元使用Dagger 2进行测试 - Android Unit Tests with Dagger 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM