问题描述
我试图在运行使用Robolectric一个简单的测试蚀。我跟着,但得到这个错误:
I am trying to run a simple test with Robolectric in eclipse. I followed this tutorial, but got this error :
了java.lang.RuntimeException:java.io.FileNotFoundException:C:\\维沙尔\\开发\\工作区> \\ AndroidHelloWorldTester \\ AndroidManifest.xml中找不到或不是一个文件。它>应指向你的项目的AndroidManifest.xml
然后根据尤金的建议在here,我试图延长RobolectricTestRunner类按指示但得到这个错误: - (井我不能写构造,因为它是导致编译错误完全按照导演)
Then as per Eugen's advice at here, I tried to extend the RobolectricTestRunner class as per instructed here but got this error :- (well I couldn't write the constructor exactly as directed since it was causing compilation error)
java.lang.Exception的:测试类应该有且仅有一个公共的无参数的构造函数
目前,这是类: -
Currently this is the class :-
package com.myTest;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.model.InitializationError;
import android.widget.Button;
import android.widget.TextView;
import com.visd.helloworld.AndroidHelloWorldActivity;
import com.visd.helloworld.R;
import com.xtremelabs.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class Ahtest extends RobolectricTestRunner {
public Ahtest(Class AndroidHelloWorldActivity) throws InitializationError {
super(AndroidHelloWorldActivity, "<the project root location>");
// TODO Auto-generated constructor stub
}
private Button pressMeButton;
private TextView results;
AndroidHelloWorldActivity activity;
@Before
public void setUp() throws Exception {
activity = new AndroidHelloWorldActivity();
activity.onCreate(null);
pressMeButton = (Button) activity.findViewById(R.id.mybut1);
results = (TextView) activity.findViewById(R.id.text1);
}
@After
public void tearDown() throws Exception {
}
@Test
public void testSomethingMeaningfulToMyApp() {
pressMeButton.performClick();
String resultsText = results.getText().toString();
assertThat(resultsText, equalTo("Button Clicked"));
}
}
P.N。 :结果
1)我的应用程序,并为测试做不同的项目。结果
2)按规定我不能确切地写构造函数,因为如果我指定: -
P.N. :
1) I made different project for the application and for the test.
2) I could not write the constructor exactly as specified since if I specified :-
超(TestClass中,新的文件(../其它/项目的根));
我会得到编译错误。
请帮助。
I would get compilation error.Please help.
推荐答案
维沙尔,
你是混合测试和亚军类。
you're mixing test and runner classes.
的JUnit 工作在一般如下:
for (TestClass c: allTests) {
Test t = TestClass.newInstance();
testListener.startTest(t.getName());
try {
t.beforeTest();
t.run();
t.afterTest();
testListener.testPass(t.getName());
} catch (Exception e) {
if (t.isExpectedException(e)) {
testListener.testPass(t.getName());
} else if (isAssertException(e)) {
testListener.testFail(t.getName(), getExplanation(e));
} else {
testListener.testError(t.getName(), e);
}
}
testListener.testEnd(t.getName());
}
由于 Class.newInstance的()
方法调用它需要有公共非零参数的构造函数用于测试类。注释 @Test
, @Before
和 @After
标记的JUnit 哪些类测试类。
Because of Class.newInstance()
method call it's required to have public non-zero parameters constructor for test class. Annotations @Test
, @Before
and @After
mark to junit which classes are test classes.
上面的code是一种测试运行器类。有时你需要从测试运行器类的其他逻辑。例如 Robolectric 和 PowerMock 替换标准的类加载器与自己的特异目的。 Robolectric 以提供针对Android类自己的实现。 PowerMock 作为嘲笑静态的,最终的类和方法,以及对嘲笑类的构造函数的能力。
The code above is kind of test runner class. Sometimes you need additional logic from test runner class. For example Robolectric and PowerMock are replacing standard ClassLoader with own specific for purposes. Robolectric to provide own implementation for Android classes. PowerMock for ability to mock static, final classes and methods, as well for mocking classes constructors.
@RunWith
标注存在通知的JUnit ,它应该用于测试类另一个测试亚军。因此, Robolectric Android的测试应该有 @RunWith(RobolectricTestRunner.class)
。然而,你必须通过延长 RobolectricTestRunner
类以添加额外的功能为 Robolectric 测试运行能力。例如,对于自己的阴影或指向特定的AndroidManifest.xml文件。
@RunWith
annotation exists to notify junit that it should use another test runner for test class. So Robolectric Android test should have @RunWith(RobolectricTestRunner.class)
. However you have ability to add additional functionality to Robolectric test runner by extending RobolectricTestRunner
class. For example for own shadowing or pointing to specific AndroidManifest.xml file.
所以,你应该有自己的测试亚军类 AHTesRunner
在这里您将指向 Robolectric 以特定的清单文件位置:
So you should have your own test runner class AHTesRunner
where you will point Robolectric to specific manifest file location:
public class AHTestRunner extends RobolectricTestRunner {}
和你需要通知的的JUnit ,它应该使用自己的测试运行器(可定制的 Robolectric 测试运行器):
And you need to notify junit that it should use your own test runner (customized Robolectric test runner):
@RunWith(AHTestRunner.class)
public class AHTest {
@Before
......
@Test
......
@After
}
希望这是现在很多明确
Hope this is much clear now
这篇关于在Eclipse中Robolectric测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!