forked from afsalashyana/Library-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataHelperTest.java
More file actions
68 lines (54 loc) · 2.2 KB
/
DataHelperTest.java
File metadata and controls
68 lines (54 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package library.assistant.database;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import library.assistant.data.model.Book;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class DataHelperTest {
@Test
public void testInsertNewBook() throws SQLException {
// Arrange
Connection mockConn = mock(Connection.class);
PreparedStatement mockStmt = mock(PreparedStatement.class);
Book book = new Book("B100", "Test Title", "Test Author", "Test Publisher", true);
when(mockConn.prepareStatement(anyString())).thenReturn(mockStmt);
when(mockStmt.executeUpdate()).thenReturn(1);
// Act
boolean result = DataHelper.insertNewBook(book, mockConn);
// Assert
assertTrue(result);
verify(mockStmt).setString(1, "B100");
verify(mockStmt).setString(2, "Test Title");
verify(mockStmt).setString(3, "Test Author");
verify(mockStmt).setString(4, "Test Publisher");
verify(mockStmt).setBoolean(5, true);
verify(mockStmt).executeUpdate();
verify(mockStmt).close();
}
@Test
public void testInsertNewBookFailure() throws SQLException {
// Arrange
Connection mockConn = mock(Connection.class);
PreparedStatement mockStmt = mock(PreparedStatement.class);
Book book = new Book("B100", "Test Title", "Test Author", "Test Publisher", true);
when(mockConn.prepareStatement(anyString())).thenReturn(mockStmt);
when(mockStmt.executeUpdate()).thenReturn(0);
// Act
boolean result = DataHelper.insertNewBook(book, mockConn);
// Assert
assertFalse(result);
}
@Test
public void testInsertNewBookException() throws SQLException {
// Arrange
Connection mockConn = mock(Connection.class);
Book book = new Book("B100", "Test Title", "Test Author", "Test Publisher", true);
when(mockConn.prepareStatement(anyString())).thenThrow(new SQLException("DB Error"));
// Act
boolean result = DataHelper.insertNewBook(book, mockConn);
// Assert
assertFalse(result);
}
}