简体   繁体   English

AssertionFailedError:<class>没有公共构造函数

[英]AssertionFailedError: <class> has no public constructor


I am working with Android Studio and I need to add a unit tests to my project. 我正在使用Android Studio,我需要为我的项目添加单元测试。
I read various tutorials, but nothing hepled me. 我阅读了各种教程,但没有什么能让我理解。
My problem is: 我的问题是:
TestXMLParser.java: TestXMLParser.java:

public class TestXMLParser extends ActivityInstrumentationTestCase2<HomePageActivity> {

public TestXMLParser(Class<HomePageActivity> activityClass) {
    super(activityClass);
}

@Override
public void setUp() throws Exception {
    super.setUp();

    //Controller.init((Activity)getContext());
}

@Override
public void tearDown() throws Exception {
    super.tearDown();
}

public void testTrue() throws Exception {
    assertTrue(true);
}
...
}

When I run it, I see this message: 当我运行它时,我看到这个消息:

junit.framework.AssertionFailedError: Class cz.cvut.kosapp.jUnitTests.TestXMLParser has no public constructor TestCase(String name) or TestCase()
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

I really do not know why. 我真的不知道为什么。 Other jUnit tests works well, for example when I use: 其他jUnit测试运行良好,例如我使用时:

public class TestXMLParser extends AndroidTestCase { ...

in header, this works and tests are running correctly. 在标题中,这项工作和测试正确运行。
But I need use the Context (as a Activity) to run other code (in Controller class). 我需要使用Context(作为Activity)来运行其他代码(在Controller类中)。

Do you have any idea how fix it? 你知道如何解决这个问题吗?
Thank you for your comments. 谢谢您的意见。

You need to add either a default constructor or a constructor which takes a String as a parameter. 您需要添加默认构造函数或将String作为参数的构造函数。 Adding the following default constructor with a call to the base class constructor should work: 通过调用基类构造函数添加以下默认构造函数应该有效:

public TestXMLParser() {
    super(HomePageActivity.class);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么类实例本身没有公共构造函数? - Why class instance itself has no public constructor? 确保类名存在,是公共的,并且具有带有类名和空构造函数的公共片段的空构造函数 - make sure class name exists, is public, and has an empty constructor for public fragment with class name and empty constructor 嵌套类的公共构造函数 - Public constructor for nested class 强制关闭:无法实例化片段,请确保类名称存在,为公共名称并且具有一个空的公共构造函数 - FORCE CLOSE: Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public AssertionFailedError - AssertionFailedError 具体类中的受保护构造函数与抽象类中的公共构造函数 - protected constructor in concrete class vs public constructor in abstract class Java用Package Private Constructor继承公共类 - Java Inheriting a public class with Package Private Constructor 抽象类的公共构造函数是否有充分的理由 - Are there good reasons for a public constructor of an abstract class 内部类中的公共无参数构造函数的NoSuchMethodException - NoSuchMethodException for public no argument constructor in Inner Class 在构造函数中访问另一个类的公共变量 - Access another class's public variable in constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM