A note about adding Junit 4 to Android Studio

Introduction

When you create a project in Android Studio these days, test folders etc. are created from the beginning, but it is not so with old projects. This article is a memo of Android Studio 3.1.2 because I added junit4 to an old project. Basically, I referred to Google's Document.

Folder structure

Assuming that the namespace is com.example.hoge, the folder structure is as follows at first. Folders that are not directly related to the contents of this time, such as res, are not written.

app/  └ src/    └ main/      └ java/         └ com/           └ example/               └ hoge/                 ┝ MainActivity.java

Add a test folder as follows. Prepare ʻExampleTest.java` as a test file.

app/  └ src/    ├ main/    │  └ java/    │     └ com/    │       └ example/    │           └ hoge/    │             ┝ MainActivity.java    │    ┝ test/       └ java/         └ com/           └ example/                └ hoge/                  ┝ ExampleTest.java

You should now see com.example.hoge (test) on your Android Studio Project.

Gradle added

Add the following line to Gradle.

dependencies {
    testImplementation 'junit:junit:4.12'
}

Test program execution

Write the following program in ʻExampleTest.java` above.

package com.example.hoge;

import org.junit.Test;
import static org.junit.Assert.assertTrue;

public class ExampleTest {

    @Test
    public void example1() {
        assertEquals("test", true, true);
//        assertEquals("test2", true, false);
    }

Once it compiles, you can run the test by right-clicking on com.example.hoge (test) in Android Studio. TODO I also try UI tests

Recommended Posts

A note about adding Junit 4 to Android Studio
I have a question about Android studio.
A note about Java GC
A note about the scope
A private note about AtomicReference
About adding a like function
I tried adding a separator line to TabLayout on Android
A note about RocksDB's Column Families
Android Development-Try to display a dialog-
I tried to create a simple map app in Android Studio
[Rails] I learned about migration files! (Adding a column to the table)
[Swift] A note about function and closure
Android Easily give a "press" to a button
Java to C and C to Java in Android Studio
How to use ExpandableListView in Android Studio
[Android] Inherit ImageView to create a new class
[Android] How to convert a character string to resourceId
3 ways to import the library in Android Studio
[Android] Implement a function to display passwords quickly
A story about trying to operate JAVA File
[Android] Two ways to get a Bluetooth Adapter
[Ruby] When adding a null constraint to a table
[Note] About the introduction to Swift practice Chapter 11
A note about the Rails and Vue process
[Note] About nil
[Android Studio] How to change TextView to any font [Java]
java: How to write a generic type list [Note]
[Android / Java] Set up a button to return to Fragment
A rough note about Ruby arrays and hash objects
I got a cannot resolve symbol in Android Studio
[Android Studio] [Java] How to fix the screen vertically
A story about trying to get along with Mockito
A note about th: field th: each th: object of thymeleaf
[Android Studio] I want to use Maven library on Android
[Note] Challenge to develop Android application for business use
[Java] Things to note about type inference extended in Java 10
A story about trying hard to decompile JAR files
A story about reducing memory consumption to 1/100 with find_in_batches
[Introduction to Android application development] Let's make a counter
How to create a Wear OS by Google app project on Android Studio 3.0 or higher