简体   繁体   中英

Testing with Firesbase in Android Studio JUnit Test

Firebase totally works fine in my Android Studio, but it crashes when I try to test it with it.

public class ExampleUnitTest {

    DatabaseReference rootReference = FirebaseDatabase.getInstance().getReference();
    DatabaseReference tournamentReference = rootReference.child("Tournaments");
    DatabaseReference matchReference = tournamentReference.child("Test Tournament").child("Matches");

    @Test
    public void addition_isCorrect() throws Exception {

        DatabaseReference testReference = matchReference.child("Nov 13, 2017 Team Eagles vs Team Tigers");

        testReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    assertEquals(snapshot.child("fieldName").getValue().toString(), "UIUC Sixpack Field");
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

In this test case, it gives me an error :

java.lang.ExceptionInInitializerError
    at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
    at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)

Is there a way to solve make it work?

This is happening because the json file does not exists in the right place. Redownload google-services.json file from Project Settings in the Firebase console and put it in the right place in your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM