在 JUnit 的测试类中定义内部类时出错
对于 JUnit 3,在从 TestCase 继承的 Test 类中定义内部类时遇到一些问题。场景如下所示:
Foo.java
public class Foo {
public void method() { ... }
}
FooTest.java
public class FooTest extends TestCase {
public class Bar extends Foo {
public void method() { ... }
}
public void testMethod() { ... }
}
现在,如果我从 Eclipse 运行它,测试运行正常,但如果我尝试从 Ant 任务运行失败:
[junit] junit.framework.AssertionFailedError: Class Foo$Bar has no public constructor TestCase(String name) or TestCase()
Bar is NOT a Test class, it's just a subclass of Foo overrideing一些我在测试时不需要做真正的事情的方法。
我现在很迷茫,不知道如何解决这个问题。是独立创建子类的唯一方法吗?
I'm having some problems when defining inner classes in a Test class inherited from TestCase, for JUnit 3. Scenario is like following:
Foo.java
public class Foo {
public void method() { ... }
}
FooTest.java
public class FooTest extends TestCase {
public class Bar extends Foo {
public void method() { ... }
}
public void testMethod() { ... }
}
Now, if I run this from Eclipse, the tests run ok, but if I try to run from an Ant task it fails:
[junit] junit.framework.AssertionFailedError: Class Foo$Bar has no public constructor TestCase(String name) or TestCase()
Bar is NOT a Test class, it's just a subclass of Foo overriding some method that I don't need to do the real stuff when testing.
I'm quite lost at the moment and I don't know how to approach this problem. Is the only way to create the subclasses as standalone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为您将嵌套类包含到 junit 文件集中。
将“excludes”属性添加到您的 build.xml 中。
例如:
This is because you included a nested class into junit fileset.
Add an "excludes" property to your build.xml.
For example:
您可以尝试将 Bar 类定义为静态:
...但它在一种环境中工作但在另一种环境中不起作用这一事实表明以下两件事之一:
You could try defining the Bar class as static:
... but the fact that it works in one environment but not in another suggests one of two things:
我感觉自己像个死人,但问题是我今天在 Maven 上遇到了类似的问题。
通常的
mvn test
运行良好,但是当我想从特定包运行测试时,例如mvn test -Dtest=com.test.*
- 抛出initializationError
。这对 Junit 3 和 4 都“有效”。我找到了我的 Maven 案例的原因,这对于 ant 可能是相同的。
事情是:默认情况下,maven 的测试插件(surefire 即)仅将所有类的特定子集视为“测试类”,即按名称搜索它们,例如 *Test 等(您可以在 surefire的主页)。当我们定义
test 属性我们完全覆盖默认行为。这意味着使用
-Dtest=com.test.*
Surefire 不仅会获取com.test.MyTestClass
,还会获取com.test.MyTestClass.InnerClass< /code> 甚至
com.test.MyTestClass$1
(即匿名类)。因此,为了运行某个包中的类,您应该使用类似
-Dtest=com.test.*Test
的内容(当然,如果您使用后缀来标识测试类)。I'm feeling like a necrposter, but the thing is that I've ran into similar problem with maven today.
Usual
mvn test
runs well but when I want run tests from specific package likemvn test -Dtest=com.test.*
-initializationError
is thrown. This "works" for both Junit 3 and 4.I found the reason for my maven-case, this may be the same for ant.
The thing is: by default maven's test plugin (surefire that is) considers only specific subset of all classes as "test-classes", namely searching them by name, like *Test and so on (you can read about this at surefire's home page).When we define
test
property we completely override default behavior. This means that with-Dtest=com.test.*
surefire will pick up not onlycom.test.MyTestClass
but alsocom.test.MyTestClass.InnerClass
and evencom.test.MyTestClass$1
(i.e. anonymous classes).So in order to run e.g. classes from some package you should use something like
-Dtest=com.test.*Test
(if you use suffixes for identifying test-classes of course).如果您不想排除所有内部类,也可以在嵌套类上注释@Ignore。
You can also annotate the nested class @Ignore if you don't want to exclude all inner classes.