| Start |
|---|
| Creating Local unit tests Place your test classes here: Example test classBreakdownThe test class, you can create several test classes and place them inside the test package. The test method, several test methods can be created inside a test class. Notice the annotation The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. There are several other useful annotations like These methods are member of the Annotation Information for JUnit Test: @Test: The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. @Before: When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @After: If you allocate external resources in a Before method you need to release them after the test runs. Annotating a public void method with Tip Quickly create test classes in Android Studio
Tip Easily execute tests in Android Studio
|
| Moving Business Logic Out of Android Componenets A lot of the value from local JVM unit tests comes from the way you design your application. You have to design it in such a way where you can decouple your business logic from your Android Components. Here is an example of such a way using the Model-View-Presenter pattern (opens new window). Lets practice this out by implementing a basic sign up screen that only takes a username and password. Our Android app is responsible for validating that the username the user supplies is not blank and that the password is at least eight characters long and contains at least one digit. If the username/password is valid we perform our sign up api call, otherwise we display an error message. Example where business logic is highly coupled with Android Component. Example where business logic is decoupled from Android Component. Here we define in a single class, LoginContract, that will house the various interactions between our various classes. Our LoginActivity is for the most part the same except that we have removed the responsibility of having to know how to validate a user's sign up form (our business logic). The LoginActivity will now rely on our new LoginPresenter to perform validation. Now your business logic will reside in your new LoginPresenter class. And now we can create local JVM unit tests against your new LoginPresenter class. As you can see, when we extracted our business logic out of the LoginActivity and placed it in the LoginPresenter POJO (opens new window). We can now create local JVM unit tests against our business logic. It should be noted that there are various other implications from our change in architecture such as we are close to adhering to each class having a single responsibility, additional classes, etc. These are just side effects of the way I choose to go about performing this decoupling via the MVP style. MVP is just one way to go about this but there are other alternatives that you may want to look at such as MVVM (opens new window). You just have to pick the best system that works for you. |
Getting started with JUnit SetupTo start unit testing your Android project using JUnit you need to add the JUnit dependency to your project and you need to create a test source-set which is going to contain the source code for the unit tests. Projects created with Android Studio often already include the JUnit dependency and the test source-set Add the following line to your module JUnit test classes are located in a special source-set named If your project doesn't have the If setup correctly your project structure (in the Android view in Android Studio) should look like this: Note: You don't necessarily need to have the Writing a test**Create a new class within the `test` source-set.** **Add the `@RunWith` annotation** **Create a test** Note: unlike the standard Java method naming convention unit test method names do often contain underscores. Running a test**Method** ](https://i.stack.imgur.com/BtkNl.png)
**Class** **Package (everything)** |
| Exceptions JUnit can also be used to test if a method throws a specific exception for a given input. In this example we will test if the following method really throws an exception if the Boolean format (input) is not recognized/unknown: By adding the This works well, however, it does limit you to just a single test case within the method. Sometimes you might want to test multiple cases within a single method. A technique often used to overcome this limitation is using Note: Some people consider it to be bad practice to test more than a single case inside a unit test. |
| Static import JUnit defines quite some To add a static import for the You can also static import all assert methods including the Without static import: With static import: Remarks
|
Read Article
https://develop.horje.com/learn/1434/reference