使用 Junit5 在 spring 启动应用程序中测试服务层时如何避免数据库连接
How to avoid database connection while testing service layer in spring boot application with Junit5
我正在尝试对内部调用存储库方法的服务方法进行单元测试。我的测试方法如下:-
@SpringBootTest
public class EmployeeServiceImplTest {
@MockBean
private EmployeeRepository employeeRepository;
@Autowired
private EmployeeService employeeService;
private static Employee emp;
@BeforeAll
static void setup() {
emp = new Employee(99, "TestUser");
}
@Test
public void listAllEmployeesTest() throws Exception {
List<Employee> allEmployee = Arrays.asList(employee);
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployee);
List<Employee> employeeList = (List<Employee>) cashierService.listAllEmployee();
Assertions.assertIterableEquals(allEmployee,employeeList);
}
}
我问的其实不是问题。当我达到 运行 以上时,Spring 引导应用程序启动并尝试使用数据库连接创建 hikari 池初始化。
我如何避免这种情况,因为它是单元测试,我正在模拟存储库而不与数据库交互。
谢谢
通常用于测试服务层,没有必要使用Spring测试框架。您可以模拟您的服务使用的所有 bean,除非您确实需要 Spring 上下文。
@RunWith(MockitoJUnitRunner.class)
public class EmployeeServiceImplTest {
@Mock
private EmployeeRepository employeeRepository;
@InjectMocks
private EmployeeService employeeService;
private static Employee emp;
@BeforeAll
static void setup() {
emp = new Employee(99, "TestUser");
}
@Test
public void listAllEmployeesTest() throws Exception {
List<Employee> allEmployee = Arrays.asList(employee);
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployee);
List<Employee> employeeList = (List<Employee>) cashierService.listAllEmployee();
Assertions.assertIterableEquals(allEmployee,employeeList);
}
}
也许你可以尝试通过添加
只加载你需要加载的类
@SpringBootTest(classes = {EmployeeRepository.class,EmployeeService.class})
我正在尝试对内部调用存储库方法的服务方法进行单元测试。我的测试方法如下:-
@SpringBootTest
public class EmployeeServiceImplTest {
@MockBean
private EmployeeRepository employeeRepository;
@Autowired
private EmployeeService employeeService;
private static Employee emp;
@BeforeAll
static void setup() {
emp = new Employee(99, "TestUser");
}
@Test
public void listAllEmployeesTest() throws Exception {
List<Employee> allEmployee = Arrays.asList(employee);
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployee);
List<Employee> employeeList = (List<Employee>) cashierService.listAllEmployee();
Assertions.assertIterableEquals(allEmployee,employeeList);
}
}
我问的其实不是问题。当我达到 运行 以上时,Spring 引导应用程序启动并尝试使用数据库连接创建 hikari 池初始化。
我如何避免这种情况,因为它是单元测试,我正在模拟存储库而不与数据库交互。
谢谢
通常用于测试服务层,没有必要使用Spring测试框架。您可以模拟您的服务使用的所有 bean,除非您确实需要 Spring 上下文。
@RunWith(MockitoJUnitRunner.class)
public class EmployeeServiceImplTest {
@Mock
private EmployeeRepository employeeRepository;
@InjectMocks
private EmployeeService employeeService;
private static Employee emp;
@BeforeAll
static void setup() {
emp = new Employee(99, "TestUser");
}
@Test
public void listAllEmployeesTest() throws Exception {
List<Employee> allEmployee = Arrays.asList(employee);
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployee);
List<Employee> employeeList = (List<Employee>) cashierService.listAllEmployee();
Assertions.assertIterableEquals(allEmployee,employeeList);
}
}
也许你可以尝试通过添加
只加载你需要加载的类@SpringBootTest(classes = {EmployeeRepository.class,EmployeeService.class})