[英]How to use Dependency Injection with Spring Boot Unit Testing?
Is it possible to use dependency injection with unit tests using Spring Boot?是否可以使用 Spring Boot 在单元测试中使用依赖注入? For integration testing
@SpringBootTest
start the whole application context and container services.对于集成测试
@SpringBootTest
启动整个应用程序上下文和容器服务。 But is it possible to enable dependency injection functionality at unit test granularity?但是是否可以在单元测试粒度上启用依赖注入功能?
Here's the example code这是示例代码
@ExtendWith(SpringExtension.class)
public class MyServiceTest {
@MockBean
private MyRepository repo;
@Autowired
private MyService service; // <-- this is null
@Test
void getData() {
MyEntity e1 = new MyEntity("hello");
MyEntity e2 = new MyEntity("world");
Mockito.when(repo.findAll()).thenReturn(Arrays.asList(e1, e2));
List<String> data = service.getData();
assertEquals(2, data.size());
}
}
@Service
public class MyService {
private final MyRepository repo; // <-- this is null
public MyService(MyRepository repo) {
this.repo = repo;
}
public List<String> getData() {
return repo.findAll().stream()
.map(MyEntity::getData)
.collect(Collectors.toList());
}
}
Or should I just manage the SUT (service class) as POJO and manually inject the mocked dependencies?还是我应该将 SUT(服务类)作为 POJO 管理并手动注入模拟的依赖项? I want to keep tests fast but minimize boilerplate code.
我想保持快速测试,但尽量减少样板代码。
As @M.Deinum mentioned in the comments, unit tests shouldn't use dependency injection.正如评论中提到的@M.Deinum,单元测试不应该使用依赖注入。 Mock
MyRepository
and inject MyService
using Mockito (and Junit5):使用 Mockito(和 Junit5)模拟
MyRepository
并注入MyService
:
@ExtendWith(MockitoExtension.class)
public class MyServiceTest {
@InjectMocks
private MyService service;
@Mock
private MyRepository repo;
@Test
void getData() {
MyEntity e1 = new MyEntity("hello");
MyEntity e2 = new MyEntity("world");
Mockito.when(repo.findAll()).thenReturn(Arrays.asList(e1, e2));
List<String> data = service.getData();
assertEquals(2, data.size());
}
}
If you want to test the repository, use @DataJpaTest
.如果要测试存储库,请使用
@DataJpaTest
。 From the docs :从文档:
Using this annotation will disable full auto-configuration and instead apply only configuration relevant to JPA tests.
使用此注释将禁用完全自动配置,而仅应用与 JPA 测试相关的配置。
@DataJpaTest
public class MyRepositorTest {
@Autowired
// This is injected by @DataJpaTest as in-memory database
private MyRepository repo;
@Test
void testCount() {
repo.save(new MyEntity("hello"));
repo.save(new MyEntity("world"));
assertEquals(2, repo.count());
}
}
In conclusion, the suggested approach is to test the service layer mocking the repository layer with Mockito (or similar library) and to test the repository layer with @DataJpaTest
.总之,建议的方法是使用Mockito (或类似库)测试服务层 mocking 存储库层,并使用
@DataJpaTest
测试存储库层。
You have not added the @Autowired
in service for MyRepository
您尚未为
MyRepository
添加服务中的@Autowired
Service Class服务 Class
@Service
public class MyService {
private final MyRepository repo; // <-- this is null
@Autowired
public MyService(MyRepository repo) {
this.repo = repo;
}
public List<String> getData() {
return repo.findAll().stream()
.map(MyEntity::getData)
.collect(Collectors.toList());
}
}
Service Test Class服务测试 Class
@ExtendWith(MockitoExtension.class)
public class MyServiceTest {
@Mock
private MyRepository repo;
@InjectMocks
private MyService service;
@Test
void getData() {
MyEntity e1 = new MyEntity("hello");
MyEntity e2 = new MyEntity("world");
Mockito.when(repo.findAll()).thenReturn(Arrays.asList(e1, e2));
List<String> data = service.getData();
assertEquals(2, data.size());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.