如何使用Junit 5模拟春季依赖



我们必须测试类,请参见下文。我们的问题是我们如何填充依赖项,以便我们可以测试您在下面看到的原始类。

public class FragenAntwortenDataprovider extends SortableDataProvider<FragenAntworten, String> {
    @SpringBean
    private IFragenAntwortenService service;
    private IModel<FragenAntworten> filter;

    public FragenAntwortenDataprovider(IModel<FragenAntworten> filter){
        this.filter = filter;
        Injector.get().inject(this);
        setSort("id", SortOrder.DESCENDING);  // absteigend sortieren
    }

    @Override
    public Iterator<? extends FragenAntworten> iterator(long first, long count){
        List<FragenAntworten> list = load();
        List<FragenAntworten> sublist = list.subList((int) first, (int) (first+count));
        return sublist.iterator();
    }
    @Override
    public long size() {return getListSize();}
    @Override
    public IModel<FragenAntworten> model(FragenAntworten object) {
        return Model.of(object);
    }
    private void sort(final List<FragenAntworten> list){
    }
    private long getListSize(){
        List<FragenAntworten> list = service.ladeAlle(filter.getObject().getSystem());
        return list.size();
    }

    private List<FragenAntworten> load(){
        List<FragenAntworten> list = service.ladeAlle(filter.getObject().getSystem());
        return list;
    }
}

由于您使用弹簧,因此可以使用其ReflectionUtils助手类来注入依赖项:

IFragenAntwortenService service = mock(IFragenAntwortenService.class);
IModel<FragenAntworten> model = ...;
FragenAntwortenDataprovider provider = new FragenAntwortenDataprovider(model);
Field serviceField = ReflectionUtils.findField("service", provider);
ReflectionUtils.setField(serviceField, provider, service);

另一个选项是为service字段介绍包装私有设置,并避免使用反射。

您应该让原始类具有注入@Autowired@Inject的依赖项。实际上,您不应使用现场注射,而应使用构造函数注入。然后,您应该将@ExtendWith(MockitoExtension.class)用于Junit 5测试类。您必须将每个依赖性定义为@Mock,对于要测试的类,您使用@InjectMocks注释。

这是要使用的构造函数注入:

public class FragenAntwortenDataprovider extends SortableDataProvider<FragenAntworten, String> {
    private IFragenAntwortenService service;
    private IModel<FragenAntworten> filter;
    @Inject
    public FragenAntwortenDataprovider(IFragenAntwortenService service, IModel<FragenAntworten> filter){
        this.service = service;
        this.filter = filter;
        //I guess you don't need the injector anymore
        //Injector.get().inject(this);
        setSort("id", SortOrder.DESCENDING);  // absteigend sortieren
    }
    //Rest of class
}

这是一个测试类:

@ExtendWith(MockitoExtension.class)
public class testClass() {
    @Mock
    private IFragenAntwortenService service;
    @Mock
    private IModel<FragenAntworten> filter;
    @InjectMocks
    FragenAntwortenDataprovider sut; //System under Test
    @Test
    void test() {
        //Testcode
        // Control the mock with 
        when(service.anymethod()).thenReturn(result);
        var result = sut.callMethodToTest();
        // verify all calls
        verify(service, times(1)).anymethod();
        // and assert
        assertNotNull(result);
    }
}

相关内容

  • 没有找到相关文章

最新更新