0

I have question about the creation of testing data in webapp running in Glassfish with JPA (and JSF).

I usually create class like TestDataCreator which generates some basic testing database environment. This class contains main method like:

public static void main(String[] args) {
  createSomeUsers();
  createSomeStockItems();
  putSomeItemsIntoBaskets();
  // ...
}

and each of theese methods just inserts some entities into database.

While I used to use good-old JDBC for database connection, this worked fine. But, now I am implementing app inside of Glassfish and with JPA. So, now the Glassfish is the one who performs the database connection.

So, my question is - how to (correctly) modify this class to be working again. Is some better way than creating website with button "Create database" invoking TestDataCreator's method?

Also, i prefer to store this class in src/test/java, but the "website" solution leads to put class into src/main/java. And this - correct me, if not - stinks.

Thanks in advice

martlin
  • 158
  • 2
  • 10

1 Answers1

0

Well, I can answer half of the questions, if you let me do so.

Is some better way than creating website with button "Create database" invoking TestDataCreator's method?

Yes, there is. My favourite option is the Flyway solution, where it can easily reproduce all the modifications on the table, and raw inserts, across any environment you are working with (dev, test, stage, production, ...). But there is others alternatives as well, like using just the JPA to do so, as this article describes.

Also, i prefer to store this class in src/test/java, but the "website" solution leads to put class into src/main/java. And this - correct me, if not - stinks.

Yes, in the ideal world, you would have the tests using JUnit, with the test files into the "src/test/java" directory. I'm not sure how the datasource connection settings are done into glassfish, so I can't help you with this. But, try to take a look into this stackoverflow answer, maybe it could be useful for you somehow.

Community
  • 1
  • 1