Issue
I am new to Unit Testing.
I was trying to create a folder using a Unit Test
@Test
public void testGenerateFile() throws IOException {
Context context = ApplicationProvider.getApplicationContext();
File file = new File(context.getFilesDir(),"myfile");
file.createNewFile();
byte[] data1={1,1,0,0};
//write the bytes in file
if(file.exists())
{
OutputStream fo = new FileOutputStream(file);
fo.write(data1);
fo.close();
System.out.println("file created: "+file);
}
assertTrue(true);
}
I also try to get the context from
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
But the file that was generated is in the folder
file created: /var/folders/yh/mkknvc7n2qx2k4swl_mtj7lw0000gn/T/robolectric-Method_testGenerateFile9017115247567873084/org.robolectric.default-dataDir/files/myfile
This is a folder generated for robolectric instrumented test. How can I get the folder to the real emulator or android device?
Solution
With the help of @michpohl
import android.content.Context;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withSubstring;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.Matchers.containsString;
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Rule
public ActivityTestRule<ActionBarDrawerActivity> mActivityRule = new ActivityTestRule<>(
ActionBarDrawerActivity.class, false, true);
@Test
public void navigateHelloWorld() throws Exception {
onView(withId(R.id.tv_gta_info)).check(matches(withText(containsString("Defesa"))));
onView(withId(R.id.tv_gta_info)).check(matches(withSubstring("Defesa")));
onView(withId(R.id.btn_servicos_animal)).perform(click());
assertTrue(true);
}
@Test
public void checkFileCreation() throws Exception {
Context context = mActivityRule.getActivity().getApplication().getBaseContext();
File file = new File(context.getFilesDir(), "myfile.txt");
file.createNewFile();
byte[] data1={1,1,0,0};
//write the bytes in file
if(file.exists())
{
OutputStream fo = new FileOutputStream(file);
fo.write(data1);
fo.close();
System.out.println("file created: "+file);
}
assertTrue(true);
}
This is my code that I use to create a file in the android emulator.
Don't forget to add these import on "build.gradle" file.
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/AL2.0'
exclude("META-INF/*.kotlin_module")
}
testOptions {
unitTests.returnDefaultValues = true
animationsDisabled = true
}
dependencies {
testImplementation 'junit:junit:4.13.1' // Required -- JUnit 4 framework
testImplementation 'androidx.test:core-ktx:1.3.0'
testImplementation 'androidx.test.ext:junit-ktx:1.1.2'
testImplementation 'org.robolectric:robolectric:4.4' // Robolectric environment
testImplementation 'androidx.test.ext:truth:1.3.0' // Optional -- truth
testImplementation 'com.google.truth:truth:1.0'
testImplementation 'org.mockito:mockito-core:3.3.3' // Optional -- Mockito framework
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3' // Optional -- Hamcrest library
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' // Optional -- UI testing with Espresso
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0' // Optional -- UI testing with UI Automator
}
Answered By - fabiobh