`
JerryWang_SAP
  • 浏览: 969473 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

JUnit 注解@RunWith的工作原理

阅读更多

Suppose you have a large number of unit test cases and you don’t want them to be executed all at the same time during Maven build. You can simply achieve it via annotation @Category.

(1) Create empty class FastTests and SlowTests. (2) In your test case class, categorize your test method using @Category annotation:

 

 

(3) Append the following code to your pom.xml:

<profiles>
        <profile>
            <id>SlowTests</id>
            <properties>
                <testcase.groups>com.sap.SlowTests</testcase.groups>
            </properties>
        </profile>
        <profile>
            <id>FastTests</id>
            <properties>
                <testcase.groups>com.sap.FastTests</testcase.groups>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.13</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.13</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <groups>${testcase.groups}</groups>
                </configuration>
            </plugin>
        </plugins>
    </build>

(4)In my project, by default all 7 test methods will be executed during Maven build:

 

 

Suppose you only want to execute unit test belonging to category “SlowTests”, use the following command line:

 

 

Since now I only marked one method with annotation SlowTests, only one test method is executed:

 

 

If you would like to execute all unit tests EXCEPT @SlowTests, simply add another profile in pom.xml:

<profile>
            <id>NonSlowTests</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <excludedGroups>com.sap.SlowTests</excludedGroups>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

Before test, in order to prove that Slow method is NOT really executed, I add a system.out.println in each method:

 

 

Use command line: mvn test -P NonSlowTests From console output, I can ensure that the method with @Category(SlowTests.class) is NOT executed at all.

 

 

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

0
1
分享到:
评论

相关推荐

    MyBatis 需要注意的地方junit注解

    @RunWith 指定测试类使用的某个运行器参数SpringJUnit4ClassRunner.class @Parameters 指定参数类的参数数据集合 @Rule 允许灵活添加或重新定义测试类中的每个测试方法的行为 @FixMethodOrder 指定测试方法的执行...

    Junit_test.rar_JUnit_JUnit测试_junit工具 @test

    Junit是java中测试的必备工具,Junit_test这个程序是更好的实现对Junit的了解。帮助大家学习

    JUnitCategories:与 JUnit 4 @Category 注释一起使用的(小)类别集合

    JUnit 类别 JUnit 4.9 引入了一个很好的注释来对您的测试进行分类。 而 TestNG 选择为使用字符串 JUnit 类别使用类。 这比字符串更灵活,这在使用多模块 maven 项目时可能会很痛苦(有关更多详细信息,请参阅 JUnit ...

    junit-seasar2

    如何使用@RunWith将Seasar2.class指定的Seasar2.class为Seasar24.class 。 @RunWith ( Seasar24 . class)public class FooTest { // ...}未实现它基本上只是从 Seasar2 复制和粘贴,所以它的工作原理和以前一样,但...

    反射和注解模拟JUnit4单元测试例子

    介绍如何利用反射和注解去简单的模拟JUnit4单元测试的使用,之所以选择JUnit4是...需要注意的是这里并不是完全的模拟,只是简单实现了一下Runner类和JUnit注解相关的工作流程。所以本文的主要目是介绍反射和注解的使用

    Spring In Action-2.1-01-@Component注解

    @RunWith(SpringJUnit4ClassRunner.class)//Spring的Junit测试,会在测试开始时,创建Spring的应用上下文 @ContextConfiguration(classes=CDPlayerSpringConfig.class)//表明配置类 public class SpringTest1 { //...

    Junit单元测试内部机制解析

    Junit java单元测试内部机制解析

    junit-4.13.2的压缩包,JUnit是Java编程语言的单元测试框架,用于编写和可重复运行的自动化测试

    @Runwith 放在测试类名之前,用来确定这个类怎么运行的。也可以不标注,会使用默认运行器 @Parameters 用于使用参数化功能 @SuiteClasses 用于套件测试 @BeforeClass @Before @After @AfterClass这些注解标注的方法...

    junit.zip 包

    springboot整合junit. 。 进行单元测试。 @RunWith和@SpringBootTest

    junit-extension-run-if:JUnit扩展可根据其他测试的结果有条件地运行测试

    &lt;version&gt;0.1.0&lt;/version&gt;&lt;/dependency&gt;如何使用@RunWith(ConditionalRunner.class)public class TestClass1 { @Test public void test01() { ... } @Test @RunIf({"test01"}) public void test02() {

    Junit中的基本注解(教学视频)

    Junit中的基本注解(教学视频) Junit中的基本注解,是必须掌握的。 @BeforeClass – 表示在类中的任意public static void方法执行之前执行 @AfterClass – 表示在类中的任意public static void方法执行之后执行 ...

    Junit 5中@ParameterizedTest与@EnumSource结合使用

    今天小编就为大家分享一篇关于Junit 5中@ParameterizedTest与@EnumSource结合使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    junit-extension:JUnit扩展

    junit-extension 背景 原生的Junit无法满足我们在自动化测试实践过程中的碰到一些需求,比如 ...以上三种注解和Junit build-in的注解@Test配合使用,示例如下 import org.junit.Test; import org.sdet.junit.extens

    java自定义注解\标签库\监听\junit简单测试

    java自定义 注解 annotation、标签库tag、监听listener、junit简单测试代码

    JUnit5用户手册-中文

    Junit4Demo:Junit4 演示

    junit4 使用总结 @Test @Test(timeout=毫秒),@Test(expected=异常类) @Ignore @RunWith @BeforeCalss @AfterClass @Before @After 测试套件@RunWith(Suite.class) 参数化设置@RunWith(Parameterized.class)

    Junit单元测试所需要的两个jar包

    1 Junit是什么 JUnit 是一个 Java 编程语言的单元测试框架。JUnit 在测试驱动的开发方面有很重要的发展,是起源于 JUnit 的一个统称为 xUnit 的单元测试框架之一。 2 单元测试概念 单元:在Java中,一个类就是一个...

    junit的jar包

    org.junit.runner.RunWith.class org.junit.runner.Runner.class org.junit.runner.manipulation.Filter.class org.junit.runner.manipulation.Filterable.class org.junit.runner.manipulation....

    在JUnit中使用@Rule测试文件和目录Java开发Ja

    在JUnit中使用@Rule测试文件和目录Java开发Java经验技巧共3页.pdf.zip

    junit安装告诉读者如何安装junit

    junit安装,告诉读者如何安装junit;junit安装,告诉读者如何安装junit;junit安装,告诉读者如何安装junit;junit安装,告诉读者如何安装junit;junit安装,告诉读者如何安装junit;junit安装,告诉读者如何安装junit;

Global site tag (gtag.js) - Google Analytics