如何为JUnit测试测试Comparator? [英] How to test Comparator for JUnit test?

查看:67
本文介绍了如何为JUnit测试测试Comparator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试compare()方法,而我对此却感到困惑.我可以看看该怎么做吗?

I need to test the compare() method and i am confused on how. Can I see how to do this?

public class MemberComparator implements Comparator<Member> {

    private final String clientId;

    public MemberComparator(String clientId) {
        this.clientId = clientId;
    }

    @Override
    public int compare(Member m1, Member m2) {
        if (m1.getClientId().startsWith(clientId)) {
            return m2.getClientId().startsWith(clientId) ? m1.getClientId().compareTo(m2.getClientId())
                    : -1;
        } else {
            return m2.getClientId().startsWith(clientId) ? 1
                    : m1.getClientId().compareTo(m2.getClientId());
        }
    }

}

到目前为止,这是我在测试中创建的.我该如何工作?假设我不更改MemberComparator类的当前方法,我应该怎么做才行.

So far, this is what i have created in my test. How can i get this to work? What should i do as this way isn't working, assuming i do not change the current approach to MemberComparator class.

class MemberComparatorTest {

//private MemberComparator caseID_test;
//private final MemberComparator memberComparator = new MemberComparator("jake");

@Test
void testEqual() {
    Member m1 = new Member();
    Member m2 = new Member();
    int result = memberComparator.compare(m1,m2);
    //assertTrue("expected to be equal", result == 0);

}

}

推荐答案

编写Comparator时需要回答两个问题:

There are two questions you'd need to answer when writing a Comparator:

  1. 是否满足Comparator界面的要求?
  2. 它能满足您的需求吗?
  1. Does it meet the requirements of the Comparator interface?
  2. Does it do what you need?

第一个很难编写测试;我认为最好用已知的好"的东西来写东西.模式,以帮助您从代码中推断出其正确性.

The first one is reasonably hard to write tests for; I'd argue it's better to write something using a "known good" pattern, in order to help you reason about its correctness from the code.

Comparator的情况下,已知商品"是已知商品".模式是您从链接方法中看到的:

In the case of Comparator, the "known good" pattern is something that you see from the chaining methods:

// This boolean comparison may be the wrong way round, I never remember which way is which. If so, reverse it.
Comparator.comparing(m -> m.getClientId().startsWith(clientId))
    .thenComparing(Member::getClientId)

您比较第一件事,如果不同则返回.您比较第二件事,如果它们不同则返回,等等.

You compare the first thing, and return if they're different; you compare the second thing, and return if they're different, etc.

如果您使用的是Java 8+,则可以使用上面的代码:完全可以使用Comparator.如果由于某种原因您不能使用此方法,则您的compare方法可以重写为:

You could use the code above if you're using Java 8+: this gives a totally fine Comparator. If you can't use this (for whatever reason), your compare method could be rewritten as:

int compareSw = Boolean.compare(m1.getClientId().startsWith(clientId), m2.getClientId().startsWith(clientId));
if (compareSw != 0) {
  return compareSw;
}
return m1.getClientId().compareTo(m2.getClientId());

因为这遵循了已知商品"的规定,模式,通过检查可以看出它符合Comparator界面的要求.

Because this follows the "known good" pattern, this can be seen to meet the requirements of the Comparator interface by inspection.

(当然,您必须谨慎思考对我看起来好!",因为您可能会错过一些东西,例如两次使用m1.getClientId()而不是同时使用m1.getClientId()m2.getClientId().)缺陷应通过以下充分的测试来发现).

(Of course, you have to be careful about thinking "Looks Good To Me!", because you might miss something, e.g. using m1.getClientId() twice instead of both m1.getClientId() and m2.getClientId(). But that sort of defect would be caught by sufficient testing of the following).

然后只需要进行测试即可确保它符合您的要求:也就是说,如果您带了两个Member,此Comparator是否按照您想要的方式对其进行排序?这是编写一个简单的测试,而不会陷入Comparator的细节中.

Then it's just a matter of testing to make sure it does what you want: that is, if you take two Members, does this Comparator order them the way you want? That's an easy test to write, without getting bogged down in the details of the Comparator.

List<Member> members = List.of(m1, m2);
assertEquals(m1, Collections.min(members, comparator));  // For example.
assertEquals(m2, Collections.max(members, comparator));  // For example.

这篇关于如何为JUnit测试测试Comparator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆