java - Junit5 和 Maven:@BeforeAll 初始化方法未被调用
问题描述
我会很感激任何关于为什么@BeforeAll
在以下 Junit5 测试套件中被跳过的提示。仅供参考,我使用 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);
}
}
解决方案
您的课程没有@RunWith(JUnitPlatform.class)
注释。添加它,它会正常工作。
推荐阅读
- django - When overriding change_form.html template in Django Admin, the breadcrumb for its model listview is grayed out and unclickable
- cython - 如何在 Cython 中使用 atexit?
- android - 是否有任何类型的列表可以将用户信息保存到第二个选项卡中?
- javascript - Datetime-local 设置默认小时数
- javascript - 如果存在经过身份验证的用户,则显示组件
- angular - 如何理解以下代码,这在 Angular 项目中充当管道
- python - 随着维度的增加,带有多处理的 Numpy 矩阵乘法突然变慢
- php - 如何从特定字符中剪切动态字符串并将剩余部分保存在变量中
- rust - 如何将 impl Fn 存储在枚举类型中?
- android - 如何使所有子视图在表格布局中具有相同的长度?