我有一个家庭作业,需要创建J2EE应用程序网站。在我的团队中,我是需要准备单元测试的人,但是我在其中存在很多错误,而且我不知道如何解决它们,因此,我想寻求帮助。
类客户:
public class Customer {
private int CustomerID;
private String email;
public int getCustomerID() {
return CustomerID;
}
public void setCustomerID(int CustomerID) {
this.CustomerID = CustomerID;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Class CustomerDAO:
private final DataSource myDataSource;
public CustomerDAO(DataSource dataSource) {
this.myDataSource = dataSource;
}
public Customer LoginCustomer(int customerID, String email) throws DAOException {
Customer result = null;
String sql = "SELECT * FROM CUSTOMER WHERE CUSTOMER_ID = ? AND EMAIL = ?";
try (Connection connection = myDataSource.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, customerID);
stmt.setString(2, email);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String name = rs.getString("NAME");
String address = rs.getString("ADDRESSLINE1");
result = new Customer();
result.setCustomerID(customerID);
result.setEmail(email);
}
}
} catch (SQLException ex) {
Logger.getLogger("DAO").log(Level.SEVERE, null, ex);
throw new DAOException(ex.getMessage());
}
return result;
}
}
DAO类:
public class DAO {
private final DataSource myDataSource;
public DAO(DataSource dataSource) {
this.myDataSource = dataSource;
}
public int addOrder(int ordernum, int idcustomer, int idproduct, int quantity, int shippingcost, Date salesDate, Date shippingDate, String company) throws SQLException {
int result = 0;
String sql = "INSERT INTO PURCHASE_ORDER VALUES=(?,?,?,?,?,?,?,?) ";
try (Connection connection = myDataSource.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, ordernum);
stmt.setInt(2, idcustomer);
stmt.setInt(3, idproduct);
stmt.setInt(4, quantity);
stmt.setInt(5, shippingcost);
stmt.setDate(6, (java.sql.Date) salesDate);
stmt.setDate(7, (java.sql.Date) shippingDate);
stmt.setString(8, company);
result = stmt.executeUpdate();
}
return result;
}
public int deleteOrder(int OrderNum) throws SQLException {
int result = 0;
String sql = "DELETE FROM PURCHASE_ORDER WHERE ORDER_NUM = ?";
try (Connection connection = myDataSource.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, OrderNum);
result = stmt.executeUpdate();
}
return result;
}
public int changeOrder(int Qte, int ordernum) throws SQLException {
int result=0;
String sql = "UPDATE PURCHASE_ORDER SET QUANTITY = ? WHERE ORDER_NUM=?";
try (Connection connection = myDataSource.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, Qte);
stmt.setInt(2, ordernum);
result=stmt.executeUpdate();
}
return result;
}
DataSouceFactory类:
public class DataSourceFactory {
public enum DriverType {
embedded, server
};
// Choic du type de driver : embedded ou serveur
private static final DriverType TYPE = DriverType.embedded;
/**
* Renvoie la source de données (server ou embbeded)
*
* @return la source de données
*/
public static DataSource getDataSource() {
ds.setDatabaseName("sample");
ds.setUser("app");
ds.setPassword("app");
// The host on which Network Server is running
ds.setServerName("localhost");
// port on which Network Server is listening
ds.setPortNumber(1527);
return ds;
}
}
类产品: 公共类产品{
private int idproduct;
private int idmanufacturer;
private String productcode;
private int purchasecost;
private int quantityonhand;
private int markup;
private String available;
private String description;
public Product(int idproduct, int idmanufacturer, String productcode, int purchasecost, int quantityonhand, int markup, String available, String description){
this.idproduct=idproduct;
this.idmanufacturer=idmanufacturer;
this.productcode=productcode;
this.purchasecost=purchasecost;
this.quantityonhand=quantityonhand;
this.markup=markup;
this.available=available;
this.description=description;
}
//GETTER
public int getIdProduct() {
return idproduct;
}
public int getIdManufacturer(){
return idmanufacturer;
}
public String getProductCode(){
return productcode;
}
public int getPurchaseCost(){
return purchasecost;
}
public int getQuantityOnHand(){
return quantityonhand;
}
public int getMarkup(){
return markup;
}
public String getAvailable(){
return available;
}
public String getDescription(){
return description;
}
}
PurchaseOrder:
public class PurchaseOrder {
private int ordernum;
private int idcustomer;
private int idproduct;
private int quantity;
private int shippingcost;
private Date salesDate;
private Date shippingDate;
private String company;
public PurchaseOrder(int ordernum, int idcustomer, int idproduct, int quantity, int shippingcost, Date salesDate, Date shippingDate, String company) {
this.ordernum=ordernum;
this.idcustomer=idcustomer;
this.idproduct=idproduct;
this.quantity=quantity;
this.shippingcost=shippingcost;
this.salesDate=salesDate;
this.shippingDate=shippingDate;
this.company=company;
}
//GETTER
public int getOrderNumber() {
return ordernum;
}
public int getIdCustomer() {
return idcustomer;
}
public int getIdProduct(){
return idproduct;
}
public int getShippingCost(){
return shippingcost;
}
public Date getSalesDate() {
return salesDate;
}
public Date getShippingDate() {
return shippingDate;
}
public String getCompany() {
return company;
}
}
DAOException类:
public class DAOException extends Exception {
/**
* Creates a new instance of <code>DAOException</code> without detail
* message.
*/
public DAOException() {
}
/**
* Constructs an instance of <code>DAOException</code> with the specified
* detail message.
*
* @param msg the detail message.
*/
public DAOException(String msg) {
super(msg);
}
}
单元测试类:
public class DAOTest {
private DAO myDAO;
private DataSource myDataSource;
private static Connection myConnection;
/**
* Test for the data base
*/
@Before
//Test la création de la BDD.
public void createBDD() throws SQLException {
myDataSource = getDataSource();
myConnection = myDataSource.getConnection();
myDAO = new DAO(myDataSource);
}
@After
//Test la destruction de la BDD.
public void deleteBDD() throws SQLException {
myConnection.close();
myDAO = null;
}
@Test
//Test le remplissage de la BDD à partir d'un fichier sql.
private void fillBDD(String file, Connection myConnection) throws SQLException {
String path = DAOTest.class.getResource(file).getFile();
SqlFile fileSQL = new SqlFile(new File(path));
fileSQL.setConnection(myConnection);
fileSQL.execute();
fileSQL.closeReader();
}
/**
* Test for the class dataSourceFactory
*/
//Test l'acquisition de la source de donnée.
@Test
public void getDataSource() throws DAOException {
org.hsqldb.jdbc.JDBCDataSource ds = new org.hsqldb.jdbc.JDBCDataSource();
ds.setDatabase("jdbc:hsqldb:mem:testcase;shutdown=true");
ds.setUser("username");
ds.setPassword("password");
return ds;
}
/**
* Test for the class CustomerDAO
*/
@Test
// Test si le login fonctionne
public void logIN() throws DAOException {
int customerID = 1;
String email = "test@example.com";
assertEquals(customerID, myDAO.identification(email));
}
/**
* Test for the class ???
*/
@Test
//Test le calcule du prix du produit.
public void finalPrice() {
float price = 10;
int quantity = 1;
float discountRate = 0;
float shippingcost = 10;
float markup = 0;
assertEquals(price * quantity * 0.01 * (1 - discountRate) + shippingcost, "Méthode qui calcule le prix final");
}
}
我不知道它是否可以帮助您,但我们在此处制作了一个git:https://github.com/Selena00/ProjetTechnoWeb
我在单元测试类中遇到的第一个问题是,当我执行“ throws DAOException”时,我在确认其位置时出错。
与“私有DAO myDAO”相同,它表示找不到符号DAO。在创建新的“ DAO”的地方到处都有这个问题。 我再次遇到测试“ fillBDD”的问题,它再次表明未找到符号SqlFile。
最后,在测试“ getDataSource”中,它在第一行中表示包org.hsqlsb.jsbc不存在。
在上述所有情况下,它向我提出的唯一建议是在Maven存储库中搜索缺陷,但没有成功。
然后,如果有人可以给我一些有关如何进行测试的建议:“ addOrder”,“ deleteOrder”和“ changeOrder”,我将不胜感激。对于这三个测试,我不知道如何开始:/。
在此先感谢那些愿意花时间帮助我的人:D。