How to build the robolectric environment

How to build the robolectric environment

[[153333]]

I've been learning test-driven development recently. Test-driven development is a method of extreme programming. It advocates writing test code according to requirements before writing real code (of course, this test code is impossible to pass), and then writing formal code based on the test code. The code written is to make the test code pass step by step. If test-driven development can also be used in Android development, it will greatly speed up our development. But we all know that Android's unit testing is really a headache. Each of its tests requires running on a real device or simulator, which will greatly slow down our testing speed.

Robolectric is an Android unit testing framework. The charm of this framework is that it allows your Android code to run on the jvm (not the Android virtual machine), which means that when you do unit testing, you don't need to install your apk on the machine to run it, which will greatly reduce our testing time. (Wouldn't it be nice to run your Android tests directly from inside your IDE?)

robolectric official website

I encountered many difficulties in the process of building robolectric, and the solutions on the Internet were relatively limited, so I had to explore it myself. Today I will record the construction of the robolectric environment here.

1. Development environment android-studio

2. Required jar package, download address

3. After importing the jar package, you need to modify the project configuration: There is a build variant in the lower left corner of Android studio. Click it and change the test artifact to Unit tests.

4. Write test code:

  1. @RunWith (RobolectricGradleTestRunner. class )
  2. @Config (constants = BuildConfig. class )
  3. public   class MyTest {
  4. @Test  
  5. public   void testTest() {
  6. MainActivity mainActivity = Robolectric.setupActivity(MainActivity. class );
  7. TextView textView = (TextView) mainActivity.findViewById(R.id.text);
  8. assertEquals(textView.getText(), "Hello World!1" );
  9. }
  10. }

Note the annotations on the test class here

5. Run this unit test method. When it runs, it may download some things. After it finishes downloading, it will be executed like a normal junit.

<<:  Why I don't like working at a mainstream tech company

>>:  Which Google products have more than 1 billion users?

Recommend

Human figure drawing: character creation from scratch

Human body painting: Character creation from scra...

Doctors warn: This change in nails may be cancer! Don't take it lightly

When you cut your nails, have you ever noticed th...

The three laws of traffic products

Why can Waipojia’s Mapo Tofu be sold for as low a...

What are the examples and methods of APP promotion plans?

In recent years, mobile Internet has developed ra...

With customer acquisition as the goal, how to achieve user growth from 0 to 1?

If the goal is to acquire customers, then you nee...

How to create a Douyin IP expert and share the tips on how to gain fans!

Everyone is familiar with the word "Tik Tok&...

Xiaomi enters JD.com, can compromise reverse its decline?

Fan Bingbing and Li Chen announced their relation...

105-year-old IBM is overwhelmed by reform. Can artificial intelligence save it?

Some companies embrace reform, while others are o...

LeEco announcement interpretation: the license issue will be completely resolved

From the closure letter in June to the talks with...

Block Tesla and see who are the competitors of Model 3

Tesla's recently released Model 3 entry-level...

How much does it cost to develop a mobile game?

Nowadays, most people like to play a few mobile g...