hamcrest自定义BaseMatcher

阅读更多
import com.google.common.collect.Lists;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2015/3/29.
 */
public class RandomDigits {

    public static final int DigitLength = 4;

    public String generate() {
        List numbers = Lists.newArrayList();
        int index = DigitLength;
        while (index > 0) {
            String number = String.valueOf(RandomUtils.nextInt(10));
            if (!numbers.contains(number)) {
                numbers.add(String.valueOf(number));
                index--;
            }
        }
        return StringUtils.join(numbers, "");
    }
}

import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;


/**
 * Created by Administrator on 2015/3/29.
 */
public class RandomDigitsTest {
    @Test
    public void should_return_four_digits_when_generate() throws Exception {

        RandomDigits randomDigits = new RandomDigits();

        assertThat(randomDigits.generate(), is(NumberMatcher.matcher()));
    }

    @Test
    public void should_return_four_different_digits_when_generate() throws Exception {

        RandomDigits randomDigits = new RandomDigits();

        assertThat(randomDigits.generate(), is(DifferentNumberMatch.matcher()));
    }
    @Test
    public void should_return_random_digits_when_generate() throws Exception {

        RandomDigits randomDigits = new RandomDigits();

        String firstRandomDigits = randomDigits.generate();
        String secondRandomDigits = randomDigits.generate();

        assertThat(StringUtils.equals(firstRandomDigits,secondRandomDigits), is(false));
    }
}

 

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/3/29.
 */
public class NumberMatcher extends BaseMatcher {

    public String number;

    public static Matcher matcher()
    {
        return new NumberMatcher();
    }
    @Override
    public boolean matches(Object number) {
        this.number = (String) number;
        Pattern pattern = Pattern.compile("^\\d{4}$");
        return pattern.matcher(this.number).find();
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(number + " not match '^\\d{4}$'");
    }
}

 

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

/**
 * Created by Administrator on 2015/3/29.
 */
public class DifferentNumberMatch extends BaseMatcher {

    private String number;

    public static BaseMatcher matcher()
    {
        return new DifferentNumberMatch();
    }
    @Override
    public boolean matches(Object o) {

        number = (String) o.toString();

        return Sets.newHashSet(Lists.charactersOf(number)).size() == number.length();
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(this.number + " has same digit.");
    }
}

 

  • digitgame.rar (3.4 KB)
  • 下载次数: 0

你可能感兴趣的:(单元测试,hamcrest,BaseMatcher)