-1

In my program is two classes: Main.java and DatabaseConnector.java. The second includes 10 methods (making/closing connection, interpreting args values and methods that interacts with database by PrepareStatement).

For each function I want to write about 2-4 test. My question is how to organize that? Should I create DatabaseConnetorTest class and there include all 20-40 tests? Or it is good to make new test class for each function (makeConnectionTest, InsertOrderTest, ...) for better code transparency?

Another question is, what is a good practice to test function that interacting with mysql database?

jqboml
  • 13
  • 2
  • Having many public methods in a single class, which requires many tests for that single class, is a code smell. For example, `DatabaseConnector` suggests a class which provides method(s) to connect to a database. The name does not suggest methods which _interpreting args values and methods that interacts with database by PrepareStatement_. Those methods could be in their own class (single responsibility). – Andrew S Jun 12 '20 at 16:22

1 Answers1

1

It is not a good practice to write tests for each method in the class. This might prompt you to make all methods public and break encapsulation principle.

This about how the class is going to be used and write unit tests to cover those functional scenarios (both positive and negative) rather than trying to write tests for each method.

If there are a lot of tests, one way is to organize them into separate test classes based on the functional scenario(s)

Some useful answers were provided in this stackoverflow thread for a similar question.

Sudhir
  • 1,339
  • 2
  • 15
  • 36