I was searching in google to test Java classes which is simple JDBC connection to persist objects into MySQL DB tables. I don't want to create real DB to test the classes. I found out a way to test it using In-Memory database in MySQL with some Junit Stuff
For example, if you want to test the DB table "Students". You might have already written a DB Impl class to persist rows into "Students" table. Let's say, StudentsDBImpl.java is the class which needs to be tested. Write a Junit class StudentsDBImplTest.java and inside setup()
1. setup the JDBC connection as same as in old traditional way,
DriverManager.getConnection(URL, USERNAME, PASSWORD)
2. The only difference is the table creation which is shown below,
create the "Students" table like
"CREATE TABLE Students(Id,Name,Description) Engine=Memory".
Note that the word "Memory" will create this table in memory allow any UPDATES to this table in cache even though the connection created in step 1 is the real one.
3. In teardown(), drop the table like "DROP TABLE Students" and close the connection, if needed.
Hope this is an easiest way to test the DB classes in Junit.
For example, if you want to test the DB table "Students". You might have already written a DB Impl class to persist rows into "Students" table. Let's say, StudentsDBImpl.java is the class which needs to be tested. Write a Junit class StudentsDBImplTest.java and inside setup()
1. setup the JDBC connection as same as in old traditional way,
DriverManager.getConnection(URL, USERNAME, PASSWORD)
2. The only difference is the table creation which is shown below,
create the "Students" table like
"CREATE TABLE Students(Id,Name,Description) Engine=Memory".
Note that the word "Memory" will create this table in memory allow any UPDATES to this table in cache even though the connection created in step 1 is the real one.
3. In teardown(), drop the table like "DROP TABLE Students" and close the connection, if needed.
Hope this is an easiest way to test the DB classes in Junit.
No comments:
Post a Comment