这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions persistence-modules/core-java-persistence-4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
<artifactId>spring-boot-starter</artifactId>
<version>${springframework.boot.spring-boot-starter.version}</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.16.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.jdbc.mocking;

public record Customer(int id, String name, Status status) {

public enum Status {
ACTIVE, LOYAL, INACTIVE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.baeldung.jdbc.mocking;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.baeldung.jdbc.mocking.Customer.Status;

public class CustomersService {

private final DataSource dataSource;

public CustomersService(DataSource dataSource) {
this.dataSource = dataSource;
}

public List<Customer> customersEligibleForOffers() throws SQLException {
try (Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement()) {
ResultSet resultSet = stmt.executeQuery("SELECT * FROM customers");
List<Customer> customers = new ArrayList<>();

while (resultSet.next()) {
Customer customer = mapCustomer(resultSet);
if (customer.status() == Status.ACTIVE || customer.status() == Status.LOYAL) {
customers.add(customer);
}
}
return customers;
}
}

private Customer mapCustomer(ResultSet resultSet) throws SQLException {
return new Customer(
resultSet.getInt("id"),
resultSet.getString("name"),
Status.valueOf(resultSet.getString("status"))
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.jdbc.mocking.v2;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import javax.sql.DataSource;

import com.baeldung.jdbc.mocking.Customer;

public class AllCustomers implements Supplier<List<Customer>> {

private final DataSource dataSource;

@Override
public List<Customer> get() {
try (Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement()) {
ResultSet resultSet = stmt.executeQuery("SELECT * FROM customers");
List<Customer> customers = new ArrayList<>();
while (resultSet.next()) {
customers.add(mapCustomer(resultSet));
}
return customers;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public AllCustomers(DataSource dataSource) {
this.dataSource = dataSource;
}

private Customer mapCustomer(ResultSet resultSet) throws SQLException {
return new Customer(resultSet.getInt("id"), resultSet.getString("name"), Customer.Status.valueOf(resultSet.getString("status")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.jdbc.mocking.v2;

import java.util.List;
import java.util.function.Supplier;

import com.baeldung.jdbc.mocking.Customer;
import com.baeldung.jdbc.mocking.Customer.Status;

public class CustomersServiceV2 {

private final Supplier<List<Customer>> findAllCustomers;

public List<Customer> customersEligibleForOffers() {
return findAllCustomers.get()
.stream()
.filter(customer -> customer.status() == Status.ACTIVE || customer.status() == Status.LOYAL)
.toList();
}

public CustomersServiceV2(Supplier<List<Customer>> findAllCustomers) {
this.findAllCustomers = findAllCustomers;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baeldung.jdbc.mocking;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

import java.sql.SQLException;
import java.util.List;

import org.h2.jdbcx.JdbcDataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.baeldung.jdbc.mocking.Customer.Status;

class JdbcMockingIntegrationTest {

private static JdbcDataSource dataSource;

@BeforeAll
static void setUp() {
dataSource = new JdbcDataSource();
dataSource.setURL("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'classpath:data.sql'");
dataSource.setUser("sa");
dataSource.setPassword("");
}

@Test
void whenFetchingCustomersEligibleForOffers_thenTheyHaveActiveOrLoyalStatus() throws SQLException {
CustomersService customersService = new CustomersService(dataSource);

List<Customer> customers = customersService.customersEligibleForOffers();

assertThat(customers).extracting(Customer::status)
.containsOnly(Status.ACTIVE, Status.LOYAL);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.baeldung.jdbc.mocking;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.mockito.Mockito.when;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

import javax.sql.DataSource;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.baeldung.jdbc.mocking.Customer.Status;
import com.baeldung.jdbc.mocking.v2.CustomersServiceV2;

@ExtendWith(MockitoExtension.class)
class JdbcMockingUnitTest {

@Mock
DataSource dataSource;
@Mock
Connection conn;
@Mock
Statement stmt;
@Mock
ResultSet resultSet;

@Test
void whenFetchingEligibleCustomers_thenTheyHaveCorrectStatus() throws Exception {
//given
CustomersService customersService = new CustomersService(dataSource);

when(dataSource.getConnection())
.thenReturn(conn);
when(conn.createStatement())
.thenReturn(stmt);
when(stmt.executeQuery("SELECT * FROM customers"))
.thenReturn(resultSet);

when(resultSet.next())
.thenReturn(true, true, true, false);
when(resultSet.getInt("id"))
.thenReturn(1, 2, 3);
when(resultSet.getString("name"))
.thenReturn("Alice", "Bob", "John");
when(resultSet.getString("status"))
.thenReturn("LOYAL", "ACTIVE", "INACTIVE");

// when
List<Customer> eligibleCustomers = customersService.customersEligibleForOffers();

// then
assertThat(eligibleCustomers).containsExactlyInAnyOrder(new Customer(1, "Alice", Status.LOYAL), new Customer(2, "Bob", Status.ACTIVE));
}

@Test
void whenFetchingEligibleCustomersFromV2_thenTheyHaveCorrectStatus() {
// given
List<Customer> allCustomers = List.of(new Customer(1, "Alice", Status.LOYAL), new Customer(2, "Bob", Status.ACTIVE),
new Customer(3, "John", Status.INACTIVE));

CustomersServiceV2 service = new CustomersServiceV2(() -> allCustomers);

// when
List<Customer> eligibleCustomers = service.customersEligibleForOffers();

// then
assertThat(eligibleCustomers).containsExactlyInAnyOrder(new Customer(1, "Alice", Status.LOYAL), new Customer(2, "Bob", Status.ACTIVE));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create table customers (id int primary key, name varchar(255), status varchar(255));
insert into customers (id, name, status) values (1, 'Alice', 'LOYAL');
insert into customers (id, name, status) values (2, 'Bob', 'ACTIVE');
insert into customers (id, name, status) values (3, 'Charlie', 'INACTIVE');