'Flutter testing error: NotInitializedError which involves environment variables (.env)

To start, I did review this but to no avail. How to access Flutter environment variables from tests?

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:integration_test/integration_test.dart';
import 'package:main.dart' as app;

import 'package:mockito/annotations.dart';
import 'package:firebase_auth/firebase_auth.dart' as firebaseauth;

import 'package:flutter_dotenv/flutter_dotenv.dart';

import 'basic_app_test.mocks.dart';


@GenerateMocks([firebaseauth.FirebaseAuth])
void main() async {
  TestWidgetsFlutterBinding.ensureInitialized();
  await DotEnv().load(fileName: ".env");

  group('Login Page Tests', () {
    testWidgets('Empty Login Box', (WidgetTester tester) async {
      app.main();

      await tester.pumpAndSettle(Duration(seconds: 3));

      expect(find.byKey(Key('loginButton')), findsOneWidget);

      final Finder loginButton = find.byKey(Key('loginButton'));

      await tester.tap(loginButton);

      await tester.pumpAndSettle();

      expect(
          find.text(
              'Something went wrong. Please check your email and password and try again.'),
          findsOneWidget);
    });

    testWidgets('Email Login - Proper Credentials',
        (WidgetTester tester) async {

      app.main();

      // Wait for app to reach the login page
      await tester.pumpAndSettle(Duration(seconds: 3));

      final Finder emailBox = find.byKey(Key('emailBox'));
      final Finder passwordBox = find.byKey(Key('passwordBox'));
      final Finder loginButton = find.byKey(Key('loginButton'));
      final Finder welcomeHeader = find.byKey(Key('welcomeHeader'));

      var testPassword = DotEnv().env['TEST_PASSWORD'];
      await tester.enterText(passwordBox, testPassword);

      expect(find.byKey(Key('loginButton')), findsOneWidget);
      await tester.tap(loginButton);
      await tester.pumpAndSettle(Duration(seconds: 5));

      // SHOULD BE EXPECTING A WIDGET IN THE NEXT WINDOW
      expect(welcomeHeader, findsOneWidget);
    });
  });
}

I have a group of tests. The first one runs just fine. The second one runs into an error when the testPassword variable is created.

Here is the error:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following NotInitializedError was thrown running a test:
Instance of 'NotInitializedError'

When the exception was thrown, this was the stack:
#0      DotEnv.env (package:flutter_dotenv/src/dotenv.dart:42:7)
#1      main.<anonymous closure>.<anonymous closure> (file:///D:/WEBDEV/EndevStudios/MedicalApp/gshDevWork/medical-app-frontend/integration_test/basic_app_test.dart:52:31)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

The test description was:
  Email Login - Proper Credentials

The .env file is located at the root.

My pubspec.yaml file:

dependencies:
  flutter_dotenv: ^5.0.2

flutter:
    #Integration Testing
    - .env

What I've tried: Most solutions involve some variation of TestWidgetsFlutterBinding.ensureInitialized(); or IntegrationTestWidgetsFlutterBinding.ensureInitialized(); as well as await DotEnv().load(fileName: ".env"); and await DotEnv().load();.

Thank you in advance to anyone who can help.



Solution 1:[1]

You need to change from

await DotEnv().load(fileName: ".env");

to:

await dotenv.load(fileName: ".env");

using the latest library.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yusuf Adefolahan