如何在使用Junit5测试Spring Boot应用程序的服务层时避免数据库连接。

huangapple go评论55阅读模式
英文:

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, &quot;TestUser&quot;);
    }

    @Test
    public void listAllEmployeesTest() throws Exception {
        List&lt;Employee&gt; allEmployee = Arrays.asList(employee);
        Mockito.when(employeeRepository.findAll()).thenReturn(allEmployee);
        List&lt;Employee&gt; employeeList = (List&lt;Employee&gt;) 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, &quot;TestUser&quot;);
    }

    @Test
    public void listAllEmployeesTest() throws Exception {
        List&lt;Employee&gt; allEmployee = Arrays.asList(employee);
        Mockito.when(employeeRepository.findAll()).thenReturn(allEmployee);
        List&lt;Employee&gt; employeeList = (List&lt;Employee&gt;) cashierService.listAllEmployee();
        Assertions.assertIterableEquals(allEmployee,employeeList);
    }

}

huangapple
  • 本文由 发表于 2020年8月12日 21:49:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63377953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定