在单个方法中实现BeforeEach。

huangapple go评论89阅读模式
英文:

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:

  1. public class DataSource {
  2. private static HikariConfig config = new HikariConfig();
  3. private static HikariDataSource ds;
  4. public static HikariDataSource getDataSource() {
  5. if (ds == null) {
  6. config.setJdbcUrl(System.getProperty("jdbcUrl","jdbc:postgresql://localhost:5433/postgres"));
  7. config.setUsername(System.getProperty("jdbcUser","postgres"));
  8. config.setPassword(System.getProperty("jdbcPassword","postgres"));
  9. ds = new HikariDataSource(config);
  10. initDatabase();
  11. }
  12. return ds;
  13. }
  14. private static void initDatabase() {
  15. try {
  16. DatabaseConnection connection = new JdbcConnection(ds.getConnection());
  17. Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
  18. Liquibase liquibase = new Liquibase(
  19. System.getProperty("liquibaseFile","liquibase.xml"),
  20. new ClassLoaderResourceAccessor(),
  21. database);
  22. liquibase.update(new Contexts());
  23. } catch (SQLException | LiquibaseException e) {
  24. throw new RuntimeException(e);
  25. }
  26. }
  27. public static Connection getConnection() throws SQLException {
  28. return ds.getConnection();
  29. }
  30. }

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):

  1. public class Base {
  2. @BeforeAll
  3. static void setup() {
  4. System.setProperty("jdbcUrl", "jdbc:h2:mem:test_" + UUID.randomUUID());
  5. System.setProperty("jdbcUser", "sa");
  6. System.setProperty("jdbcPassword", "");
  7. System.setProperty("liquibaseFile", "liquibase_test.xml");
  8. }
  9. @BeforeEach
  10. void init() {
  11. setup();
  12. }
  13. }

如何确保 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:

  1. public class DataSource {
  2. private static HikariConfig config = new HikariConfig();
  3. private static HikariDataSource ds;
  4. public static HikariDataSource getDataSource() {
  5. if (ds == null) {
  6. config.setJdbcUrl(System.getProperty("jdbcUrl","jdbc:postgresql://localhost:5433/postgres"));
  7. config.setUsername(System.getProperty("jdbcUser","postgres"));
  8. config.setPassword(System.getProperty("jdbcPassword","postgres"));
  9. ds = new HikariDataSource(config);
  10. initDatabase();
  11. }
  12. return ds;
  13. }
  14. private static void initDatabase() {
  15. try {
  16. DatabaseConnection connection = new JdbcConnection(ds.getConnection());
  17. Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
  18. Liquibase liquibase = new Liquibase(
  19. System.getProperty("liquibaseFile","liquibase.xml"),
  20. new ClassLoaderResourceAccessor(),
  21. database);
  22. liquibase.update(new Contexts());
  23. } catch (SQLException | LiquibaseException e) {
  24. throw new RuntimeException(e);
  25. }
  26. }
  27. public static Connection getConnection() throws SQLException {
  28. return ds.getConnection();
  29. }
  30. }

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):

  1. public class Base {
  2. @BeforeAll
  3. static void setup() {
  4. System.setProperty("jdbcUrl", "jdbc:h2:mem:test_" + UUID.randomUUID());
  5. System.setProperty("jdbcUser", "sa");
  6. System.setProperty("jdbcPassword", "");
  7. System.setProperty("liquibaseFile", "liquibase_test.xml");
  8. }
  9. @BeforeEach
  10. void init() {
  11. setup();
  12. }
  13. }

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.

huangapple
  • 本文由 发表于 2023年5月20日 22:23:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定