英文:
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);
}
}
希望这有所帮助!
英文:
I am trying to unit test a service method which internally calls a repository method. My Test method is as follows:-
@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);
}
}
What I am asking is not a issue actually. When I am running my above, Spring boot application starts up and trying to create hikari pool initialization using DB connection.
How can I avoid this as it's unit test and I am mocking the repository and not interacting with database.
Thanks
答案1
得分: 2
@SpringBootTest(classes = {EmployeeRepository.class, EmployeeService.class}) 可能可以尝试仅加载您需要加载的类。
英文:
Maybe u can try to load only the classes that u need to load by adding
@SpringBootTest(classes = {EmployeeRepository.class,EmployeeService.class})
答案2
得分: 1
通常,为了测试服务层,不需要使用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(emp);
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployee);
List<Employee> employeeList = (List<Employee>) cashierService.listAllEmployee();
Assertions.assertIterableEquals(allEmployee, employeeList);
}
}
英文:
Usually to test the service layer, it is not necessary to use Spring Test framework. You can mock all beans used by your service except if you really need Spring context.
@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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论