如何在Espresso测试时从Asset文件夹中读取json文件?

时间:2016-02-18 02:25:11

标签: android json android-espresso

我正在尝试基于.json文件创建一些假对象。所以我的项目的架构看起来像这样:

- MyProject
- app
---- src
-------- androidTest
------------ assets
---------------- FirstObject.json
---------------- SecondObject.json
------------ java
-------- main
-------- test

我正在使用Espresso进行测试,并在.json文件夹下有一些assets个文件。

我的测试类看起来像这样:

@RunWith(AndroidJUnit4.class)
public class LocatingActivityTest
{
    @Rule
    public ActivityTestRule<BookingActivity> mActivityTestRule = new ActivityTestRule<>(BookingActivity.class);

    private BookingActivity mBookingActivity;

    @Before
    public void setup()
    {
        mBookingActivity = mActivityTestRule.getActivity();
    }

    @Test
    public void viewsMustBeVisible()
    {
        onView(withId(R.id.fab_booking)).perform(click());
        onView(withId(R.id.booking_book_now)).check(matches(not(isEnabled())));

        mBookingActivity.setTestBooking(BookingTest.getStandardFlight(),
                MyServiceProvider.getServices1(mBookingActivity), // <= Problem
                true,
                MyServiceProvider.getServices2(mBookingActivity), // <= Problem
                MyServiceProvider.getServices3(mBookingActivity)); // <= Problem

        onView(withId(R.id.booking_book_now)).check(matches(isEnabled()));
        onView(withId(R.id.booking_book_now)).perform(click());

        onView(withId(R.id.text)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.sonarView)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.cancel_booking)).check(matches(isCompletelyDisplayed()));
    }
}

问题出在我的MyServiceProvider课程中:

public final class MyServiceProvider
{
    public static List<FlightType> getServices1(Context context)
    {
        try
        {
            InputStream inputStream = context.getAssets().open("FlightTypes.json");
            String jsonString = read(inputStream);
            Log.e("XXX", "getServices1() => " + jsonString);
            Type listType = new TypeToken<List<FlightType>>(){}.getType();
            List<FlightType> flightTypeList = new Gson().fromJson(jsonString, listType);

            return flightTypeList;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return null;
    }

    private static String read(InputStream inputStream) throws IOException
    {
        StringWriter writer = new StringWriter();
        IOUtils.copy(inputStream, writer, "UTF-8");
        return writer.toString();
    }
.
.
.
}

由于某些原因InputStream inputStream = context.getAssets().open("FlightTypes.json");无法打开json文件和异常抛出。这是我的日志:

02-17 21:00:58.984 5686-5706/com.xxx.xxx W/System.err: java.io.FileNotFoundException: FlightTypes.json
02-17 21:00:58.984 5686-5706/com.xxx.xxx W/System.err:     at android.content.res.AssetManager.openAsset(Native Method)
02-17 21:00:58.984 5686-5706/com.xxx.xxx W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:313)
02-17 21:00:58.984 5686-5706/com.xxx.xxx W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:287)
02-17 21:00:58.984 5686-5706/com.xxx.xxx W/System.err:     at com.xxx.xxx.utility.MyServiceProvider.getServices1(MyServiceProvider.java:31)

2 个答案:

答案 0 :(得分:14)

不要将活动作为上下文传递。而是通过 Dim strSQL As String = "SELECT containers FROM tbl_items_containers" Dim da As New MySqlDataAdapter(strSQL, SQLConnection) Dim cmd As New MySqlCommand Dim ds As New DataSet SQLConnection.ConnectionString = ServerString cmd.Connection = SQLConnection da.Fill(ds, "tbl_items_containers") With cb_container_type .DataSource = ds.Tables("tbl_items_containers") .DisplayMember = "containers" .ValueMember = "containers" .SelectedIndex = 0 End With ,看看是否有帮助。

答案 1 :(得分:-1)

我遇到了同样的问题,我发现了这个解决方法

        boolean thrown = false;
        try {
            AssetManager assetManager = mainActivity.getResources().getAssets();
            InputStream inputStream = assetManager.open("/my/rep/file_name");
        } catch (FileNotFoundException e) {
            thrown = true;
            e.printStackTrace();
        }
        assertFalse(thrown);