使用Robolectric 2.X创建自定义阴影

时间:2015-02-16 07:41:40

标签: android robolectric

我在我的项目上运行Robolectric 2.x(2.4)。我已经按照官方文档中的示例创建了我自己类的自定义阴影,但Robolectric没有使用它。

项目在GitHub(branch roboMockitoTutorial)。以下是代码的一些部分:

我尝试创建阴影的类(AppUtils):

package com.jiahaoliuliu.robolectricsample;

/**
 * Created by jiahao on 2/15/15.
 */
public class AppUtils {

    public AppUtils() {}

    public int generateNumberUsersRandomly() {
        // :)
        return 42;
    }
}

使用该类的活动:

package com.jiahaoliuliu.robolectricsample;

import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.io.OutputStream;


public class MainActivity extends ActionBarActivity {

    TextView my_hello_text_view;
    Button mClickMeBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppUtils appUtils = new AppUtils();
        System.out.println("The randomly generated number of users is " + appUtils.generateNumberUsersRandomly());
        setContentView(R.layout.activity_main);
        my_hello_text_view = (TextView) findViewById(R.id.my_hello_text_view);
        mClickMeBtn = (Button) findViewById(R.id.clickMeBtn);

        int w = 200, h = 200;
        Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
        Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
        bmp.compress(Bitmap.CompressFormat.PNG, 200, new OutputStream() {
            @Override
            public void write(int oneByte) throws IOException {
                System.out.println("Writting in the outputStream " + oneByte);
            }
        });
    }

    public void clickMeBtnPressed(View view) {
        my_hello_text_view.setText(getString(R.string.ok_thanks));
    }
}

影子类(AppUtilsShadow):

package com.jiahaoliuliu.robolectricsample;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.shadows.ShadowView;

/**
 * Created by jiahao on 2/15/15.
 */
@Implements(AppUtils.class)
public class AppUtilsShadow {

    @RealObject
    private AppUtils appUtils;

    public AppUtilsShadow() {}

    // Trying to create the constructor for it.
    public void __constructor__() {}

    @Implementation
    public int generateNumberUsersRandomly() {
        System.out.println("Invoking the real object method");
        return appUtils.generateNumberUsersRandomly();
    }
}

测试类:

package com.jiahaoliuliu.robolectricsample;

import com.jiahaoliuliu.robolectricsample.MainActivity;
import com.jiahaoliuliu.robolectricsample.RobolectricGradleTestRunner;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.List;

import static org.assertj.android.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@RunWith(RobolectricGradleTestRunner.class)
@Config(shadows = {AppUtilsShadow.class, MyShadowBitmap.class})
public class MyActivityTest {

  private MainActivity mActivity;

  @Before
  public void setup() {
    mActivity = Robolectric.buildActivity(MainActivity.class).create().get();
  }

  @Test
  public void testMyActivityAppearsAsExpectedInitially() {
    assertThat(mActivity.my_hello_text_view).isVisible();
    assertThat(mActivity.my_hello_text_view).hasText("Hello world!");
    assertThat(mActivity.mClickMeBtn).hasText("Click Me");
  }

}

1 个答案:

答案 0 :(得分:4)

Shadows的问题是它们用于遮蔽Android类,而不是所有类。请参阅documentation或更好的code

由于AppUtils不是来自Android SDK,因此您的阴影不会被拾取。

这不是一个缺陷,据我所知,这是一个故意的设计决定。由于这是您自己的类,因此您可以通过更好的方式加载测试类,并且不需要阴影。

也就是说,如果你真的想为此使用阴影,你可以创建一个自定义测试运行器,覆盖shouldInstrument,使其也可以检测你感兴趣的类或包。然后,你上面的代码应该工作。我从未尝试过此操作,但您可以查看this以获取一些示例代码。