<tfoot id='nvfa1'></tfoot><legend id='nvfa1'><style id='nvfa1'><dir id='nvfa1'><q id='nvfa1'></q></dir></style></legend>

      <small id='nvfa1'></small><noframes id='nvfa1'>

        <bdo id='nvfa1'></bdo><ul id='nvfa1'></ul>

      <i id='nvfa1'><tr id='nvfa1'><dt id='nvfa1'><q id='nvfa1'><span id='nvfa1'><b id='nvfa1'><form id='nvfa1'><ins id='nvfa1'></ins><ul id='nvfa1'></ul><sub id='nvfa1'></sub></form><legend id='nvfa1'></legend><bdo id='nvfa1'><pre id='nvfa1'><center id='nvfa1'></center></pre></bdo></b><th id='nvfa1'></th></span></q></dt></tr></i><div id='nvfa1'><tfoot id='nvfa1'></tfoot><dl id='nvfa1'><fieldset id='nvfa1'></fieldset></dl></div>
    1. JUnit 混淆:使用“扩展 TestCase"或“@Test"?

      时间:2023-10-01
        <tbody id='No4So'></tbody>
      • <bdo id='No4So'></bdo><ul id='No4So'></ul>

            <small id='No4So'></small><noframes id='No4So'>

            <tfoot id='No4So'></tfoot>

              1. <i id='No4So'><tr id='No4So'><dt id='No4So'><q id='No4So'><span id='No4So'><b id='No4So'><form id='No4So'><ins id='No4So'></ins><ul id='No4So'></ul><sub id='No4So'></sub></form><legend id='No4So'></legend><bdo id='No4So'><pre id='No4So'><center id='No4So'></center></pre></bdo></b><th id='No4So'></th></span></q></dt></tr></i><div id='No4So'><tfoot id='No4So'></tfoot><dl id='No4So'><fieldset id='No4So'></fieldset></dl></div>

                <legend id='No4So'><style id='No4So'><dir id='No4So'><q id='No4So'></q></dir></style></legend>
                本文介绍了JUnit 混淆:使用“扩展 TestCase"或“@Test"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我发现 JUnit 的正确使用(或至少是文档)非常令人困惑.这个问题既可以作为未来的参考,也可以作为一个真实的问题.

                I've found the proper use (or at least the documentation) of JUnit very confusing. This question serves both as a future reference and as a real question.

                如果我理解正确的话,创建和运行 JUnit 测试有两种主要方法:

                If I've understood correctly, there are two main approaches to create and run a JUnit test:

                方法 A(JUnit 3 风格):创建一个扩展 TestCase 的类,并使用单词 test 开始测试方法.当将该类作为 JUnit 测试运行时(在 Eclipse 中),所有以单词 test 开头的方法都会自动运行.

                Approach A (JUnit 3-style): create a class that extends TestCase, and start test methods with the word test. When running the class as a JUnit Test (in Eclipse), all methods starting with the word test are automatically run.

                import junit.framework.TestCase;
                
                public class DummyTestA extends TestCase {
                
                    public void testSum() {
                        int a = 5;
                        int b = 10;
                        int result = a + b;
                        assertEquals(15, result);
                    }
                }
                

                方法 B(JUnit 4 风格): 创建一个普通"类并在方法前添加 @Test 注释.请注意,您不必以单词 test 开始方法.

                Approach B (JUnit 4-style): create a 'normal' class and prepend a @Test annotation to the method. Note that you do NOT have to start the method with the word test.

                import org.junit.*;
                import static org.junit.Assert.*;
                
                public class DummyTestB {
                
                    @Test
                    public void Sum() {
                        int a = 5;
                        int b = 10;
                        int result = a + b;
                        assertEquals(15, result);
                    }
                }
                

                将两者混合似乎不是一个好主意,请参见例如这个stackoverflow问题:

                Mixing the two seems not to be a good idea, see e.g. this stackoverflow question:

                现在,我的问题:

                1. 首选方法是什么,或者您什么时候会使用其中一种方法而不是另一种方法?
                2. 方法 B 允许通过扩展 @Test 注释来测试异常,如 @Test(expected = ArithmeticException.class).但是在使用方法 A 时如何测试异常?
                3. 当使用方法 A 时,您可以将多个测试类分组到一个测试套件中,如下所示:

                1. What is the preferred approach, or when would you use one instead of the other?
                2. Approach B allows for testing for exceptions by extending the @Test annotation like in @Test(expected = ArithmeticException.class). But how do you test for exceptions when using approach A?
                3. When using approach A, you can group a number of test classes in a test suite like this:

                TestSuite suite = new TestSuite("All tests");
                suite.addTestSuite(DummyTestA.class);
                suite.addTestSuite(DummyTestAbis.class);

                但这不能与方法 B 一起使用(因为每个测试类都应该是 TestCase 的子类).为方法 B 分组测试的正确方法是什么?

                我已将 JUnit 版本添加到这两种方法中

                I've added the JUnit versions to both approaches

                推荐答案

                区分比较简单:

                • 扩展 TestCase 是在 JUnit 3 中编写单元测试的方式(当然 JUnit 4 仍然支持它)
                • 使用@Test注解是JUnit 4引入的方式
                • extending TestCase is the way unit tests were written in JUnit 3 (of course it's still supported in JUnit 4)
                • using the @Test annotation is the way introduced by JUnit 4

                通常您应该选择注释路径,除非需要与 JUnit 3(和/或早于 Java 5 的 Java 版本)兼容.新方法有几个优点:

                Generally you should choose the annotation path, unless compatibility with JUnit 3 (and/or a Java version earlier than Java 5) is needed. The new way has several advantages:

                • @Test 注释 更明确,更容易在工具中支持(例如,通过这种方式可以轻松搜索所有测试)
                • 可以用 @Before 注释多个方法/@BeforeClass@After/@AfterClass提供更大的灵活性
                • 支持 @Rule 注释ExpectedException 之类的东西
                • 支持 @Ignored 注释
                • 支持使用 @RunWith 的替代测试运行器

                要在 JUnit 3 TestCase 中测试预期的异常,您必须明确文本.

                To test for expected exceptions in a JUnit 3 TestCase you'd have to make the text explicit.

                public void testMyException() {
                  try {
                    objectUnderTest.myMethod(EVIL_ARGUMENT);
                    fail("myMethod did not throw an Exception!");
                  } catch (MyException e) {
                    // ok!
                    // check for properties of exception here, if desired
                  }
                }
                

                JUnit 5 引入了另一个 API 更改,但仍使用注释.新的 @Test 注释是 org.junit.jupiter.api.Test(旧"JUnit 4 是 org.junit.Test),但它的工作方式与 JUnit 4 几乎相同.

                JUnit 5 introduced yet another API change, but still uses annotations. The new @Test annotation is org.junit.jupiter.api.Test (the "old" JUnit 4 one was org.junit.Test), but it works pretty much the same as the JUnit 4 one.

                这篇关于JUnit 混淆:使用“扩展 TestCase"或“@Test"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <legend id='vdhep'><style id='vdhep'><dir id='vdhep'><q id='vdhep'></q></dir></style></legend>
                    <bdo id='vdhep'></bdo><ul id='vdhep'></ul>
                      <tbody id='vdhep'></tbody>
                  • <i id='vdhep'><tr id='vdhep'><dt id='vdhep'><q id='vdhep'><span id='vdhep'><b id='vdhep'><form id='vdhep'><ins id='vdhep'></ins><ul id='vdhep'></ul><sub id='vdhep'></sub></form><legend id='vdhep'></legend><bdo id='vdhep'><pre id='vdhep'><center id='vdhep'></center></pre></bdo></b><th id='vdhep'></th></span></q></dt></tr></i><div id='vdhep'><tfoot id='vdhep'></tfoot><dl id='vdhep'><fieldset id='vdhep'></fieldset></dl></div>

                        <small id='vdhep'></small><noframes id='vdhep'>

                      1. <tfoot id='vdhep'></tfoot>