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.
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.
Add the following line to Gradle.
dependencies {
testImplementation 'junit:junit:4.12'
}
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