BAEL-1348 | BatchProcessing example using jdbc (#3216)
* BAEL-EVAL | Adding an example to demo Bean Injection ### Changes done A Spring boot app to demo bean injection * Adding @Autowired for Constructor Injection * BAEL-EVAL | Correcting the method name * BAEL-EVAL * BAEL-1348 | Adding example of Batch Processing in JDBC ### Changes done - Added one example using statement - Added one example using preparedstatement * Removing evaluation article * BAEL-1348 | Adding example of Batch Processing in JDBC ### Changes done - Added an example using statement - Added an example using preparedstatement * BAEL-1348 | BatchProcessing example using jdbc ### Changes done - Add example using statement - Add example using PreparedStatement - Add Unit Tests * BAEL-1348 | Changes to pom.xml and refactoring UnitTest * BAEL-1348 | Removing the example using spring-boot
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
617c2ffdba
commit
94ace2eda8
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.jdbc;
|
||||
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Statement;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class BatchProcessingTest {
|
||||
|
||||
|
||||
@InjectMocks
|
||||
private BatchProcessing target = new BatchProcessing();
|
||||
|
||||
@Mock
|
||||
private Connection connection;
|
||||
|
||||
@Mock
|
||||
private Statement statement;
|
||||
|
||||
@Mock
|
||||
private PreparedStatement employeeStatement;
|
||||
|
||||
@Mock
|
||||
private PreparedStatement employeeAddressStatement;
|
||||
|
||||
@Before
|
||||
public void before(){
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void when_useStatement_thenInsertData_success() throws Exception {
|
||||
Mockito.when(connection.createStatement()).thenReturn(statement);
|
||||
target.useStatement();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_useStatement_ifThrowException_thenCatchException() throws Exception {
|
||||
Mockito.when(connection.createStatement()).thenThrow(new RuntimeException());
|
||||
target.useStatement();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_usePreparedStatement_thenInsertData_success() throws Exception {
|
||||
String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);";
|
||||
String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);";
|
||||
Mockito.when(connection.prepareStatement(insertEmployeeSQL)).thenReturn(employeeStatement);
|
||||
Mockito.when(connection.prepareStatement(insertEmployeeAddrSQL)).thenReturn(employeeAddressStatement);
|
||||
target.usePreparedStatement();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_usePreparedStatement_ifThrowException_thenCatchException() throws Exception {
|
||||
String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);";
|
||||
String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);";
|
||||
Mockito.when(connection.prepareStatement(insertEmployeeSQL)).thenReturn(employeeStatement);
|
||||
Mockito.when(connection.prepareStatement(insertEmployeeAddrSQL)).thenThrow(new RuntimeException());
|
||||
target.usePreparedStatement();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user