编写用于带有默认方法的通用接口的测试用例

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

Writing Test case for Generic interface with default method

问题

I'm trying to write test cases for the interface with default method
Interface:

public interface XYZConverter<D extends DomainClass, E extends EntityClass>{
   E createFrom(D dto);
   E updateEntity(E entity, D dto);
   
   default List<E> createFromDtos(Collection<D> dtos) {
       return dtos.stream().map(this::createFrom).collect(Collectors.toList());
   }
}

and my test class be like:

public class XYZConverterTest<D, E> {
    
    XYZConverter<D, E> converter = spy(XYZConverter.class);
    
    @Mock
    DomainClass domainClass;
    
    @Mock
    EntityClass entityClass;
    
    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        domainClass = getDomainClass();
        entityClass = getEntityClass();
    }
    
    @Test
    void createFromDtos() {
        EntityClass entityClass = getEntityClass();
        DomainClass domainClass = getDomainClass();
        Mockito.when(converter.createFromDtos(Collection<D> domainClass)).thenReturn(List.of(entityClass));
    }
}

I tried to create test cases but had no luck. If anyone can help me with writing test cases?

英文:

I'm trying to write test cases for the interface with default method
Interface:

public interface XYZConverter &lt;D extends DomainClass, E Extends EntityClass&gt;{
   E createFrom(D dto);
   E updateEntity(E entity, D dto);
   
   default dtos.stream().map(this::createFrom).collect(Collectors.toList());
}

and my test class be like:

public class XYZConverterTest&lt;D,E&gt;{

    XYZConverter converter = spy(XYZConverter.class);
    
@Mock
DomainClass domainClass;

@Mock
EntityClass entityClass;

@BeforeEach
void setUp(){
  MockitoAnnotations.openMocks(this);
  domainClass = getDomainClass();
entityClass = getEntityClass();
 
}

@Test
void createFromDtos(){
  EntityClass = getEntityClass();
  domainClass = getDomainClass();
  Mockito.when(converter.createFromDtos(Collection&lt;D&gt; domainClass)).thenReturn(List.of(entityClass));
}
}

I tried to create test cases but had no luck if anyone can help me with writing test cases?

答案1

得分: 1

使用模拟对象进行测试的关键思想是,你不应该模拟正在测试的内容,只能模拟其他方法或类,这些方法或类是附带使用的。在这种情况下,不应该模拟 createFromDtos 方法,但可以模拟它使用的 createFrom 方法。

例如:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class XYZConverterTest {

    @Spy
    XYZConverter&lt;DomainClass, EntityClass&gt; converter;

    @Mock
    DomainClass domainClass;

    @Mock
    EntityClass entityClass;

    @Test
    public void createFromDtos() {
        when(converter.createFrom(domainClass)).thenReturn(entityClass);
        List&lt;EntityClass&gt; converted = converter.createFromDtos(List.of(domainClass));
        assertEquals(List.of(entityClass), converted);
    }
}
英文:

The key idea with using mocks to test is that you should never mock what is being tested, only other methods or classes that are used incidentally. In this case, the createFromDtos method should not be mocked, but the createFrom method that it uses can be.

For example:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class XYZConverterTest {

    @Spy
    XYZConverter&lt;DomainClass, EntityClass&gt; converter;

    @Mock
    DomainClass domainClass;

    @Mock
    EntityClass entityClass;

    @Test
    public void createFromDtos() {
        when(converter.createFrom(domainClass)).thenReturn(entityClass);
        List&lt;EntityClass&gt; converted = converter.createFromDtos(List.of(domainClass));
        assertEquals(List.of(entityClass), converted);
    }
}

huangapple
  • 本文由 发表于 2023年3月4日 00:17:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629488.html
匿名

发表评论

匿名网友

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

确定