英文:
Junit5 and Maven: @BeforeAll initialization method not being called
问题
我会感激任何关于为什么以下 Junit5 测试套件中的 `@BeforeAll` 被跳过的提示。供您参考,我使用 Maven 构建时的输出:
14:43:53.681 [main] DEBUG org.benthumb.db2_web_app.tests.CustomerDaoTest - 创建测试对象...
运行中:org.benthumb.db2_web_app.tests.CustomerDaoTest
14:43:53.689 [main] DEBUG org.benthumb.db2_web_app.tests.CustomerDaoTest - 执行测试一...
14:43:53.696 [main] DEBUG org.benthumb.db2_web_app.tests.CustomerDaoTest - 客户访问对象为空...
共运行 2 个测试,失败 0 个,错误 0 个,跳过 0 个,耗时 0.005 秒
public class CustomerDaoTest {
    
    private static CustomerDao cDao;
    private static Customer cust;
    private static Properties props;
    private final static String PROP_FILE_NAME = "customer.properties";
    private static InputStream is;
    final static Logger logger = LoggerFactory.getLogger(CustomerDaoTest.class);
    
    public CustomerDaoTest() {
        logger.debug("创建测试对象...");
        is = getClass().getClassLoader().getResourceAsStream(PROP_FILE_NAME);
    }
    
    @BeforeAll
    public static void initializeTestConditions() throws IOException {
        logger.debug("设置客户访问对象...");
        props = new Properties();
        if (is != null) {
            props.load(is);
        } else {
            throw new FileNotFoundException("在类路径中找不到属性文件'" + PROP_FILE_NAME + "'");
        }
        
        cust = new Customer(
                props.getProperty("id"),
                props.getProperty("businessName"),
                props.getProperty("jobTitle"),
                props.getProperty("firstName"),
                props.getProperty("address"),
                props.getProperty("address2"),
                "",
                props.getProperty("city"),
                props.getProperty("state"),
                props.getProperty("country"),
                props.getProperty("phone")
        );
        cDao = new CustomerDao();
        //conn = cDao.conn;
    }
    
    @Test
    public void testConnectionCustomerDao() throws SQLException {
        //Properties prop = conn.getClientInfo();
        //assertEquals("这两个值相等", prop, null);
        logger.debug("执行测试一...");
        Assumptions.assumeTrue(1 == 1);
    }
    
    @Test
    public void testPersistCustomerDao() {
        if (cDao != null) {
            try {
                cDao.addCustomer(cust);
            } catch (SQLException ex) {
                logger.error(Marker.ANY_MARKER, null, ex.getErrorCode());
            }
        } else {
            logger.debug("客户访问对象为空...");
        }
        Assumptions.assumeTrue(1 == 1);
    }
    
}
英文:
I would appreciate any hints as to why @BeforeAll is skipped in the following Junit5 test suite.  FYI, my output when I build with Maven:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
14:43:53.681 [main] DEBUG org.benthumb.db2_web_app.tests.CustomerDaoTest - creating test object...
Running org.benthumb.db2_web_app.tests.CustomerDaoTest
14:43:53.689 [main] DEBUG org.benthumb.db2_web_app.tests.CustomerDaoTest - test one executing...
14:43:53.696 [main] DEBUG org.benthumb.db2_web_app.tests.CustomerDaoTest - customer access object is null...
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
public class CustomerDaoTest {
    
    private static CustomerDao cDao;
    private static Customer cust;
    private static Properties props;
    private final static String PROP_FILE_NAME = "customer.properties";
    private static InputStream is;
    final static Logger logger = LoggerFactory.getLogger(CustomerDaoTest.class);
    
    public CustomerDaoTest() {
        logger.debug("creating test object...");
        is = getClass().getClassLoader().getResourceAsStream(PROP_FILE_NAME);
    }
    
    @BeforeAll
    public static void initializeTestConditions() throws IOException {
        logger.debug("setting up customer access object...");
        props = new Properties();
        if (is != null) {
            props.load(is);
        } else {
            throw new FileNotFoundException("property file '" + PROP_FILE_NAME + "' not found in the classpath");
        }
        
        cust = new Customer(
                props.getProperty("id"),
                props.getProperty("businessName"),
                props.getProperty("jobTitle"),
                props.getProperty("firstName"),
                props.getProperty("address"),
                props.getProperty("address2"),
                "",
                props.getProperty("city"),
                props.getProperty("state"),
                props.getProperty("country"),
                props.getProperty("phone")
        );
        cDao = new CustomerDao();
        //conn = cDao.conn;
    }
    
    @Test
    public void testConnectionCustomerDao() throws SQLException {
        //Properties prop = conn.getClientInfo();
        //assertEquals("these two values are equal", prop, null);
        logger.debug("test one executing...");
        Assumptions.assumeTrue(1 == 1);
    }
    
    @Test
    public void testPersistCustomerDao() {
        if (cDao != null) {
            try {
                cDao.addCustomer(cust);
            } catch (SQLException ex) {
                logger.error(Marker.ANY_MARKER, null, ex.getErrorCode());
            }
        } else {
            logger.debug("customer access object is null...");
        }
        Assumptions.assumeTrue(1 == 1);
    }
    
}
答案1
得分: 0
你的类没有 @RunWith(JUnitPlatform.class) 注解。添加它就可以正常运行了。
英文:
Your class doesn't have @RunWith(JUnitPlatform.class) annotation. Add it and it will work fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论