77

我读到了结构化单元测试,每个类都有一个测试类,每个方法有一个内部类。认为这似乎是一种组织测试的便捷方式,所以我在我们的 Java 项目中进行了尝试。但是,内部类中的测试似乎根本没有被选中。

我大致是这样做的:

public class DogTests
{
    public class BarkTests
    {
        @Test
        public void quietBark_IsAtLeastAudible() { }

        @Test
        public void loudBark_ScaresAveragePerson() { }
    }

    public class EatTests
    {
        @Test
        public void normalFood_IsEaten() { }

        @Test
        public void badFood_ThrowsFit() { }
    }
}

JUnit 不支持这个,还是我做错了?

4

5 回答 5

97

您应该使用 注释您的类@RunWith(Enclosed.class),并且像其他人所说的那样,将内部类声明为静态:

@RunWith(Enclosed.class)
public class DogTests
  {
  public static class BarkTests
  {
    @Test
    public void quietBark_IsAtLeastAudible() { }

    @Test
    public void loudBark_ScaresAveragePerson() { }
  }

  public static class EatTests
  {
    @Test
    public void normalFood_IsEaten() { }

    @Test
    public void badFood_ThrowsFit() { }
  }
}
于 2013-03-01T13:10:37.130 回答
32
public class ServicesTest extends TestBase {

   public static class TestLogon{

       @Test
       public void testLogonRequest() throws Exception {
         //My Test Code
       }
   }
}

使内部类静态对我有用。

于 2012-01-06T13:27:47.270 回答
19

在 JUnit 5 中,您只需将非静态内部类标记为@Nested

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class DogTests {
    @Nested
    public class BarkTests {
        @Test
        public void quietBark_IsAtLeastAudible() { }

        @Test
        public void loudBark_ScaresAveragePerson() { }
    }

    @Nested
    public class EatTests {
        @Test
        public void normalFood_IsEaten() { }

        @Test
        public void badFood_ThrowsFit() { }
    }
}
于 2017-11-10T00:48:58.457 回答
15

我认为一些答案可能适用于旧版本的 JUnit。在 JUnit 4 中,这对我有用:

    @RunWith(Suite.class)
    @Suite.SuiteClasses({ DogTests.BarkTests.class, DogTests.EatTests.class })
    public class DogTests
    {
        public static class BarkTests
        {
            @Test
            public void quietBark_IsAtLeastAudible() { }

            @Test
            public void loudBark_ScaresAveragePerson() { }
        }

        public static class EatTests
        {
            @Test
            public void normalFood_IsEaten() { }

            @Test
            public void badFood_ThrowsFit() { }
        }
    }
于 2014-03-20T22:25:36.160 回答
10

我在Nitor Creation 的 Nested Runner上也取得了成功。

如何使用 Nitor Creation 的嵌套运行器

这里有一篇解释它的帖子:

添加此依赖项:

<dependency>
    <groupId>com.nitorcreations</groupId>
    <artifactId>junit-runners</artifactId>
    <version>1.2</version>
    <scope>test</scope>
</dependency>

@RunWith你的测试:

import com.nitorcreations.junit.runners.NestedRunner
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
  
@RunWith(NestedRunner.class)
public class RepositoryUserServiceTest {
           
    public class RegisterNewUserAccount {
     
        public class WhenUserUsesSocialSignIn {
             
            public class WhenUserAccountIsFoundWithEmailAddress {
                 
                @Test
                public void shouldThrowException() {
                     assertTrue(true);
                }
            }
         
        }
    }
}

PS:示例代码取自上述博文并进行了修改

于 2015-10-27T13:24:51.530 回答