Unit Testing with Firebase

Here is my solution - hope this helps:

[Update] I've removed my prev. sample in favor of this one. It's simpler and shows the main essence

    public class TestFirebase extends AndroidTestCase {
        private static Logger logger = LoggerFactory.getLogger(TestFirebase.class);

        private CountDownLatch authSignal = null;
        private FirebaseAuth auth;

        @Override
        public void setUp() throws InterruptedException {
            authSignal = new CountDownLatch(1);
            Firebase.setAndroidContext(mContext); //initializeFireBase(context);

            auth = FirebaseAuth.getInstance();
            if(auth.getCurrentUser() == null) {
                auth.signInWithEmailAndPassword("[email protected]", "12345678").addOnCompleteListener(
                        new OnCompleteListener<AuthResult>() {

                            @Override
                            public void onComplete(@NonNull final Task<AuthResult> task) {

                                final AuthResult result = task.getResult();
                                final FirebaseUser user = result.getUser();
                                authSignal.countDown();
                            }
                        });
            } else {
                authSignal.countDown();
            }
            authSignal.await(10, TimeUnit.SECONDS);
        }

        @Override
        public void tearDown() throws Exception {
            super.tearDown();
            if(auth != null) {
                auth.signOut();
                auth = null;
            }
        }

        @Test
        public void testWrite() throws InterruptedException {
            final CountDownLatch writeSignal = new CountDownLatch(1);

            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("message");

            myRef.setValue("Do you have data? You'll love Firebase. - 3")
                    .addOnCompleteListener(new OnCompleteListener<Void>() {

                        @Override
                        public void onComplete(@NonNull final Task<Void> task) {
                            writeSignal.countDown();
                        }
                    });

            writeSignal.await(10, TimeUnit.SECONDS);
        }
    }

UPDATE 2020: "So far this year (2020), this problem seems to have been solved using a (beta, at the date of this comment) Firebase emulator: Build unit tests using Fb Emulators and Unit testing security rules with the Firebase Emulator Suite, at YouTube This is done locally in the developer's computer. – carloswm85"

I found this https://www.firebase.com/blog/2015-04-24-end-to-end-testing-firebase-server.html but the article is over a year old. I only scanned it, I'll give it a more thorough read in a bit.

Either way, we really need the equivalent of the local Google AppEngine Backend that you can run in Intellij (Android Studio). Testing cannot be an afterthought in 2016. Really hoping one of the awesome Firebase devs notices this thread and comments. Testing should be part of their official guides.