BB_Dev
BB_Dev

Reputation: 511

Robolectric Unit tests with AndroidAnnotations

I'm having problems with AndroidAnnotations unit tests using Robolectric.

I got my tests running. But generated class was unable to find any elements that should be included in the view. Here is my test class. While debugging I can see that "afterSetContentView_" was not able to findViewById.

@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {

private MainActivity activity;
private EditText mTextView;
private Button btnLogin;

 @Before
 public void setup() {
    activity = Robolectric.buildActivity(MainActivity.class).create().get();
    mTextView = (EditText) activity.findViewById(R.id.etUserName);
 }
}

Here is my activity:

@EActivity(R.layout.login)
public class MainActivity extends Activity {

@ViewById(R.id.etUserName)
EditText etUerName;

..........
}

Here I have updated with my test class

@RunWith(RobolectricTestRunner.class) 
@Config(manifest = "./src/main/AndroidManifest.xml", emulateSdk = 18)   

public class LoginActivityTest { 

@Before 
public void setup() {

 Robolectric.buildActivity(LoginActivity_.class).create().get(); 

 }
}

Upvotes: 1

Views: 505

Answers (2)

BB_Dev
BB_Dev

Reputation: 511

Finally i found the solution. I added below code into my test class and now it works fine. Thanks!

@Config(manifest = "app/src/main/AndroidManifest.xml", emulateSdk = 18, reportSdk = 18)
public class LoginActivityTest {

@Before
public void setup() {
Robolectric.buildActivity(LoginActivity_.class).create().get();
    usrName = (EditText) activity.findViewById(R.id.etUserName);
    password = (EditText) activity.findViewById(R.id.etPassword);

    usrName.setText("[email protected]");
    password.setText("volum3");

}
}

Upvotes: 1

nenick
nenick

Reputation: 7438

You should use the generated classes (MainActivity_) when you want to start an activity with robolectric.

Robolectric.buildActivity(MainActivity_.class).create().get();

Upvotes: 1

Related Questions