JUnit error - complaints about missing data that was just inserted

I have a very strange problem in JUnit tests that I just can't cover. I have a multi-module java webapp project with a fairly standard structure (DAO, service class, etc.). As part of this project, I have a “main” project that contains some abstract installation code that inserts a test user along with the necessary elements for the user (in this case, it’s “enterprise”, so the user must belong to the enterprise, and this is mandatory at the base level data)

Simple enough so far ... but here weirdness begins

  • some tests do not run and generate a database exception, where he complains that the user cannot be inserted because the enterprise does not exist. But he just created an enterprise in the previous line of code! And there were no errors when entering the enterprise.
  • Even stranger, if this test class works on its own, everything works fine. Only when a test is run as part of a project does it not work!
  • And the same abstracted code was run by 10+ tests before the one that doesn't work!
  • F

I hit my head against a wall with this for several days and did not make any progress. I don’t even know what information to offer to help diagnose this.

  • Using JUnit 4.4, Spring 2.5.6, iBatis 2.3.0, Postgresql 8.3
  • org.springframework.jdbc.datasource.DriverManagerDataSource org.apache.commons.dbcp.BasicDataSource . DriverManagerDataSource , ! .
  • Eclipse Maven

, , !

: . , , , , . . " [java.util.concurrent.ConcurrentHashMap]",

+3
3

... , , , . , , . , , , .

, w760 , @RunWith @ContextConfiguration. - @RunWith @ContextConfiguration, , . , @Ignore ( ).

... - - , , .

0

SO, , , .

: , . ( BasicDataSource - ​​ ). ( DriverManagerDataSource), , .

, " !" , .

(, , ), - ( BasicDataSource, , , ) . 10 , - , , , .

: , - . , , - , . , , . , , , . , .

EDIT: , , close, :

 public class SomeConnectionWrapper {
      private Connection dbConnection;

      protected void finalize() {
           dbConnection.close();
      }
 }

SomeConnectionWrapper , .

+1

I had a similar problem using Junit4 and spring. I had to add @Transactional to my testing methods to read data from the database before committing.

+1
source

Source: https://habr.com/ru/post/1747881/


All Articles