入力パラメータは、IDのときにそれを行う方法を、フィルタリングされたリストを返すために、サービスのテストを行いますか?

Mudasar:

私は、このメソッドを含むサービスクラスを持っています:

public List<Tag> getSegmentByTypeSaved(String mailChimpListId) {

        List<Tag> tags = mailChimpClient.listTags(mailChimpListId);

        List<Tag> listOfSegmentsByTypeSaved = tags.stream()
                .filter(x -> x.getType().equals(TagType.SAVED))
                .map(y -> new Tag(y.getId(), y.getName(), y.getUpdatedAt(), y.getMemberCount(), y.getType()))
                .sorted(Comparator.comparing(Tag::getUpdatedAt).reversed())
                .collect(Collectors.toList());

        return listOfSegmentsByTypeSaved;
    }

私は私が望むように、サービスの仕事かどうかをテストするテストを作成したいです。今までのテストクラスは次のとおりです。

@Test
    public void getFiltredListByTypeSaved() {

        Tag tagOne = new Tag(1, "Test One", LocalDate.now().minusDays(2).toString(), 5, TagType.SAVED);
        Tag tagTwo = new Tag(2, "Test Two", LocalDate.now().minusDays(5).toString(), 5, TagType.STATIC);

        List<Tag> tags = new ArrayList<>();
        tags.add(tagOne);
        tags.add(tagTwo);

        // Check if list has size of two after filtered

       // Check if list have the properties as above only for SAVED

       // Check if list if sorted by attribute eg: LocalDate

サービスが実際に私にフィルタリングされたリストを返すので、どのように私はモックができますか?入力パラメータとしての私のサービスクラスメソッドのテイクID。サービスクラスでこのコード行List<Tag> tags = mailChimpClient.listTags(mailChimpListId);を返す、私はtags別のオブジェクト含むタイプ「保存」と「静」とのリスト。しかし、私のテストで私が唯一のみのリストを取得することをテストしたいですTagType.SAVED

私のMailChimpServiceクラス:

@Inject
public MailChimpService(MailChimpClient mailChimpClient,
                        MailChimpRepository mailChimpRepository,
                        ProductService productService,
                        SchoolService schoolService,
                        SchoolYearService schoolYearService,
                        SchoolYearConfigService schoolYearConfigService,
                        @Value("${mailchimp.customer.list}") String mailChimpCustomerListId,
                        @Value("${mailchimp.customer.smspermission}") String mailChimpCustomerSmsPermission,
                        @Value("${mailchimp.customer.emailpermission}") String mailChimpCustomerEmailPermission,
                        @Value("${mailchimp.schooladmin.list}") String mailChimpSchoolAdminListId,
                        @Value("${mailchimp.schooladmin.smspermission}") String mailChimpSchoolAdminSmsPermission,
                        @Value("${mailchimp.schooladmin.emailpermission}") String mailChimpSchoolAdminEmailPermission
) {
    this.mailChimpClient = mailChimpClient;
    this.mailChimpRepository = mailChimpRepository;
    this.productService = productService;
    this.schoolService = schoolService;
    this.schoolYearService = schoolYearService;
    this.schoolYearConfigService = schoolYearConfigService;
    this.mailChimpCustomerListId = mailChimpCustomerListId;
    this.mailChimpCustomerEmailPermission = mailChimpCustomerEmailPermission;
    this.mailChimpCustomerSmsPermission = mailChimpCustomerSmsPermission;
    this.mailChimpSchoolAdminListId = mailChimpSchoolAdminListId;
    this.mailChimpSchoolAdminEmailPermission = mailChimpSchoolAdminEmailPermission;
    this.mailChimpSchoolAdminSmsPermission = mailChimpSchoolAdminSmsPermission;
}

何か案が?

ありがとうございました!

マンスールQulam:

あなたが言及したように、基本的には、MailChimpClientは、混合TagTypesのリストを返しますし、あなたのサービスの方法は、それらを除外します。それはあなたが正確に同様にあなたのテストで何をすべきかです。私は単純にやったことはMailChimpClientを模擬し、呼び出されたときに混合タグ要素のリストを返すようにしました。

class FooServiceTest {

    @Mock
    private MailChimpClient mailChimpClient;

    private FooService fooService;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.initMocks(this);
        this.fooService = new FooService(mailChimpClient);
    }

    @Test
    void getFilteredListByTypeSaved() {
        String mailChimpListId = "id";

        final List<Tag> tags = List.of(
                new Tag(1, "Test One", LocalDate.now().minusDays(2).toString(), 5, TagType.SAVED),
                new Tag(2, "Test Two", LocalDate.now().minusDays(5).toString(), 5, TagType.STATIC),
                new Tag(1, "Test Three", LocalDate.now().minusDays(2).toString(), 5, TagType.SAVED),
                new Tag(2, "Test Four", LocalDate.now().minusDays(5).toString(), 5, TagType.STATIC),
                new Tag(1, "Test Five", LocalDate.now().minusDays(2).toString(), 5, TagType.SAVED),
                new Tag(2, "Test Six", LocalDate.now().minusDays(5).toString(), 5, TagType.STATIC)
        );
        // mocking the class to return a list of tags when invoked
        Mockito.when(mailChimpClient.listTags(mailChimpListId))
                .thenReturn(tags);

        // actual invocation
        final List<Tag> result = fooService.getSegmentByTypeSaved(mailChimpListId);

        // assertions
        // assert the actual size of the result
        Assertions.assertNotNull(result);
        Assertions.assertEquals(3, result.size());
        // assertions related to the actual content of the result
        final Set<TagType> typeSet = result.stream().map(Tag::getType).collect(Collectors.toSet());

        Assertions.assertEquals(1, typeSet.size());
        Assertions.assertEquals(TagType.SAVED, typeSet.iterator().next());

        Mockito.verify(mailChimpClient, Mockito.times(1)).listTags(eq(mailChimpListId));
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=33913&siteId=1