2012-04-18 55 views
3
public class LoginActivityTest extends ActivityInstrumentationTestCase2<Login> { 


     Login mActivity; 
     private EditText username; 
     private EditText password; 

      @SuppressWarnings("deprecation") 
     public LoginActivityTest() 
      { 
       super("com.main.account.Login",Login.class); 
      } 


      @Override 
     protected void setUp() throws Exception { 
       super.setUp(); 
       mActivity = this.getActivity(); 

       username = (EditText) mActivity.findViewById(R.id.username); 
       password = (EditText) mActivity.findViewById(R.id.password); 
         } 

      public void testPreconditions() { 
       assertNotNull(username); 
       assertNotNull(password); 
      } 


      public void testText() { 
       assertEquals("hello",username.getText()); 
       assertEquals("hello123", password.getText()); 
      } 

} 

錯誤我得到的是這樣的:EditTextView的單元測試。我該怎麼做?

04-18 16:03:47.132: I/TestRunner(12376): junit.framework.AssertionFailedError: expected:<hello> but was:<> 
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.fail(Assert.java:47) 
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.failNotEquals(Assert.java:282) 
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.assertEquals(Assert.java:64) 
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.assertEquals(Assert.java:71) 
04-18 16:03:47.132: I/TestRunner(12376): at com.crumbs.main.test.LoginActivityTest.testText(LoginActivityTest.java:46) 
04-18 16:03:47.132: I/TestRunner(12376): at java.lang.reflect.Method.invokeNative(Native Method) 
04-18 16:03:47.132: I/TestRunner(12376): at java.lang.reflect.Method.invoke(Method.java:507) 
04-18 16:03:47.132: I/TestRunner(12376): at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204) 
04-18 16:03:47.132: I/TestRunner(12376): at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194) 
04-18 16:03:47.132: I/TestRunner(12376): at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186) 
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestCase.runBare(TestCase.java:127) 
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestResult$1.protect(TestResult.java:106) 
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestResult.runProtected(TestResult.java:124) 
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestResult.run(TestResult.java:109) 
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestCase.run(TestCase.java:118) 
04-18 16:03:47.132: I/TestRunner(12376): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) 
04-18 16:03:47.132: I/TestRunner(12376): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) 
04-18 16:03:47.132: I/TestRunner(12376): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529) 
04-18 16:03:47.132: I/TestRunner(12376): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448) 

如何測試它呢?如在,如何設置EditText的值,以便我可以在這裏測試它?

該文檔有點分散。

回答

1

它工作得很好。其只是說你的測試用例失敗,因爲在的EditText文本不等於你的文字比較.. 嘗試這種/ ..

 @Override 
      protected void setUp() throws Exception { 
        super.setUp(); 
        mActivity = this.getActivity(); 

        username = (EditText) mActivity.findViewById(R.id.username); 
        password = (EditText) mActivity.findViewById(R.id.password); 


          } 

       public void testPreconditions() { 
        assertNotNull(username); 
        assertNotNull(password); 
       } 


       public void testText() { 
//now that it will pass the test case without Exception.. 
//but if you want to compare text then you may have to set it in xml and then compare it.. doing it in onCreate() of Login may also work.. 
        assertEquals("",username.getText()); 
        assertEquals("", password.getText()); 
       } 
+0

我收到此錯誤:04-18 16:35:26.632:I/TestRunner(13709):android.view.ViewRoot $ CalledFromWrongThreadException:只有創建視圖層次結構的原始線程可以觸及其視圖。 – Hick 2012-04-18 11:06:35

+0

@Hiccup ..好的...我會編輯我的帖子.. – ngesh 2012-04-18 11:07:31

+0

這並不能解決問題。 – Hick 2012-04-18 11:18:53

6

How do I test it then? As in, how do I set the values for the EditText so that I can test it here?

它沒有多大意義,僞測試EditText.setText()方法,但是,如果你堅持,做這樣的事情:

public void testText() { 
    // simulate user action to input some value into EditText: 
    final EditText mUsername = (EditText) mActivity.findViewById(R.id.username); 
    final EditText mPassword = (EditText) mActivity.findViewById(R.id.password); 
    mActivity.runOnUiThread(new Runnable() { 
    public void run() { 
     mUsername.setText("hello"); 
     mPassword.setText("hello123"); 
    } 
    }); 

    // Check if the EditText is properly set: 
    assertEquals("hello", mUsername.getText()); 
    assertEquals("hello123", mPassword.getText()); 
} 

希望這有助於。

0

您可以使用與TouchUtils相同的方法。下面是我做的:

用靜態方法創建一個TextUtil類,所以它的易於使用

package org.dashee.venus; 

import android.app.Activity; 
import android.widget.TextView; 

/** 
* Provide a Text utility where a view text can be changed 
* by running inside a thread 
*/ 
public class TextUtils 
{ 
    public static void setText(Activity activity, final TextView view, final String text) 
    { 
     activity.runOnUiThread(
       new Runnable() 
       { 
        @Override 
        public void run() 
        { 
         view.setText(text); 
        } 
       } 
     ); 
    } 
} 

然後就可以調用它的TestMethod的內部

public void testText() 
{ 
    TextView email = this.getActivity().findViewById(R.id.email); 
    TextUtil.setText(this.getActivity(), email, "[email protected]"); 
    assertEquals("[email protected]", email.getText()); 
} 

希望這有助於

1

這項技術在測試運行時能夠看到正在鍵入的測試字符串具有很大的好處。

此方法會將一個字符串寫入editText元素,其中password是您的測試字符串。它應該被添加到儀器測試類(它擴展ActivityInstrumentationTestCase2)

private void addPassword(final String password) { 
    final EditText pw = (EditText) activity.findViewById(R.id.someId); 
    // Send string input value 
    getInstrumentation().runOnMainSync(new Runnable() { 
     @Override 
     public void run() { 
      pw.requestFocus(); 
     } 
    }); 
    getInstrumentation().waitForIdleSync(); 
    getInstrumentation().sendStringSync(password); 
    getInstrumentation().waitForIdleSync(); 
} 

然後,您可以用

EditText pw = (EditText)findViewById(R.id.someId); 
    final String entry = pw.getText().toString().trim(); 

檢索文本並與

assertEquals(TEST_STRING, entry); 

希望這會比較它們是否相等幫助。這裏http://developer.android.com/training/activity-testing/activity-functional-testing.html

0

進一步的信息我知道這是一個很老的帖子,怎麼樣這個解決方案:

@Before 
public void init() { 
    MockitoAnnotations.initMocks(this); 

    when(rootView.findViewById(R.id.button_logout)).thenReturn(buttonLogout); 
    when(rootView.findViewById(R.id.button_unlock)).thenReturn(buttonUnlock); 
    when(rootView.findViewById(R.id.ScreenLock_PasswordTextField)).thenReturn(passwordField); 

    when(passwordField.getText()).thenReturn(Editable.Factory.getInstance().newEditable("asd")); 
    when(application.getPassword()).thenReturn("asd"); 

    sut = new ScreenLockPresenterImpl(application, rootView, screenLockListener, 
      logoutButtonClickListener); 
} 


@Test 
public void testOnClickWhenOk() { 
    sut.onClick(null); 

    verify(passwordField).getText(); 
    verify(screenLockListener).unLock(); 
} 

我認爲這是你在找什麼: 時(passwordField.getText())。 thenReturn(Editable.Factory.getInstance()newEditable( 「ASD」));