There are two classes, one testing the connection to the database, the other making entries to the database and reading from it. Each class has this code:

static Connection connection; @BeforeClass public static void init() { connection = DBconnector.getInstance().getConnection(); } @AfterClass public static void close() throws SQLException { connection.close(); } 

If you run these tests separately, then they work fine, but if you throw out the SQL, then the SQL exception No operations allowed after connection closed.

In my opinion, some kind of lag occurs when, after closing the connection, it is impossible to immediately open it again. It would be possible to integrate these two classes into one test, but as I read it would be a bad tone, each class should have its own test.

  • one
    Show the class code DBconnector , I suspect the problem is in it. - Roman
  • Speaking of the uselessness of singletons - etki
  • Why did you decide that "connection to the database should be one"? - Roman
  • I read or watched a video tutorial with some clever uncle somewhere, he said so - fxrbfg

1 answer 1

The DBconnector class should each time getConnection() call open a new connection to the database and return it. And he always returns the same connection with you. Therefore, when it closes in one test ( connection.close() ), it cannot be used in another test.