简体   繁体   English

在Android Studio JUnit Test中使用Firesbase进行测试

[英]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. Firebase在我的Android Studio中完全可以正常工作,但是当我尝试对其进行测试时会崩溃。

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. 发生这种情况是因为json文件不在正确的位置。 Redownload google-services.json file from Project Settings in the Firebase console and put it in the right place in your project. Firebase控制台的“项目设置”中重新下载google-services.json文件,并将其放在项目中的正确位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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