2017-04-05 85 views
1

我想的JUnit測試這個類:的JUnit測試與火力

public class WeekListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { 

    private ArrayList<String> weekList = new ArrayList<>(); 
    private ArrayAdapter<String> adapter; 


    ListView weekListView; 
    Button AddWeekButton; 
    EditText InsertWeekEditText; 
    String weekNumber; 
    String subjectName; 
    String subjectCode; 
    User user; 

    DatabaseReference mDatabase; 
    DatabaseReference mRef; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_week_list); 
     FirebaseApp.initializeApp(this); 

     FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); 
     FirebaseUser firebaseUser = firebaseAuth.getCurrentUser(); 

     Intent moveToDetailIntent = this.getIntent(); 
     subjectName = moveToDetailIntent.getExtras().getString("Subject"); 
     subjectCode = moveToDetailIntent.getExtras().getString("Fagkode"); 

     mDatabase = FirebaseDatabase.getInstance().getReference().child("Studentfag").child(subjectCode).child("Week"); 
     mRef = FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseUser.getUid()).child("User info"); 

     weekListView = (ListView) findViewById(R.id.WeekListView); 
     AddWeekButton = (Button) findViewById(R.id.AddWeekButton); 
     InsertWeekEditText = (EditText) findViewById(R.id.InsertWeek); 

     String userID = firebaseUser.getUid(); 
     mRef.addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       user = dataSnapshot.getValue(User.class); 
       if (user.isStudent){ 
        View a = weekListView; 
        a.setMinimumHeight(80); 
        View b = AddWeekButton; 
        b.setVisibility(View.GONE); 
        View c = InsertWeekEditText; 
        c.setVisibility(View.GONE); 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 

     adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, weekList); 
     weekListView.setAdapter(adapter); 
     weekListView.setOnItemClickListener(this); 
     AddWeekButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       weekNumber= InsertWeekEditText.getText().toString(); 
       mDatabase.child(weekNumber).child("id").setValue(weekNumber); 
      } 
     }); 

     mDatabase.addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
       String week = dataSnapshot.getKey().toString(); 
        weekList.add("Week: " + week); 
        adapter.notifyDataSetChanged(); 
       } 

      @Override 
      public void onChildRemoved(DataSnapshot dataSnapshot) { 

      } 

      //Urelevante metoder for oss. 
      @Override 
      public void onChildChanged(DataSnapshot dataSnapshot, String s) {} 
      @Override 
      public void onChildMoved(DataSnapshot dataSnapshot, String s) {} 
      @Override 
      public void onCancelled(DatabaseError databaseError) {} 
     }); 

    } 

的問題是,當我在我的設置方法,建立一個新的活動,報告說因爲這句話:

mRef = FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseUser.getUid()).child("User info"); 

無法構建,當我們沒有Firebase用戶時。 因此我試圖在我的測試類中模擬一個firebaseuser。問題是,我怎麼能告訴班級它應該使用onCreate中的模擬firebaseuser?有沒有辦法「發送」嘲弄的對象?謝謝!

我的設置方法的開頭:

@Before 
public void setUp() throws Exception { 

    Intent i = new Intent(); 
    i.putExtra("Subject", "Matematikk 1"); 
    i.putExtra("Fagkode", "TMA4100"); 

    FirebaseUser mockFirebaseUser = mock(FirebaseUser.class); 
    when(mockFirebaseUser.getUid()).thenReturn("uTZpVPPz8NT2LOvP4ufjs1L6r3P2"); 

    Activity activity = Robolectric.buildActivity(WeekListActivity.class).withIntent(i).create().get(); 

} 
+0

就我個人而言,我發現Robolectric不適用於Firebase API。我只能完全模擬所有的API,比如FirebaseDatabase,而沒有Robolectric直接調用FirebaseDatabase的幫助。 –

+0

另外我很好奇你爲什麼要調用FirebaseApp.initializeApp(this)。如果您將Firebase與gradle插件和google-services.json集成,則不必這樣做。 –

回答

0

像往常一樣,我建議大家不要混用演示和存儲代碼。這是另一個話題的問題。

這裏的訣竅是如何實現你想要的。 首先,Firebase初始化和提供FirebaseAuth提取方法:

@VisibleForTest 
@NonNull 
FirebaseAuth initAndReturnFirebaseAuth() { 
    FirebaseApp.initializeApp(this); 
    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); 
} 

其次,創建測試活動,並重寫此方法:

public class TestWeekListActivity extends WeekListActivity { 
    @Override 
    @NonNull 
    FirebaseAuth initAndReturnFirebaseAuth() { 
     FirebaseAuth authMock = mock(FirebaseAuth.class); 
     when(authMock.getCurrentUser()).thenReturn(mockFirebaseUser); 
     return authMock; 
    } 
} 

然後用測試活動測試,而不是你真正的活動。

希望它有幫助!