Add more tests, break them into files.
This commit is contained in:
parent
5260dcfb81
commit
358bd1f82b
|
@ -7,6 +7,7 @@ import java.sql.Connection;
|
|||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -23,7 +24,23 @@ public class UserDAO {
|
|||
db = new DBConnection();
|
||||
}
|
||||
|
||||
public List<User> findAll(){
|
||||
public ArrayList<User> findAll(){
|
||||
ArrayList<User> users = new ArrayList<User>();
|
||||
PreparedStatement ps;
|
||||
try {
|
||||
ps = db.connection.prepareStatement("SELECT id, username, full_name FROM user");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
Integer userid = Integer.parseInt(rs.getString("id"));
|
||||
String username = rs.getString("username");
|
||||
String fullName = rs.getString("full_name");
|
||||
User user = new User(userid, username, fullName);
|
||||
users.add(user);
|
||||
}
|
||||
return users;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public User getById(int id){
|
||||
|
|
|
@ -12,7 +12,6 @@ public class DBConnection {
|
|||
|
||||
public DBConnection() {
|
||||
try {
|
||||
// conn = DriverManager.getConnection("jdbc:mysql://localhost/hourtracker?user=hourtracker&password=hours");
|
||||
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hourtracker","hourtracker","hours");
|
||||
} catch (SQLException e) {
|
||||
System.out.println("SQLException: " + e.getMessage());
|
||||
|
|
|
@ -1,14 +1,8 @@
|
|||
|
||||
import dao.UserDAO;
|
||||
import db.DBConnection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import model.User;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* @author tyrelsouza, @date 2/27/17 9:23 PM
|
||||
*/
|
||||
|
@ -23,18 +17,4 @@ public class HourTrackerTest {
|
|||
Assert.assertEquals("tyrel", user.getUsername());
|
||||
Assert.assertEquals("Tyrel Souza", user.getFullName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDBConnection(){
|
||||
DBConnection db = new DBConnection();
|
||||
Assert.assertNotNull(db.connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserDAO(){
|
||||
UserDAO userDao = new UserDAO();
|
||||
User user = userDao.getById(1);
|
||||
Assert.assertEquals("tyrel", user.getUsername());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
37
src/test/java/dao/UserDAOTest.java
Normal file
37
src/test/java/dao/UserDAOTest.java
Normal file
|
@ -0,0 +1,37 @@
|
|||
package dao;
|
||||
|
||||
import dao.UserDAO;
|
||||
import db.DBConnection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import model.User;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* @author tyrelsouza, @date 2/27/17 9:23 PM
|
||||
*/
|
||||
public class UserDAOTest {
|
||||
UserDAO userDao;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
userDao = new UserDAO();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserDAO(){
|
||||
User user = userDao.getById(1);
|
||||
Assert.assertEquals("tyrel", user.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserDAOgetAll(){
|
||||
ArrayList<User> users = userDao.findAll();
|
||||
Assert.assertEquals("tyrel", users.get(0).getUsername());
|
||||
}
|
||||
|
||||
}
|
16
src/test/java/db/DBConnectionTest.java
Normal file
16
src/test/java/db/DBConnectionTest.java
Normal file
|
@ -0,0 +1,16 @@
|
|||
package db;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created by tyrelsouza on 2/28/17.
|
||||
*/
|
||||
public class DBConnectionTest {
|
||||
@Test
|
||||
public void testDBConnection(){
|
||||
DBConnection db = new DBConnection();
|
||||
Assert.assertNotNull(db.connection);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user