简体   繁体   English

为具有组合的类编写测试的最佳实践是什么

[英]What is the best practice for writing tests for classes with composition

Assume I have class Formater假设我有类Formater

class Formatter {
  public FormattedData format(Map<String, Data> data) {
    return .....
  }  
}

and another class Collector that uses Formatter and can return formatted data和另一个使用Formatter并可以返回格式化数据的类Collector

class Collector {
  Formatter formatter;
  Map<Id, Data> map = new HashMap<>()

  class Collector (Formatter formatter) {
    this.formatter = formatter;  
  }

  public void addData(Data data) (
    map.put(data.getId(), data);
  }

  public FormattedData getFormattedData() {
    return formatter.format(map)
  }

So the problem - I want to write tests.所以问题 - 我想写测试。 I wrote all the tests for Formatter class, but how should I test Collector ?我为Formatter类编写了所有测试,但是我应该如何测试Collector

Since I should not rely on implementation of collector - I need to copy all tests for Formatter and pass them as input for Collector .因为我不应该依赖收集器的实现 - 我需要复制Formatter所有测试并将它们作为Collector输入传递。 Surely in tests I would change Map<String, Data> data to Data data as input data type, but anyway there will be a huge code duplication.当然,在测试中我会将Map<String, Data> data更改为Data data作为输入数据类型,但无论如何都会有大量代码重复。 How can I avoid it?我怎样才能避免它?

You use mocks so you don't depend on the formatter implementation.您使用模拟,因此您不依赖于格式化程序的实现。

@Test
@ExtendWith(MockitoExtension.class) // @RunWith(MockitoJUnitRunner.class) for JUnit 4
class CollectorTest {
    @InjectMocks
    private Collector sut;

    @Mock
    private Formatter formatter;

    public FormattedData getFormattedData() {
        FormattedData formatted = mock(FormattedData.class);
        when(formatter.format(any()).thenReturn(formatted);

        FormattedData result = sut.getFormattedData();

        // verify injected formatter was called
        verify(formatter).format(any());
        // verify the result of the formatter is returned by the collector
        assertThat(result).isSameAs(formatted);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JDBC中编写SQL查询的最佳实践是什么? - What is the Best practice for writing a SQL query in JDBC 在java中编写哈希函数的最佳实践是什么? - What is a best practice of writing hash function in java? 将这两个类提取到一个公共类的最佳实践是什么? - What is the best practice for extracting these 2 classes to a common class? 使用Hibernate建模Java类的最佳实践是什么? - What is the best practice for modeling Java classes with Hibernate? 为验证字符串输入编写参数化测试的最佳实践是什么? - What is the best practice to write parameterized tests for validating string inputs? 在单线程和多线程程序的aop中编写方面的最佳实践是什么? - What is the best practice of writing aspects in aop for single threaded and multithreaded program? 内部对象实例化的最佳组合实践 - Composition best practice with internal object instantiation 类中的条件常量属性:最佳实践是什么? - Conditionally constant properties in classes: what's the best practice? 在Android中将类放在包名下的最佳做法是什么? - What is the best practice for putting classes under package names in Android JPA / Hibernate实体类和同步的最佳实践是什么? - What is the best practice for JPA/Hibernate entity classes and synchronization?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM