2013-11-19 2 views
2

Я использую Robolectric для тестирования класса LoginActivity. Я использую тесты ActionBarSherlock и Robolectric в строке getSupportActionBar(). Вот мой код и след.Тестирование устройств Android с помощью Robolectrics и ActionBarSherlock

LoginActivity.java

import org.json.JSONException; 
import org.json.JSONObject; 
import com.actionbarsherlock.app.SherlockActivity; 

public class LoginActivity extends SherlockActivity { 
     @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getSupportActionBar().hide(); 
      ... 
... 
.. 
} 

LoginActivityTest.java

import org.robolectric.Robolectric; 
import org.robolectric.RobolectricTestRunner; 
.. 
. 
@RunWith(RobolectricTestRunner.class) 
public class LoginActivityTest { 

    private LoginActivity activity; 
    @Before 
    public void setUp() throws Exception 
    { 
     } 

    @Test @Config(reportSdk = 10) 
    public void shouldActivityCreated() throws Exception { 
      activity = Robolectric.buildActivity(LoginActivity.class).create().get(); 
      assertNotNull(activity); 
    } 
} 

Когда я пытаюсь тест JUnit, я получил отказ со следа:

java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative. 
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.generateLayout(ActionBarSherlockCompat.java:1003) 
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.installDecor(ActionBarSherlockCompat.java:915) 
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.initActionBar(ActionBarSherlockCompat.java:138) 
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.getActionBar(ActionBarSherlockCompat.java:128) 
    at com.actionbarsherlock.app.SherlockActivity.getSupportActionBar(SherlockActivity.java:37) 
    at auth.LoginActivity.onCreate(LoginActivity.java:92) 
    at android.app.Activity.performCreate(Activity.java:5008) 
    at org.fest.reflect.method.Invoker.invoke(Invoker.java:112) 
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:116) 
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256) 
    at org.robolectric.util.ActivityController.create(ActivityController.java:111) 
    at org.robolectric.util.ActivityController.create(ActivityController.java:123) 
    at LoginActivityTest.shouldActivityCreated(LoginActivityTest.java:85) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

После исследования, Я нашел это: https://gist.github.com/marsucsb/6059760 Я создал эти 3 класса и записал необходимую регистрацию & регистрационных кодов в моей функции настройки в моем тестовом классе, я не могу установить contentview для теневой активности: shadowOf (mActivity) .setContentView (contentView);

Я использую Eclipse, и Robolectric

зависимостей 2,2-баночка-с-У вас есть какие-либо идеи, чтобы пропустить эту ActionBarSherlock ошибку во время тестирования? Спасибо. не

+1

Вы используете 'reportSdk = 10' в своем тесте. У вас есть правильная тема для API 10? Может быть, 'res/values-v11' содержит тему Шерлока, но' res/values' нет? – Nachi

+0

@Nachi Я не знаком с этими файлами стилей. Как я могу проверить, что у меня есть правильная настройка темы для API 10? Должен ли я искать res/values ​​/ style.xml? Кстати, minSdk: 8, targetSdk. 17 для моего проекта – ersentekin

+0

Опубликовать все ваши файлы style.xml. Они должны быть под 'res/values' и любыми папками' res/values-vXX'. – Nachi

ответ

1

Как я уже говорил в different answer, here is a Gist который принимает @ Сиань Gist с предложением от marsucsb и модифицирует его для работы с Robolectric 2.2+

Для ответа на этот конкретный вопрос, shadowOf(mActivity) больше не доступен в Robolectric 2.2+ , поэтому следует использовать mActivity.getWindow().setContentView(view) вместо shadowOf(mActivity).setContentView(view)

Смежные вопросы