Upload
washington-botelho
View
2.681
Download
3
Embed Size (px)
DESCRIPTION
Teste de Integração com DbUnit e jIntegrity
Citation preview
TESTE DE INTEGRAÇÃO
@wbotelhos | wbotelhos.com.br | #CafeComJava
WASHINGTON BOTELHO
jIntegrity
Por que testar?
Por que testar?
Para garantir o funcionamento do código.
Quais o tipos comuns de teste?
Quais o tipos comuns de teste?
Unidade
Quais o tipos comuns de teste?
Unidade Manual
Quais o tipos comuns de teste?
Unidade
Aceitação
Manual
Quais o tipos comuns de teste?
Unidade
Aceitação
Manual
Integração
Teste de integração éteste de “maxu”.
Eu disse Macho!!!
Acessa diretamente obanco de dados
Terei que alimentar o banco?
@Beforepublic void setup() { // SQL code...}
@Afterpublic void tearDown() { // SQL code...}
@Beforepublic void setup() { // SQL code...}
@Afterpublic void tearDown() { // SQL code...}
@Testpublic void shouldLoadByID() { assertNotNull("should find", repository.loadByID(1l));}
<dataset> <User id="1" email="[email protected]" name="name-1" /> <User id="2" email="[email protected]" name="name-2" /> <User id="3" email="[email protected]" name="name-3" /></dataset>
public class User {
private Long id; private String email; private String name;
}
Model x XML
@Beforepublic void setup() { execute(INSERT, "User.xml");}
@Afterpublic void tearDown() {
execute(DELETE, "User.xml");}
@Testpublic void shouldLoadByID() { assertNotNull("should find", repository.loadByID(1l));}
Dump Insert
Delete Update
Dump Insert
Delete Update
Restore
XML
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager .getConnection("jdbc:mysql://localhost/test", "root", "root");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager .getConnection("jdbc:mysql://localhost/test", "root", "root");
IDatabaseConnection iConn = new DatabaseConnection(conn);
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager .getConnection("jdbc:mysql://localhost/test", "root", "root");
IDatabaseConnection iConn = new DatabaseConnection(conn);
InputStream stream = getClass().getResourceAsStream("/User.xml");
FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
IDataSet dataSet = builder.build(stream);
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager .getConnection("jdbc:mysql://localhost/test", "root", "root");
IDatabaseConnection iConn = new DatabaseConnection(conn);
InputStream stream = getClass().getResourceAsStream("/User.xml");
FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
IDataSet dataSet = builder.build(stream);
DatabaseOperation.INSERT.execute(iConn, dataSet);
iConn.close();
execute(CLEAN_INSERT, xml);
execute(DELETE, xml);
execute(DELETE_ALL, xml);
execute(INSERT, xml);
execute(REFRESH, xml);
execute(UPDATE, xml);
execute(TRUNCATE_TABLE, xml);
Show me the code!
Problemas
dbUnitProvider.execute(DatabaseOperation.INSERT, "/User.xml");dbUnitProvider.execute(DatabaseOperation.INSERT, "/Product.xml");dbUnitProvider.execute(DatabaseOperation.INSERT, "/Payment.xml");
Muito código? Organização?
Flexibilidade? Reescrita?
dbUnitProvider.execute(DatabaseOperation.DELETE, "/Payment.xml");dbUnitProvider.execute(DatabaseOperation.DELETE, "/Product.xml");dbUnitProvider.execute(DatabaseOperation.DELETE, "/User.xml");
jIntegrityA toolbox to help you test!
Properties
path=xml=User,Product,Payment
jintegrity.properties
"/User.xml", "/Product.xml", "/Payment.xml"
Properties
path=xml=User,Product,Payment
jintegrity.properties
"/User.xml", "/Product.xml", "/Payment.xml"
hibernate.properties
hibernate.connection.driver_class=hibernate.connection.url=hibernate.connection.username=hibernate.connection.password=
Configuração transparente!
JIntegrity helper = new JIntegrity();
helper.clean();
helper.insert();
helper.clean();
helper.insert();
"/User.xml", "/Product.xml", "/Payment.xml"
helper.clean();
helper.insert();
"/User.xml", "/Product.xml", "/Payment.xml"
"/Payment.xml", "/Product.xml", "/User.xml" // self.down?
helper.clean();
helper.insert();
helper.cleanAndInsert();
"/User.xml", "/Product.xml", "/Payment.xml"
"/Payment.xml", "/Product.xml", "/User.xml" // self.down?
Flexibilidade!
helper.path("my/package/dataset").insert();
Flexibilidade!
helper.xml("User", "Payment").insert();
helper.path("my/package/dataset").insert();
Flexibilidade!
helper.xml("User", "Payment").insert();
helper.path("my/package/dataset").insert();
helper.insert() .insert("User") .path("package/dataset").xml("User").insert() .insert("/package/xml/User");
Vendors!
helper.vendor(JIntegrity.ORACLE10G);
Vendors!
helper.useDB2();helper.useHSQL();helper.useMSSQL();helper.useMySQL();helper.useOracle10g();
helper.vendor(JIntegrity.ORACLE10G);
Helpers!
Helpers!private UserRepository repository;
Helpers!
@BeforeClass public static void beforeClass() {JPAHelper.entityManagerFactory("default");
}
private UserRepository repository;
Helpers!
@Before public void setup() { EntityManager manager = JPAHelper.currentEntityManager(); repository = new UserBusiness(manager);}
@BeforeClass public static void beforeClass() {JPAHelper.entityManagerFactory("default");
}
private UserRepository repository;
Helpers!
@Before public void setup() { EntityManager manager = JPAHelper.currentEntityManager(); repository = new UserBusiness(manager);}
@BeforeClass public static void beforeClass() {JPAHelper.entityManagerFactory("default");
}
private UserRepository repository;
@After public void tearDown() {JPAHelper.close();
}
Show me the code!
#greenbar
Referências
jIntegrity.com
DbUnit.org
github.com/wbotelhos/jintegrity
Dúvidas?
@wbotelhos | wbotelhos.com.br
WASHINGTON BOTELHO
Obrigado! (:
jIntegrity