开发运行环境:OEPE 12c(12.1.1.1.0)+ WebLogic Server 12c(12.1.1.1.0)+ Oracle Database 10g Express Edition 10.2.0.1
1. 安装JUnit Library
至此,JPA的开发工作已基本完成,下面使用JUnit进行单元测试。
2. LoginDaoTest.java
package com.oracle.ticketsystem.dao.tests;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
import com.oracle.ticketsystem.beans.Technician;
import com.oracle.ticketsystem.dao.ILoginDao;
import com.oracle.ticketsystem.dao.impl.LoginJPADao;
public class LoginDaoTest {
private ILoginDao loginDao = null;
@Before
public void setUp() throws Exception {
loginDao = new LoginJPADao();
}
@Test
public void testLogin() {
// testing valid technician
String id = "peter";
String pwd = "peter";
Technician technician = loginDao.login(id, pwd);
assertNotNull("No technician found for the ID " + id + " and password "
+ pwd, technician);
// testing in-valid technician with junk parameters
id = "junk";
pwd = "junk";
technician = loginDao.login(id, pwd);
assertNull(
"Technician found for the ID " + id + " and password " + pwd,
technician);
}
}
3. ProductDaoTest.java
package com.oracle.ticketsystem.dao.tests;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.oracle.ticketsystem.beans.Product;
import com.oracle.ticketsystem.dao.IProductDao;
import com.oracle.ticketsystem.dao.impl.ProductJPADao;
public class ProductDaoTest {
private IProductDao productDao = null;
@Before
public void setUp() throws Exception {
productDao = new ProductJPADao();
}
@Test
public void testGetAllProducts() {
List allProducts = productDao.getAllProducts();
assertTrue("No results found", allProducts.size() > 0);
}
@Test
public void testGetProduct() {
long productId = 1001;
Product product = productDao.getProduct(productId);
assertNotNull("No product found for the ID " + productId, product);
}
}
4. ProductDaoTest.java
package com.oracle.ticketsystem.dao.tests;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.oracle.ticketsystem.beans.Ticket;
import com.oracle.ticketsystem.dao.ITicketDao;
import com.oracle.ticketsystem.dao.impl.TicketJPADao;
public class TicketDaoTest {
private ITicketDao ticketDao = null;
private static long testTicketId = 0;
@Before
public void setUp() throws Exception {
ticketDao = new TicketJPADao();
}
@Test
public void testAdd() {
// adding a test ticket
Ticket ticket = ticketDao.add(1001, "Mark", "mark@gmail.com",
"TestTicket", "Ticket about the test");
assertTrue("Could not add test ticket.", (ticket.getId() > 0));
testTicketId = ticket.getId();
}
@Test
public void testGet() {
// finding a test ticket
Ticket ticket = ticketDao.get(testTicketId);
assertNotNull("Could not find test ticket.", ticket);
}
@Test
public void testUpdate() {
// finding a test ticket
Ticket ticket = ticketDao.update(testTicketId, "peter",
"Getting the test ticket", "ASSIGNED");
assertNotNull("Could not update the test ticket.",
ticket.getTechnician());
assertTrue("Could not add history to test ticket.", (ticket
.getTicketHistory().size() > 0));
}
@Test
public void testGetTicketsOwnedByTechnician() {
// finding a test ticket
List tickets = ticketDao.getTicketsOwnedByTechnician("peter");
assertTrue("Could not find tickets assigned to technician 'peter'",
(tickets.size() > 0));
}
@Test
public void testGetOpenTickets() {
// finding a test ticket
List tickets = ticketDao.getOpenTickets();
assertTrue("Could not find open tickets.", (tickets.size() > 0));
}
@Test
public void testRemove() {
// finding a test ticket
ticketDao.remove(testTicketId);
// the ticket has been removed
assertTrue(true);
}
}
4. 测试
分别右键点击三个测试类,选择Run As,选择JUnit Test。
看到绿色的颜色条,说明测试通过。
说明:测试时,Console中总是会报出警告:[EL Warning]: 2012-10-01 21:12:50.858--java.lang.ClassNotFoundException: weblogic.version。
但JUnit测试是全部通过了。
Project 下载:TroubleTickitSystem_JPA.7z
没有评论:
发表评论