Java 为什么我的@BeforeClass 方法没有运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/733037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:50:05  来源:igfitidea点击:

Why isn't my @BeforeClass method running?

javajunitannotations

提问by Lynden Shields

I have the following code:

我有以下代码:

    @BeforeClass
    public static void setUpOnce() throws InterruptedException {
        fail("LOL");
    }

And various other methods that are either @Before, @After, @Test or @AfterClass methods.

以及@Before、@After、@Test 或@AfterClass 方法等各种其他方法。

The test doesn't fail on start up as it seems it should. Can someone help me please?

测试不会像它应该的那样在启动时失败。有人能帮助我吗?

I have JUnit 4.5

我有 JUnit 4.5

The method is failing in an immediate call to setUp() which is annotated as @before. Class def is :

该方法在立即调用 setUp() 时失败,注释为 @before。类 def 是:

public class myTests extends TestCase {

采纳答案by user85421

do NOT extend TestCase AND use annotations at the same time!
If you need to create a test suite with annotations, use the RunWith annotation like:

不要扩展 TestCase 并同时使用注释!
如果您需要创建带有注释的测试套件,请使用 RunWith 注释,例如:

@RunWith(Suite.class)
@Suite.SuiteClasses({ MyTests.class, OtherTest.class })
public class AllTests {
    // empty
}


public class MyTests {  // no extends here
    @BeforeClass
    public static void setUpOnce() throws InterruptedException {
        ...
    @Test
    ...

(by convention: class names with uppercase letter)

(按照惯例:类名用大写字母)

回答by Vladimir

the method must be staticand not directly call fail (otherwise the other methods won't be executed).

该方法必须是静态的,不能直接调用失败(否则其他方法将不会被执行)。

The following class shows all the standard JUnit 4 method types:

以下类显示了所有标准 JUnit 4 方法类型:

public class Sample {

    @BeforeClass
    public static void beforeClass() {
        System.out.println("@BeforeClass");
    }

    @Before
    public void before() {
        System.out.println("@Before");
    }

    @Test
    public void test() {
        System.out.println("@Test");
    }

    @After
    public void after() {
        System.out.println("@After");
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("@AfterClass");
    }

}

and the ouput is (not surprisingly):

输出是(不足为奇):

@BeforeClass
@Before
@Test
@After
@AfterClass

回答by shacharsol

In order that the before annotated function will run , I had to do the following: If you use Maven , add a dependency to Junit 4.11+:

为了运行之前注释的函数,我必须执行以下操作:如果您使用 Maven,请向 Junit 4.11+ 添加依赖项:

      <properties>
              <version.java>1.7</version.java>
              <version.log4j>1.2.13</version.log4j>
              <version.mockito>1.9.0</version.mockito>
              <version.power-mockito>1.4.12</version.power-mockito>
               <version.junit>4.11</version.junit>   
              <version.power-mockito>1.4.12</version.power-mockito>
      </properties>           

and the dependency:

和依赖:

      <dependencies>
        <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>${version.junit}</version>
         <scope>test</scope>
       </dependency>    
        .
        .
        .
      </dependencies>

Make sure your Junit Test class is not extending The TestCase class, since this will cause overlapping with Older version:

确保您的 Junit 测试类没有扩展 TestCase 类,因为这会导致与旧版本重叠:

public class TuxedoExceptionMapperTest{
   protected TuxedoExceptionMapper subject;

   @Before
   public void before() throws Exception {
     subject = TuxedoExceptionMapper.getInstance();
      System.out.println("Start");
      MockitoAnnotations.initMocks(this);
   }
}

回答by ben75

Make sure that :

确保:

  • Your test class doesn't inherits from TestCase
  • The @BeforeClass method is static
  • You don't have more than one @BeforeClass method in test class hierarchy (only the most specialized @BeforeClass method will be executed)
  • 您的测试类不继承自 TestCase
  • @BeforeClass 方法是静态的
  • 测试类层次结构中的@BeforeClass 方法不会超过一个(只会执行最专业的@BeforeClass 方法)

回答by Kishore

Make sure you imported @Test from the correct package.

确保从正确的包中导入了 @Test。

  • Correct package: org.junit.Test
  • Incorrect pacakge: org.junit.jupiter.api.Test
  • 正确的包:org.junit.Test
  • 不正确的包:org.junit.jupiter.api.Test

Please note that this is a solution for: If your @Before, @Atter, etc did not get called at all.

请注意,这是一个解决方案:如果您的@Before、@Atter 等根本没有被调用。