Single database connection testing and general database-specific code and unit testing questions

If I have a method that establishes a connection to a database, how can I test this method? Returning bool if the connection succeeds is one way, but is this the best way?

From the verification method, is it better to have a join method as one method and a method to get data back using a separate method?

Also, how would I test methods that return data from a database? I can make a statement against the expected data, but the actual data can change and will still be the correct result set.

EDIT: for the last point to check the data, if it should be a list of cars, then I can check that these are real car models. Or, if they are a group of web servers, I can have a list of existing web servers in the system, return them from the tested code and get the test result. If the results are different, the data is a problem, but the request is not?

Thnaks

+4
source share
5 answers

Firstly, if you use the database, you are no longer testing the device. You have entered integration (for connection configuration) or a functional earth test. And these are very different animals.

The connection method should definitely be separated from the data sample. In fact, your connection must be connected to the factory so that you can combine it. As for connection testing, really all you can check is that your configuration is correct by establishing a connection to the database. You should not try to check your connection pool, as it probably should be a library that someone wrote (dbcp or c3p0). Furthermore, you probably cannot verify this, since your unit / integration / function tests NEVER connect to a production-level database.

How to check if your data access code works. This is functional testing and includes many frameworks and support. You need a separate test database, the ability to create a schema on the fly during testing, insert any static data into the table and return the database to a known clean state after each test. In addition, this database must be created and run in such a way that two people can run tests at once. Especially if you have more than one developer, plus an automatic test box.

Statements must be against data that is either static data (for example, a list of states that often does not change) or data that was inserted during the test and the afterwords are removed so that they do not interfere with other tests.

EDIT: As already noted, there is reason for this. DBUnit is pretty common.

+15
source

You can grab ideas here . I would go for the mock objects when unit testing the database.

Otherwise, if the application is huge, and you run long and complex unit tests, you can also virtualize your database server and easily return it to a saved snapshot to run your tests again in a known environment.

+2
source

Using the Acolyte scheme ( https://github.com/cchantep/acolyte ), you can simulate any database supported by JDBC, describing cases (how to process each executed request / update) and which resultet / updatecount will be returned in each case (describe devices as list of rows for queries, count for updates).

Such a connection can be directly used to transfer an instance where JDBC is required, or registered with a unique identifier in the JDBC URL namespace jdbc: acolyte: to access the code receiving the connection, thanks to JDBC URL resolution.

Regardless of how you create the connection, Acolyte will keep every isolated one that is suitable for unit test (without additional cleaning in the test database).

Since persistence cases can be sent over a different isolated connection, you no longer need a large db file (or settings file): it can be easily divided into different connections, for example. one per method / save module.

Acolyte can be used either in pure Java or in Scala.

+1
source

If the goal is to test the functionality of the method, rather than querying the SP or SQL database, then you might want to consider dependency injection in terms of the data provider interface. In other words, your class uses an interface with methods that return data. The default implementation uses the database. The unit test implementation has several options:

  • mockery (NMock, Moq, etc.), a great way, I live mockery.
  • in-memory database
  • static database with static data

I don't like anything but the first. As a rule, programming for interfaces is always much more flexible.

0
source

To establish a database connection, you can allow the connection to execute very simple SQL as a testing method. Some application servers have this configuration, the following snippet from the JBoss DB configuration:

<!-- sql to call on an existing pooled connection when it is obtained from pool <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql> 
0
source

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


All Articles