5

I am writing a Flutter integration test with a mock client that returns a JSON response for each of the REST endpoints my app calls. These JSON responses are stored in separate JSON files, but I am unable to access the files when the test is running.

I've tried loading the files by creating and reading a new file object. Flutter: how to load file for testing but it could never find the file. I also tried putting my JSON files into assets. This worked, but also resulted in the test JSON files being bundled when I built the APK.

Simplified Mock Client:

MockClient integrationMockClient = MockClient((request) async {
  switch (request.url.toString()) {
    case 'https://staging.company.com/api/123':
      return Response(readJsonfile('myJsonFile.json'), 200);

Simplified integration test main function - passes mock client in. test_driver/app.dart

void main() async {
  enableFlutterDriverExtension();
  final app = await initializeApp(
    integrationMockClient
  );
  runApp(app);
}

When I try and read a file it can never find it. Possible because flutterDriver runs the 'real app' with no access to files stored in test directories.

How can I access a JSON file from an integration test without it being bundled in production code/APK?

alichur
  • 925
  • 7
  • 19
  • 1
    Have you found the solution? – atereshkov May 23 '19 at 07:06
  • 1
    Sadly no. I implemented a dodgy workaround by storing the JSON in a dart string `const String successJson = '{"value": 5, "Name": "test"}'` – alichur May 23 '19 at 09:18
  • Haha, I did the same thing after unsuccessful research :D – atereshkov May 23 '19 at 13:42
  • 1
    Is there any workaround that came up in the last 4 month, I really on the JSON I'm loading in...? – patreu22 Sep 19 '19 at 14:52
  • 1
    The accepted answer at https://stackoverflow.com/questions/45780255/flutter-how-to-load-file-for-testing has an additional relevant answer below it, https://stackoverflow.com/a/57086750/6668797, which might address "*but it could never find the file*" unless you are more specific about your issue. – TWL Jan 08 '20 at 23:52
  • @TWL above method works for mobile & desktop but what about loading json file for flutter-web as 'dart:io' doesn't support browser to access disk system – Rajesh Patil Jan 03 '22 at 08:19

2 Answers2

1

Put JSON into String variable, like this. const String objectJson = """{JSON}""";

Wrap your JSON with a triple quotation mark """

and use it on integration tests, instead of reading it from a file

SardorbekR
  • 1,388
  • 3
  • 18
  • 33
0

I encountered similar issues accessing file resources using flutter driver for integration tests. What I did as a workaround was to parse the JSON response directly, instead of storing the JSON response as a file.

Here's a sample that you can try out. This uses https://jsonplaceholder.typicode.com as its endpoint sample.

test('Test http', () async {
    final file = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
  final json = jsonDecode(file.body.toString());
  print('json contents: $json');
  print('userId: ${json['userId']}');
  final userId = json['userId'];
  expect(userId, 1);
});
Omatt
  • 8,564
  • 2
  • 42
  • 144