通过Maven从内部类运行测试 [英] Run tests from inner classes via Maven

查看:140
本文介绍了通过Maven从内部类运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试结构:

public class WorkerServiceTest {

    public class RaiseErrorTest extends AbstractDbUnitTest{
        @Test
        public void testSomething(){
        } 

        ...
    }

    ...
}

这样做是因为我不想为每个测试创建单独的类文件案例扩展 AbstractDbUnitTest

It's done because I don't want to create a separate class file for every test case extending AbstractDbUnitTest.

问题是 mvn test 不从我的内部课程运行测试。有没有办法如何配置 Maven 来运行此类测试?当然我可以在父类中创建调用内部类方法的方法,但我想要一个更清晰的方法。

The problem is that mvn test doesn't run test from my inner class. Is there is a way how to configure Maven to run such tests? Of course I can create methods in the parent class calling the inner class methods but I want a clearer way.

推荐答案

是的,这个可以使用新的(好吧,它不再是新的) 附上 runner(自 JUnit 4.5以来) 运行外部类的所有静态内部类。

Yes, this is possible using the new (well, it's not new anymore) Enclosed runner (since JUnit 4.5) that runs all static inner classes of an outer class.

要使用它,只需使用<$注释外部类c $ c> @RunWith(Enclosed.class)并使内部类静态。

To use it, just annotate the outer class with @RunWith(Enclosed.class) and make the inner classes static.

@RunWith(Enclosed.class)
public class WorkerServiceTest {

    public static class RaiseErrorTest extends AbstractDbUnitTest{
        @Test
        public void testSomething(){
        } 

        ...
    }

    ...
}

mvn test 将运行它们。

这篇关于通过Maven从内部类运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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