英文:
Implementation of BeforeEach within a single method
问题
My project has a DataSource class that implements the database connection parameters, as well as the initialization of the database connection:
public class DataSource {
private static HikariConfig config = new HikariConfig();
private static HikariDataSource ds;
public static HikariDataSource getDataSource() {
if (ds == null) {
config.setJdbcUrl(System.getProperty("jdbcUrl","jdbc:postgresql://localhost:5433/postgres"));
config.setUsername(System.getProperty("jdbcUser","postgres"));
config.setPassword(System.getProperty("jdbcPassword","postgres"));
ds = new HikariDataSource(config);
initDatabase();
}
return ds;
}
private static void initDatabase() {
try {
DatabaseConnection connection = new JdbcConnection(ds.getConnection());
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
Liquibase liquibase = new Liquibase(
System.getProperty("liquibaseFile","liquibase.xml"),
new ClassLoaderResourceAccessor(),
database);
liquibase.update(new Contexts());
} catch (SQLException | LiquibaseException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
The JUnit library is used to develop unit tests for project dao methods. A base class has been created with the help of which the parameters for connecting to the database are redefined (in this case, to H2):
public class Base {
@BeforeAll
static void setup() {
System.setProperty("jdbcUrl", "jdbc:h2:mem:test_" + UUID.randomUUID());
System.setProperty("jdbcUser", "sa");
System.setProperty("jdbcPassword", "");
System.setProperty("liquibaseFile", "liquibase_test.xml");
}
@BeforeEach
void init() {
setup();
}
}
如何确保 System.setProperty 属性在单个方法内重新定义?重要的是该方法的行为类似于 @BeforeEach 而不是 @BeforeAll 注释。我尝试用 @BeforeEach 替换 @BeforeAll 并删除 static,但在执行测试时,访问的是 postgress 数据库,而不是条件所要求的 H2。
【Note: The code you provided is already in Chinese. If you have any further questions or requests related to this code, please let me know.】
英文:
My project has a DataSource class that implements the database connection parameters, as well as the initialization of the database connection:
public class DataSource {
private static HikariConfig config = new HikariConfig();
private static HikariDataSource ds;
public static HikariDataSource getDataSource() {
if (ds == null) {
config.setJdbcUrl(System.getProperty("jdbcUrl","jdbc:postgresql://localhost:5433/postgres"));
config.setUsername(System.getProperty("jdbcUser","postgres"));
config.setPassword(System.getProperty("jdbcPassword","postgres"));
ds = new HikariDataSource(config);
initDatabase();
}
return ds;
}
private static void initDatabase() {
try {
DatabaseConnection connection = new JdbcConnection(ds.getConnection());
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
Liquibase liquibase = new Liquibase(
System.getProperty("liquibaseFile","liquibase.xml"),
new ClassLoaderResourceAccessor(),
database);
liquibase.update(new Contexts());
} catch (SQLException | LiquibaseException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
The JUnit library is used to develop unit tests for project dao methods. A base class has been created with the help of which the parameters for connecting to the database are redefined (in this case, to H2):
public class Base {
@BeforeAll
static void setup() {
System.setProperty("jdbcUrl", "jdbc:h2:mem:test_" + UUID.randomUUID());
System.setProperty("jdbcUser", "sa");
System.setProperty("jdbcPassword", "");
System.setProperty("liquibaseFile", "liquibase_test.xml");
}
@BeforeEach
void init() {
setup();
}
}
How can I make sure that System.setProperty properties are redefined within a single method? It is important that the behavior of the method is similar to that of @BeforeEach instead of the @BeforeAll annotation.
I tried to put @BeforeEach instead of @BeforeAll, removing static, but when the test is executed, the postgress database is accessed, and not H2, as required by the condition.
答案1
得分: 1
使用单例模式和初始化方法,而不是使用系统属性配置数据库,使您的类更易配置和可测试。
但是,值得一提的是,在生产环境和测试环境中使用不同的数据库可能无法复现完全相同的行为。
英文:
Instead of using system properties to configure the database make your classore easily configurable and testable. You could implement a singleton pattern and an initializing method for example.
Having said that, using a different database for production than for testing may not reproduce the exact behavior.
答案2
得分: 1
我理解您想要为测试配置不同的数据源。这可以通过提供不同的application.properties
文件来完成,而无需任何代码。
假设您的application.properties
文件位于src/main/resources
中。请复制该文件并将其放入src/test/resources
文件夹中。在这个文件中,您可以修改测试的属性,它们只会被测试发现,应用程序仍将使用原始文件。
英文:
I understand that you want to configure a different datasource for tests. This can be done without any code just by providing a different application.properties
file.
Assuming your application.properties
are in the src/main/resources
. Make a copy of it and put it in the folder src/test/resources
. In this file you can modify the properties for the tests. They will be found only by the tests. Application will still use the original file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论