我怎样写一个Android JUnit测试时,我的活动依赖于演员通过一个意图通过呢? [英] How do I write an android JUnit test when my activity relies on extras passed through an Intent?

查看:123
本文介绍了我怎样写一个Android JUnit测试时,我的活动依赖于演员通过一个意图通过呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android JUnit测试依赖于通过额外的意图传递给它的类。我能得到的类工作正常,但我仍想知道如何编写单元测试这样的类,作为供试仍然失败。

I am writing an android Junit test for a class that relies on extras passed to it through an Intent. I was able to get the class working properly, but I would still like to know how to write a unit test for such a class, as the test still fails.

public class AddClassEvent extends Activity{
 private String eventType;

  @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  Bundle extras = getIntent().getExtras();
  final String cNo = extras.getString("CourseNum");

  // create a model instance
  final StudentDBModel model = new StudentDBModel(this);

  setContentView(R.layout.add_class_event);
 .....
 .....
         }
     }

测试类的样子...

The test class looks like...

public class AddClassEventTest extends ActivityInstrumentationTestCase2<AddClassEvent>{
 private StudentDBModel model = null;
 private RenamingDelegatingContext context = null;

 public AddClassEventTest() {
  super("com.UI", AddClassEvent.class);
 }

 /**
  * This method is called before each test.
  */
 @Override
 public void setUp() {
  context = new RenamingDelegatingContext(getActivity(), "test_");
  model = new StudentDBModel(context);
 }

 /*
  * This function will test addNewClassEvent() from StudentDBModel
  */
 public void testAddNewClassEvent(){

  ContentValues courseValues = new ContentValues();
  courseValues.put("CourseId", "60-415");
  courseValues.put("CourseName", "Advanced Database Design");
  courseValues.put("Section", "1");
  courseValues.put("Location", "Erie");
  courseValues.put("Credit", "3");
  courseValues.put("ProfEmail", "rfortier@uwindsor.ca");
  courseValues.put("Website", "cs.uwindsor.ca");

  model.addNewCourses(courseValues);

  int numEventsBefore = model.getNumClassEvents();

  ContentValues values = new ContentValues();

  values.put("EventName", "Assignment 1");
  values.put("CourseId", "60-415");
  values.put("EventType", "Assignment");
  values.put("EventWeight", "8");
  values.put("DueDate", "10/20/2010");

  model.addNewClassEvent(values);

  int numEventsAfter = model.getNumClassEvents();

  assertEquals(numEventsBefore + 1, numEventsAfter);
 }
}

的问题是,额外的,我传递给类AddClassEvent是一个PK为我是在另一个类中创建并传递给AddClassEvent通过一个Intent DB中。每当我运行测试我得到一个空指针异常的就行了:

The problem is, the extra that I am passing to the class AddClassEvent is a PK for my DB that is created in another class and passed to AddClassEvent through an Intent. Whenever I run the test I get a NULL Pointer Exception on the on the line:

final String cNo = extras.getString("CourseNum");

我如何创建额外的JUnit测试的信息?有没有办法让这个测试工作?我已搜查广泛,无法找到答案。有一些方法在JUnit测试中错误地创建临时演员,使其认为它正在被其他类产生的?如果是的话,可能会有人请告诉我怎么样?

How do I create the info from the extra in the Junit Test? Is there a way to get this test to work? I have searched extensively and can't find an answer. Is there some way to falsely create the extras in the Junit test so that it thinks it is being created by the other class? If so, could someone please show me how?

行,所以我试图把你的意见,我已经改变了我的设置功能:

OK so I have tried to take your advice and I have changed my setUp function to:

@Override
public void setUp() {
    context = new RenamingDelegatingContext(getActivity(), "test_");
    model = new StudentDBModel(context);
    Intent addEvent = new Intent();
    addEvent.setClassName("com.UI", "com.UI.AddClassEvent");
    addEvent.putExtra("CourseNum", "60-415");
    setActivityIntent(addEvent);
    getActivity();
}

但我仍然得到一个空指针异常。是我的语法错误?有什么建议?

but I am still getting a NULL Pointer exception. Is my syntax wrong? Any suggestions?

推荐答案

你继承的类, ActivityInstrumentationTestCase2 ,允许你嘲笑意图秒。从<一个href="http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html#setActivityIntent%28android.content.Intent%29">documentation:

The class you inherit, ActivityInstrumentationTestCase2, allows you to mock Intents. From the documentation:

您可以注入自定义的Intent到你的活动(见setActivityIntent(意向))。

You can inject custom Intents into your Activity (see setActivityIntent(Intent)).

该文档的 setActivityIntent()进一步明确:

在第一次调用之前调用此方法   到getActivity()来注入   定制意图进入活动   下测试通过。

Call this method before the first call to getActivity() to inject a customized Intent into the Activity under test.

如果你没有的话,这个默认   意图将提供。如果调用   这是你的活动后,一直   启动时,它不会有任何效果。

If you do not call this, the default intent will be provided. If you call this after your Activity has been started, it will have no effect.

所以,你应该能够把调用此方法,在你的设置()在你调用 getActivity()。您可以通过在嘲笑意图进入setActivityIntent像你提到的 - 只是建立一个假的意图与群众演员,你会希望活动看

So you should be able to place a call to this method inside your setUp() before your call to getActivity(). You can pass in a mocked Intent into setActivityIntent like you mentioned -- just build a fake Intent with extras that you'd expect the Activity to see.

这篇关于我怎样写一个Android JUnit测试时,我的活动依赖于演员通过一个意图通过呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆