I am writing a unit test in my flutter app, seems I can't read json from assets in tests, but it works if I run app,

I use code below to read json as string :

var jsonString = await rootBundle.loadString("assets/student_list.json");

How can I access jsons assets in tests?


Solution 1: silvio.pereira

Add this line as the first to be executed in your test and it will work and load the assets:

TestWidgetsFlutterBinding.ensureInitialized();

It will initialise everything before running your code and the assets will be accessible at that point.