英文:
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 <D extends DomainClass, E Extends EntityClass>{
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<D,E>{
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<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?
答案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<DomainClass, EntityClass> converter;
@Mock
DomainClass domainClass;
@Mock
EntityClass entityClass;
@Test
public void createFromDtos() {
when(converter.createFrom(domainClass)).thenReturn(entityClass);
List<EntityClass> 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<DomainClass, EntityClass> converter;
@Mock
DomainClass domainClass;
@Mock
EntityClass entityClass;
@Test
public void createFromDtos() {
when(converter.createFrom(domainClass)).thenReturn(entityClass);
List<EntityClass> converted = converter.createFromDtos(List.of(domainClass));
assertEquals(List.of(entityClass), converted);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论