AndroidStudio unit test and the pits encountered

The AndroidStudio version used is 2.2.2

1 TestCompile'junit:junit:4.12' has been added by default when creating a new project, if not, you can add it manually


2 Create any Java class for testing,


3 Right-click on the new Java class name ->Go To ->Test




Note: The following choice is to choose test instead of androidTest, we just want to test a Java class


4 After the above operations, we automatically generated a Test1Test class


Then we can add some test code


5 Click the Run button on the right side of the class -> Run'Test1Test' 


6 The expected value of the running result as shown in the figure above is 4 and the parameter is test1.add(2, 2); 2+2=4, so it is correct


If we modify it, as  shown in test1.add(3, 2); 4 != 3+2, an error will be reported



Pit encountered:

1 In this step, androidTest is selected

 

Then pop up when running


So be sure not to choose the wrong one. If you choose the wrong one, you will have this problem even if you delete it and choose test generation again.


2 When using the assertEquals method, there is no such method.


Then decided to import the package  import static org.junit.Assert.*; The result is that AndroidStudio will automatically delete it after pasting or hand typing (the reason is not known yet, I hope to inform)

Use Assert.assertEquals(4, test1.add(3, 2), 0); instead



Guess you like

Origin blog.csdn.net/u011288271/article/details/79985292