c# create and print barcode Testing the persistence layer in Java

Generating Quick Response Code in Java Testing the persistence layer

Testing the persistence layer
QR Code ISO/IEC18004 Maker In Java
Using Barcode drawer for Java Control to generate, create Quick Response Code image in Java applications.
www.OnBarcode.com
QR-Code Recognizer In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
10.3.4 General database testing tips
Code-39 Generator In Java
Using Barcode creation for Java Control to generate, create Code 3/9 image in Java applications.
www.OnBarcode.com
UCC-128 Generation In Java
Using Barcode encoder for Java Control to generate, create GTIN - 128 image in Java applications.
www.OnBarcode.com
In the previous sections, we looked at examples of writing unit tests for your Hibernate code. The tests so far illustrate a few of the important items you should test and how to test them. This section adds more tips, which should streamline your database testing.
Drawing UCC - 12 In Java
Using Barcode encoder for Java Control to generate, create GTIN - 12 image in Java applications.
www.OnBarcode.com
Paint Code128 In Java
Using Barcode generation for Java Control to generate, create Code 128B image in Java applications.
www.OnBarcode.com
Use multiple databases
Code 3 Of 9 Drawer In Java
Using Barcode generation for Java Control to generate, create Code 39 Extended image in Java applications.
www.OnBarcode.com
UPC-E Supplement 2 Generator In Java
Using Barcode encoder for Java Control to generate, create UPC-E image in Java applications.
www.OnBarcode.com
To test a database, you must have a database to test against. We ve assumed you re using a local developer database. On a project, each developer should have his or her own local database, which the developer uses to run the tests against. This approach has multiple benefits. For one, database testing is naturally slow even when you run tests against a local copy of the database; you don t add network latency into the equation by testing against a remote database, which would make it slower still. Second, and more important, it s safer because you re isolated from other database operations. This means you can do whatever is necessary to the database without worrying about affecting production data or other developers. As an additional step, you may even want to have a second local database, specifically for automated testing. This allows for a populated local database to perform manual tests without having to worry about them affecting the automated tests. With the examples thus far, you ve used the events_calendar database for your web application and other tests. A second automated test database might be called events_calendar_testing. The only mild complication is how you configure the hibernate.cfg.xml file to point to the different databases, one for deploying your application and the other for unit testing. What you definitely don t want to do is write two different hibernate.cfg.xml files; duplication is bad news. Several courses of action are at your disposal. In chapter 3, we looked at using both a hibernate.cfg.xml and a hibernate.properties file. You can have multiple hibernate.properties, for multiple databases.
QR Code 2d Barcode Maker In None
Using Barcode encoder for Excel Control to generate, create QR Code image in Microsoft Excel applications.
www.OnBarcode.com
Reading QR-Code In Visual Basic .NET
Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Unit testing with JUnit and DBUnit
Printing PDF-417 2d Barcode In Java
Using Barcode maker for Android Control to generate, create PDF417 image in Android applications.
www.OnBarcode.com
Drawing Code-39 In VS .NET
Using Barcode printer for ASP.NET Control to generate, create Code 3/9 image in ASP.NET applications.
www.OnBarcode.com
A second method available is Ant filtering, as used with XDoclet property substitution in chapter 9. When you insert special @ symbols around properties in the file, Ant can replace them at build time. Here s what the prefiltered hibernate.cfg.xml file would look like:
PDF-417 2d Barcode Reader In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Drawing Barcode In .NET
Using Barcode generation for VS .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
<hibernate-configuration> <session-factory> <property name="connection.username">@db.username@</property> <property name="connection.password">@db.password@</property> <property name="connection.url">@db.url@</property> <property name="connection.driver_class">@db.driver@</property> <property name="dialect">@db.dialect@</property> <mapping resource="com/manning/hq/ch10/Event.hbm.xml"/> <mapping resource="com/manning/hq/ch10/Speaker.hbm.xml"/> <mapping resource="com/manning/hq/ch10/Location.hbm.xml"/> </session-factory> </hibernate-configuration>
Print Code 128A In None
Using Barcode encoder for Font Control to generate, create Code 128 Code Set A image in Font applications.
www.OnBarcode.com
Barcode Encoder In VB.NET
Using Barcode generation for .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
This code allows a single file to be deployed against any number of databases, and can even be used to deploy an application to development, testing, and production servers. The next step is to filter the file using Ant filter targets and separate properties file for each database. Here s what the unit-test.properties file might look like:
EAN128 Generator In None
Using Barcode creator for Software Control to generate, create EAN 128 image in Software applications.
www.OnBarcode.com
ANSI/AIM Code 128 Generator In C#.NET
Using Barcode maker for VS .NET Control to generate, create Code 128 Code Set B image in .NET framework applications.
www.OnBarcode.com
db.username=root db.password= db.url=jdbc:mysql://localhost/events_testing db.driver=com.mysql.jdbc.Driver db.dialect=org.hibernate.dialect.MySQLDialect
Barcode Creator In None
Using Barcode maker for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Barcode Generator In Visual Studio .NET
Using Barcode creation for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
The final step is to write the Ant filter (listing 10.5) that copies and replaces the database values.
Testing the persistence layer
Listing 10.5 Portion of build10.xml, demonstrating how to load the properties file and the filter hibernate.cfg.xml
Loads properties <target name="set-unit-testing"> from a properties file <property file="unit-test.properties" /> </target> <target name="filter-cfg"> Replaces @db.username@ with property value <filterset id="db.filtering" > <filter token="db.username" value="${db.username}" /> <filter token="db.password" value="${db.password}" /> <filter token="db.url" value="${db.url}" /> <filter token="db.driver" value="${db.driver}" /> <filter token="db.dialect" value="${db.dialect}" /> </filterset> <copy todir="${build.classes.dir}"> <fileset file="src/config/hibernate.cfg.xml" /> <filterset refid="db.filtering" /> </copy> </target> Loads properties file <target name="generate-unit-testing" and runs filter depends="set-unit-testing,filter-cfg"/>
Listing 10.5 shows how the same filtering logic can be used to filter and deploy the same hibernate.cfg.xml file for multiple environments. Using this technique allows you to run your tests against any database that you need to.
Write nonbrittle tests
Another way to make sure your tests are quite robust is to not assume too much knowledge about what rows might be in database already. It would be counterproductive for a query test to fail just because there was an extra event in the database. Take, for example, this test from section 10.3.3:
List eventsFor = eventDao.findEventsFor(7, 2005); assertEquals("Should return 1", 1, eventsFor.size()); Event actualEvent = (Event) eventsFor.get(0);
Copyright © OnBarcode.com . All rights reserved.