Bom, sou iniciante com o JUNIT (java web em geral) e criei uma classe de Testes baseada em um Dao, que ficou assim:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myfirstjsfapp.dao;
import java.sql.ResultSet;
import java.util.List;
import myfirstjsfapp.db.Product;
import org.junit.*;
import static org.junit.Assert.*;
/**
*
* @author rlanhellas
*/
public class ProductDAOTest {
public ProductDAOTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of Insert method, of class ProductDAO.
*/
@Test
public void testInsert() throws Exception {
System.out.println("Insert");
Product prod = new Product();
prod.setCode(2);
prod.setName("SAPATO");
prod.setNumberOfItens(10);
prod.setPrice(120.00);
ProductDAO instance = new ProductDAO();
boolean expResult = true;
boolean result = instance.Insert(prod);
assertEquals(expResult, result);
}
/**
* Test of Update method, of class ProductDAO.
*/
@Test
public void testUpdate() throws Exception {
System.out.println("Update");
Product prod = new Product();
prod.setCode(1);
prod.setName("RELOGIO");
prod.setNumberOfItens(20);
prod.setPrice(500.00);
ProductDAO instance = new ProductDAO();
boolean expResult = true;
boolean result = instance.Update(prod);
assertEquals(expResult, result);
}
/**
* Test of Delete method, of class ProductDAO.
*/
@Test
public void testDelete() throws Exception {
System.out.println("Delete");
int codeProduct = 1;
ProductDAO instance = new ProductDAO();
boolean expResult = true;
boolean result = instance.Delete(codeProduct);
assertEquals(expResult, result);
}
/**
* Test of getAllProducts method, of class ProductDAO.
*/
@Test
public void testGetAllProducts() throws Exception {
System.out.println("getAllProducts");
ProductDAO instance = new ProductDAO();
List result = instance.getAllProducts();
assertTrue(result.size() > 0);
}
/**
* Test of getById method, of class ProductDAO.
*/
@Test
public void testGetById() throws Exception {
System.out.println("getById");
int codeProduct = 1;
ProductDAO instance = new ProductDAO();
Product result = instance.getById(codeProduct);
assertNotNull(result);
}
/**
* Test of populateProduct method, of class ProductDAO.
*/
@Test
public void testPopulateProduct() throws Exception {
/* System.out.println("populateProduct");
ResultSet rs = null;
ProductDAO instance = new ProductDAO();
Product expResult = null;
Product result = instance.populateProduct(rs);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");*/
}
}
Quando ele vai executar, ele não executa na ordem que está o código, por isso da erro. Ele manda primeiro o DELETE depois o UPDATE, ou seja, executa aleatoriamente( pelo menos eu acho).