简体   繁体   English

在IDEA中运行JUnit方法的顺序错误

[英]Wrong order of running JUnit's methods in IDEA

I have a problem with Idea 14 and JUnit. 我对Idea 14和JUnit有疑问。 I can't run @BeforeClass and @AfterClass methods in proper order (before all test and after all test). 我无法以正确的顺序运行@BeforeClass和@AfterClass方法(在所有测试之前和所有测试之后)。 Every time order is different. 每次订单都不同。 I tried to reinstall IDEA, delete all settings but nothing works. 我试图重新安装IDEA,删除所有设置但没有任何作用。 Please help. 请帮忙。 This is example of my test code: 这是我的测试代码的示例:

package com.rent.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import static org.junit.Assert.*;
import org.junit.Test;

public class testnewTest {
    static int num;
    static int num1;

    @BeforeClass
    public static void OnceExecutedBeforeAll() {
        System.out.println("@BeforeClass: onceExecutedBeforeAll");
        num = 15;
        num1 = 16;
    }

    @AfterClass
    public static void after() throws Exception {
        System.out.println("End");
    }

    @Test
    public void testLogin() throws Exception {
        System.out.println("test");
        assertEquals(15, num);
    }

    @Test
    public void testGetOrdersDate() throws Exception {
        System.out.println("test2");
        assertEquals(16, num1);
    }
}

This is output: 这是输出:

 test2
 @BeforeClass: onceExecutedBeforeAll
 test
 End

What you're likely observing is the fact that the output is not always going to be synchronous in the terminal. 您可能观察到的是输出并不总是在终端中同步。 The test themselves are running in the correct sequence. 测试本身按正确的顺序运行。

If they weren't, then you would have failures in test2 given that it would have appeared that your @BeforeClass method fired afterwards . 如果它们不是,那么你会在test2失败,因为看起来你的@BeforeClass方法后来被解雇

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM