简体   繁体   English

带有 JUnit + 的单元测试用例(Android 中的 Robolectric 或 Mockito 或两者)

[英]Unit Test Cases with JUnit +(Robolectric or Mockito or both in Android)

This is first time I have to write unit test cases in Android.这是我第一次必须在 Android 中编写单元测试用例。

So I have searched lots of things.所以我搜索了很多东西。

  1. Robolectric framewordk - Runs on JVM Robolectric framewordk - 在 JVM 上运行
  2. Mockito Framwork - Mocking objects Mockito 框架-模拟对象

So I have some doubts in Robolectric & Mokito.所以我对 Robolectric 和 Mokito 有一些怀疑。

  1. Should I have to use Robolectric only with JUnit in Android app?我是否必须在 Android 应用程序中仅将 Robolectric 与 JUnit 一起使用?
  2. Should I have to use Mockito only with JUnit in Android app?我是否必须在 Android 应用程序中仅将 Mockito 与 JUnit 一起使用?
  3. Should I have to go with both framework?我应该同时使用这两个框架吗?
  4. What is the difference between Mockito & Robolectric? Mockito 和 Robolectric 有什么区别?

I have search for difference between Mokito & Robolectric but don't get any proper answer for that.我在寻找 Mokito 和 Robolectric 之间的区别,但没有得到任何正确的答案。

Please suggest.请建议。

They have slightly different usages and I tend to use both in my projects.它们的用法略有不同,我倾向于在我的项目中同时使用两者。

Mockito莫基托

is used for making mocks of your classes.用于模拟您的课程。

When you are testing a particular class you mock all of its dependencies with Mockito.当你测试一个特定的类时,你用 Mockito 模拟它的所有依赖项。

Where possible most of your tests should use mockito.在可能的情况下,您的大部分测试都应该使用 mockito。 To make this possible most people split their code up into MVP, etc where the business logic is separated from the View logic.为了使这成为可能,大多数人将他们的代码拆分为 MVP 等,其中业务逻辑与视图逻辑分离。 This way your business logic (Presenter) has no knowledge (or dependencies) on the Android library and has no need to have mocks of them.这样您的业务逻辑 (Presenter) 就没有关于 Android 库的知识(或依赖项),也不需要模拟它们。

Robolectric机器人电子

is a library which contains many mocks of Android classes.是一个包含许多Android类模拟的库。

The Robolectric test runner injects these 'shadow objects' in place of the actual Android classes when the tests are run.在运行测试时,Robolectric 测试运行器会注入这些“影子对象”来代替实际的 Android 类。 This is what allows the tests to run on the JVM without booting up an instance of Android.这就是允许测试在 JVM 上运行而无需启动 Android 实例的原因。

When using MVP your View layer tends to be implemented by the Activity/Fragment and this is where you can use Robolectric to mock these.使用 MVP 时,您的 View 层往往由 Activity/Fragment 实现,这就是您可以使用 Robolectric 来模拟这些的地方。

Notes笔记

Use Robolectric only where necessary.仅在必要时使用 Robolectric。 It basically re-implements parts of the Android framework but not always in exactly the same way.它基本上重新实现了 Android 框架的一部分,但并不总是以完全相同的方式实现。

You may also need another library such as PowerMock.您可能还需要另一个库,例如 PowerMock。 This allows the mocking of static classes such as Math or can be used to mock static Android classes such as TextUtils.这允许模拟静态类(如 Math)或可用于模拟静态 Android 类(如 TextUtils)。

Both are used with JUnit两者都与 JUnit 一起使用

Mockito alone can cover most cases. Mockito 本身就可以涵盖大多数情况。

However, Robolectric can also provide limited operations on Android Component such as Activity or Fragment in Unit Test (not instrumentation test, which has no dependency on Android SDK), which does not require any emulator or devices and is considerably faster than instrumentation tests.但是,Robolectric 也可以在单元测试(不是仪器测试,它不依赖于 Android SDK)上提供对 Android 组件的有限操作,例如 Activity 或 Fragment,它不需要任何模拟器或设备,并且比仪器测试要快得多。

My suggestion: use Mockito for unit test and Espresso for UI test since they are semi-official test frameworks for Android.我的建议:使用 Mockito 进行单元测试,使用 Espresso 进行 UI 测试,因为它们是 Android 的半官方测试框架。

Add Robolectric in your Unit Test if there are some dependencies on Android SDK.如果对 Android SDK 有一些依赖,请在单元测试中添加 Robolectric。

First of all we need to understand that Roboelectric and Mockito are two different tools commonly used in android Test Driven Development.首先我们需要了解Roboelectric和Mockito是android测试驱动开发中常用的两种不同的工具。 So mostly you will find both of the tools in a same project.所以大多数情况下,您会在同一个项目中找到这两种工具。

Below I am explaining the common use cases for both-下面我将解释两者的常见用例-

Mockito is used for mocking the dependency which means if you want to access an real object in test environment then you need to fake it or we can say mock it. Mockito用于模拟依赖,这意味着如果你想在测试环境中访问一个真实的对象,那么你需要伪造它,或者我们可以说模拟它。 Now a days it is very easier to do mocking of the objects with Mockito.现在,使用 Mockito 对对象进行模拟变得非常容易。

Roboelectric is the industry-standard unit testing framework for Android. Roboelectric是 Android 的行业标准单元测试框架。 With Robolectric, your tests run in a simulated Android environment inside a JVM, without the overhead of an emulator.使用 Robolectric,您的测试可以在 JVM 内的模拟 Android 环境中运行,而没有模拟器的开销。 Simple test written using roboelectric is使用机器人编写的简单测试是

`@RunWith(AndroidJUnit4.class)
public class MyActivityTest {
@Test
public void clickingButton_shouldChangeResultsViewText() throws Exception {
Activity activity = Robolectric.setupActivity(MyActivity.class);

Button button = (Button) activity.findViewById(R.id.press_me_button);
TextView results = (TextView) activity.findViewById(R.id.results_text_view);

button.performClick();
assertThat(results.getText().toString(), equalTo("Testing Android Rocks!"));
}
}`

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM