在 Spring 引导中使用 Junit 5 进行测试失败

Testing with Junit 5 failed in Spring boot

我有如下服务class。

public class DependantServiceImpl implements DependantService {

private DependantRepository dependantRepository;
private EmployeeRepository employeeRepository;

private final CompanyEntity companyEntity;

private final String DEPENDANT_ROLE = "dependant";

@Autowired
public DependantServiceImpl(
        CompanyEntity companyEntity, DependantRepository dependantRepository,
        EmployeeRepository employeeRepository) {

    this.companyEntity = companyEntity;
    this.dependantRepository = dependantRepository;
    this.employeeRepository = employeeRepository;
}

我使用像下面这样的工厂方法来获取服务层。

@Service
public class DependantServiceFactoryImpl implements DependantServiceFactory {

    private final DependantRepository dependantRepository;
    private final EmployeeRepository employeeRepository;
    private final CompanyRepository companyRepository;

    @Autowired
    public DependantServiceFactoryImpl(
            CompanyRepository companyRepository, DependantRepository dependantRepository,
            EmployeeRepository employeeRepository) {

        this.dependantRepository = dependantRepository;
        this.employeeRepository = employeeRepository;
        this.companyRepository = companyRepository;
    }

    @Override
    public DependantService dependantServiceForCompany(String companyId) {

        return companyRepository.findById(companyId)
                .map(companyEntity -> new DependantServiceImpl(
                        companyEntity, dependantRepository, employeeRepository))
                .orElseThrow(() ->
                        new IllegalArgumentException(String.format("Invalid Compnay Id [{%s}]", companyId)));

    }
}

我想为服务 class (DependantServiceImpl) 编写单元测试,但为此,我必须通过 [=26] 获取服务 class =]DependantServiceFactoryImpl CRUD 存储库(因为所有存储库都从 Hibernate CRUD 存储库扩展。)注入到构造函数。但问题是我无法将存储库注入 DependantServiceFactoryImpl。我尝试了很多方法,如下所示。

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = DependantServiceFactoryImpl.class)
public class DependantServiceImplTest {

    @MockBean
    DependantRepository dependantRepository;
    @MockBean
    EmployeeRepository employeeRepository;
    @MockBean
    CompanyRepository companyRepository;

    @Autowired
    DependantServiceFactory dependantServiceFactory;

    @Test
    @DisplayName("Get dependant succesfully test")
    void getDependentsTest() {

        String companyId = "1";
        String employeeId = "9a76bb33-772c-4c41-b2eb-eb40500d7026";


        List<DependantEntity> dependantEntityList = dependantServiceFactory
                .dependantServiceForCompany(companyId)
                .getDependents(employeeId);

        assertTrue(dependantEntityList.size() > 0);
    }

但是我遇到以下错误(要查看完整错误,请检查 link https://gist.github.com/Menuka5/de6cd71b6e39e0895cf9be4e7ba34b3d

java.lang.IllegalArgumentException: Invalid Compnay Id [{1}]

有人能指出一种创建有效单元测试的方法吗? 提前致谢。 :)

这里有3个大错误:

  1. 它应该是 DependantServiceImpl 的单元测试,所以不,您不需要调用您的工厂来获取服务实例。您也不需要 SpringExtension、MockBean 和 Autowired。您只需要调用 DependantServiceImpl 的构造函数传递一个真实的实体,以及存储库的模拟实例(使用 Mockito 创建)。
  2. 您使用 Autowired 注释了 DependantServiceImpl 构造函数,但这没有任何意义,因为 DependantServiceImpl 不是 Spring bean,不是由 Spring 创建的,并且不可能用 CompanyEntity 注入,因为那不是 Spring bean。
  3. 如果你想让你的测试保持原样并工作,你需要模拟 companyRepository.findById() 方法并使它 return 成为一个非空的 Optional。否则,当然,returns 的值是一个空的 Optional,所以 orElseThrow() 回调被调用,你得到那个异常。