簡體   English   中英

如何在 Spring 引導單元測試中使用依賴注入?

[英]How to use Dependency Injection with Spring Boot Unit Testing?

是否可以使用 Spring Boot 在單元測試中使用依賴注入? 對於集成測試@SpringBootTest啟動整個應用程序上下文和容器服務。 但是是否可以在單元測試粒度上啟用依賴注入功能?

這是示例代碼

@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());
    }
}

還是我應該將 SUT(服務類)作為 POJO 管理並手動注入模擬的依賴項? 我想保持快速測試,但盡量減少樣板代碼。

正如評論中提到的@M.Deinum,單元測試不應該使用依賴注入。 使用 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());
    }
}

如果要測試存儲庫,請使用@DataJpaTest 文檔

使用此注釋將禁用完全自動配置,而僅應用與 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());
    }
}

總之,建議的方法是使用Mockito (或類似庫)測試服務層 mocking 存儲庫層,並使用@DataJpaTest測試存儲庫層。

您尚未為MyRepository添加服務中的@Autowired

服務 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());
    }
}

服務測試 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM