Add more tests, break them into files.

This commit is contained in:
Tyrel Souza 2017-02-28 20:34:11 -05:00
parent 5260dcfb81
commit 358bd1f82b
No known key found for this signature in database
GPG Key ID: 2EECB5087209E6A5
5 changed files with 71 additions and 22 deletions

View File

@ -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){

View File

@ -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());

View File

@ -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());
}
}

View 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());
}
}

View 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);
}
}