Tôi đang cố gắng tạo một số đối tượng giả dựa trên tệp .json. Vì vậy, kiến trúc của dự án của tôi trông như thế này:Cách đọc tệp json từ thư mục Nội dung khi thử nghiệm bằng Espresso?
- MyProject
- app
---- src
-------- androidTest
------------ assets
---------------- FirstObject.json
---------------- SecondObject.json
------------ java
-------- main
-------- test
tôi đang làm thử nghiệm với Espresso và có một số .json
file dưới assets
thư mục.
lớp thử nghiệm của tôi trông như thế này:
@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()));
}
}
Vấn đề là trong lớp MyServiceProvider
tôi:
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();
}
.
.
.
}
Đối với một số lý do InputStream inputStream = context.getAssets().open("FlightTypes.json");
không thể mở tệp json và ngoại lệ ném. Đây là nhật ký của tôi:
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)
Mọi đề xuất sẽ được đánh giá cao. Cảm ơn.
Bạn đang tuyệt vời ... Tôi đã suy nghĩ hoạt động của tôi có thể là vấn đề nhưng không biết thay thế của nó là gì. Cảm ơn... – Hesam