2015-12-15 20 views
6

Tôi đang tạo và Ứng dụng Android. Tôi đang sử dụng Espresso để thử nghiệm.Gọi phương thức của Hoạt động đã kiểm tra từ thử nghiệm bằng cách sử dụng cà phê espresso và xem kết quả của nó bằng cách sử dụng máy pha cà phê và xem kết quả của nó

Tôi đã một Hoạt động với phương pháp:

public void render(Recipe recipe){ 
    //draw the recipe to the activity 
} 

Tôi muốn thử nghiệm rằng phương pháp này hoạt động chính xác.

Không giải pháp 1

làm việc tôi đã thử nghiệm như sau

@Test 
public void viewPaintsRecipes() { 
    final Activity activity = activityRule.launchActivity(new Intent()); 
    ((MainActivity)activity).render(Arrays.asList(new Recipe[]{recipe})); 
    onView(withId(R.id.text)).check(matches(withText(recipe.toString()))); 
} 

tôi nhận được một ngoại lệ.

Chỉ chuỗi ban đầu đã tạo phân cấp chế độ xem mới có thể chạm vào chế độ xem .

Không giải pháp 2

làm việc tôi cũng đã cố gắng để đưa hai dòng bên trong một Runnable runned bởi một Handler trong thread chính, nhưng bị treo thử nghiệm.

Làm cách nào tôi có thể tạo điều này?

Ghi chú

tôi đính kèm những thử nghiệm đầy đủ. Lưu ý rằng tôi cũng sử dụng dagger và Mockito.

@RunWith(AndroidJUnit4.class) 
@LargeTest 
public class MainActivityTestWithMockPresenter { 
    Recipe recipe = new Recipe("sampleTitle"); 
    @Rule 
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule(MainActivity.class, true, false); 

    @Mock 
    MainActivityPresenter mockPresenter; 
    @Mock 
    AndroidApplication mockContext; 

    @Before 
    public void insertMockedComponent(){ 
     MockitoAnnotations.initMocks(this); 
     Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 
     AndroidApplication app = (AndroidApplication) instrumentation.getTargetContext().getApplicationContext(); 
     MyModule mockedMyModule = new MyModule(mockContext){ 
      @Provides 
      public MainActivityPresenter getMainActivityPresenter(){ 
       return mockPresenter; 
      } 
     }; 
     MyComponent component = DaggerMyComponent.builder().myModule(mockedMyModule).build(); 
     app.setComponent(component); 
    } 

    @Test 
    public void viewPaintsRecipes() { 
     final Activity activity = activityRule.launchActivity(new Intent()); 
     ((MainActivity)activity).render(Arrays.asList(new Recipe[]{recipe})); 
     onView(withId(R.id.text)).check(matches(withText(recipe.toString()))); 
    } 
} 

Cảm ơn

+0

xóa chú thích @mock và cho biết cách chú thích sẽ hoạt động sau đó – piotrek1543

+0

kết hợp của bạn kiểm tra đơn vị ing với các bài kiểm tra tự động – piotrek1543

+0

@ piotrek1543 Tôi muốn sử dụng các mocks, bởi vì tôi chỉ muốn kiểm tra Hoạt động, không phải tất cả các lớp (Trình bày, các ca sử dụng, v.v.). nếu đây không phải là cách, làm cách nào tôi có thể thử nghiệm các phương thức Hoạt động theo cách được mã hóa? – Mateu

Trả lời

7

tôi đã quản lý để có được điều này để làm việc với những phụ thuộc vào build.gradle tập tin của tôi:

androidTestCompile 'com.android.support:support-annotations:23.1.0' 
androidTestCompile 'com.android.support.test:runner:0.4.1' 
androidTestCompile 'com.android.support.test:rules:0.4.1' 
androidTestCompile 'org.hamcrest:hamcrest-library:1.3' 
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' 

Bạn cần phải sử dụng phương pháp ActivityTestRule.runOnUiThread(@NotNull java.lang.Runnable runnable) như sau:

@Test 
public void testRecipeRender() throws Throwable { 
    // When. 
    activityTestRule.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      activityTestRule.getActivity().render(someRecipe); 
     } 
    }); 

    // Then. 
    // assert stuff about the recipe rendering here 
} 
Các vấn đề liên quan