Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit and Android?

Tags:

android

junit

Is anyone using Junit and Android? Or is that just a worthy hope? Is there a tutorial anywhere?

like image 848
Peter vdL Avatar asked Mar 24 '11 05:03

Peter vdL


People also ask

What is JUnit testing in Android?

JUnit is a “Unit Testing” framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After, etc. Here we will be using only @Test annotation to keep the article easy to understand.

Can you use JUnit with Kotlin?

Open a Kotlin project in IntelliJ IDEA. If you don't already have a project, create one. Specify JUnit 5 as your test framework when creating your project. If you created the project using the New Project wizard, the task will be added automatically.

What is Mockito for Android?

Conclusion. Mockito is used to test final classes in Kotlin. Mockito is a widely used framework for mobile developers to write unit tests.

Why do we use Android JUnit runner?

The AndroidJUnitRunner class is a JUnit test runner that lets you run instrumented JUnit 4 tests on Android devices, including those using the Espresso, UI Automator, and Compose testing frameworks. Note: AndroidJUnitRunner is not needed for local tests.


2 Answers

Android has great support for JUnit 3

From Testing Fundamentals on Android Developers:

The Android testing framework, an integral part of the development environment, provides an architecture and powerful tools that help you test every aspect of your application at every level from unit to framework.

The testing framework has these key features:

  • Android test suites are based on JUnit. You can use plain JUnit to test a class that doesn't call the Android API, or Android's JUnit extensions to test Android components. If you're new to Android testing, you can start with general-purpose test case classes such as AndroidTestCase and then go on to use more sophisticated classes.
  • The Android JUnit extensions provide component-specific test case classes. These classes provide helper methods for creating mock objects and methods that help you control the lifecycle of a component.
  • Test suites are contained in test packages that are similar to main application packages, so you don't need to learn a new set of tools or techniques for designing and building tests.
  • The SDK tools for building and tests are available in Eclipse with ADT, and also in command-line form for use with other IDES. These tools get information from the project of the application under test and use this information to automatically create the build files, manifest file, and directory structure for the test package.
  • The SDK also provides monkeyrunner, an API testing devices with Python programs, and UI/Application Exerciser Monkey, a command-line tool for stress-testing UIs by sending pseudo-random events to a device.

This document describes the fundamentals of the Android testing framework, including the structure of tests, the APIs that you use to develop tests, and the tools that you use to run tests and view results. The document assumes you have a basic knowledge of Android application programming and JUnit testing methodology.

like image 129
Prime Avatar answered Sep 22 '22 00:09

Prime


I have been using Roboelectric which is awesome because it does not launch the simulator making the run time for the tests very very quick. A sample project is provided as an example and can be found here at github

like image 42
Captain Charmi Avatar answered Sep 22 '22 00:09

Captain Charmi