Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.junit.Test;
- import sportsandleisurevillage.data.InvoiceRepoImpl;
- import sportsandleisurevillage.domain.Invoice;
- import java.sql.*;
- import java.util.HashMap;
- import static org.junit.Assert.*;
- public class InvoiceRepoImplTest {
- private Connection conn;
- private HashMap<Integer, Invoice> list = new HashMap<>();
- private static String url = "jdbc:mysql://charlesbarry.coventry.domains:3306/charlesb_260CT";
- private static String username = "charlesb_charlie";
- private static String password = "Password1234";
- private void connect(){
- try{
- conn = DriverManager.getConnection(url, username, password);
- }catch(SQLException e){
- throw new IllegalStateException("Failed to Connect", e);
- }
- }
- private static int customerId = 1;//Charlie, barryc2@uni.coventry.ac.uk
- private static int bookingId = 1;//Swimming, 2019-05-27, 30
- private void addInvoice(int numofitems){
- PreparedStatement stmt;
- try {
- stmt = conn.prepareStatement("INSERT INTO Invoice VALUES (NULL, ?, 0, 0, '2019-03-27');");
- stmt.setInt(1, customerId);
- stmt.executeUpdate();
- for(int i = 0; i < numofitems; ++i){
- stmt = conn.prepareStatement("INSERT INTO InvoiceBooking VALUES (NULL, ?, ?);");
- stmt.setInt(1, customerId);
- stmt.setInt(2, bookingId);
- stmt.executeUpdate();
- }
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- @Test
- public void read() {
- connect(); addInvoice(1);//atleast 1 invoice must exist
- InvoiceRepoImpl readDb = new InvoiceRepoImpl();
- ResultSet result = readDb.read();//get all invoices
- int count = 0;
- try{
- while(result.next()) {//put values
- count += 1;
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- assertTrue(count >= 1);//atleast one invoice must exist
- }
- @Test (expected = NullPointerException.class)
- public void updateWithoutArgs() {
- InvoiceRepoImpl updateNoArgs = new InvoiceRepoImpl();//no data provided about update query
- updateNoArgs.update();
- }
- @Test (expected = AssertionError.class)
- public void updateIncorrectArgs(){
- //int id, String col, Boolean bool
- InvoiceRepoImpl updateWrongArgs = new InvoiceRepoImpl(0, "asdfgh", false);//incorrect data given
- //id 0 (not possible unless specified + column doesnt exist
- updateWrongArgs.update();
- }
- @Test
- public void update() {
- }
- @Test
- public void deleteWithoutArgs() {
- }
- @Test
- public void deleteIncorrectArgs() {
- }
- @Test
- public void delete() {
- }
- @Test
- public void closeconn() {
- InvoiceRepoImpl connectionclose = new InvoiceRepoImpl();
- connectionclose.closeconn();//will pass unless exception
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement