在模拟器或 android 设备文件夹中使用带有 Android 的单元测试创​​建文件

Create file with Unit Test with Android in the emulator or android device folder

我是单元测试的新手。

我正在尝试使用单元测试创​​建文件夹

@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);
}

我也尝试从

获取上下文
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

但是生成的文件在文件夹中

file created: /var/folders/yh/mkknvc7n2qx2k4swl_mtj7lw0000gn/T/robolectric-Method_testGenerateFile9017115247567873084/org.robolectric.default-dataDir/files/myfile

这是为 robolectric 仪器测试生成的文件夹。如何将文件夹获取到真正的模拟器或 android 设备?

在@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);
}

这是我用来在 android 模拟器中创建文件的代码。

不要忘记在“build.gradle”文件中添加这些导入。

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

}