Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package demonstrate;
- import mockit.Delegate;
- import mockit.Mocked;
- import mockit.NonStrictExpectations;
- import mockit.StrictExpectations;
- import org.hamcrest.BaseMatcher;
- import org.hamcrest.Description;
- import org.junit.Before;
- import org.junit.Test;
- public class SomeTest {
- @Mocked
- Factory factory;
- @Mocked
- Consumer consumer;
- @Before
- public void setUp() {
- new NonStrictExpectations() {{
- factory.create(anyInt, (E) any);
- result = new Delegate<Factory>() {
- @SuppressWarnings("unused")
- public X create(int i, E e) {
- return new X(i, e);
- }
- };
- }};
- }
- @Test
- public void testWithMatcher() {
- System.err.println("-----------------------------------------");
- new StrictExpectations() {{
- consumer.consume(withArgThat(new BaseMatcher<X>() {
- final int i = 2; final E e = E.B;
- @Override
- public boolean matches(Object item) {
- return runChecks((X) item, i, e);
- }
- @Override
- public void describeTo(Description description) {
- description.appendText("\"i=" + i + ", e=" + e.name() + "\"");
- }
- }));
- }};
- new Service(factory, consumer).interact(2, E.B);
- }
- @Test
- public void testWithDelegate() {
- System.err.println("-----------------------------------------");
- new StrictExpectations() {{
- consumer.consume(with(new Delegate<X>() {
- @SuppressWarnings("unused")
- public boolean check(X x) {
- return runChecks(x, 2, E.B);
- }
- }));
- }};
- new Service(factory, consumer).interact(2, E.B);
- }
- private static boolean runChecks(X actual, int i, E e) {
- System.err.println("RUNNING CHECKS ON: " + actual);
- if (actual.getI() != i) {
- System.err.println("Primitive int mismatch!");
- return false;
- }
- if (actual.e == e) {
- System.err.println("Direct reference match.");
- } else {
- System.err.println("Reference mismatch!");
- return false;
- }
- E otherE = actual.getE();
- if (otherE != e) {
- System.err.println("Reference through getter mismatch!");
- System.err.println("Ordinal through getter reference: "
- + otherE.ordinal());
- System.err.println("Identity hashcode from getter reference: "
- + System.identityHashCode(otherE));
- System.err.println("Identity hashcode of expected reference: "
- + System.identityHashCode(e));
- return false;
- }
- return true;
- }
- public enum E {
- A, B
- }
- public static class X {
- private final int i;
- public final E e;
- public X(int i, E e) {
- this.i = i;
- this.e = e;
- }
- @Override
- public String toString() {
- return "i=" + i + ", e=" + e.name();
- }
- public int getI() {
- return i;
- }
- public E getE() {
- return e;
- }
- }
- public static class Factory {
- public X create(int i, E e) {
- return new X(i, e);
- }
- }
- public static class Consumer {
- public void consume(X arg) {
- }
- }
- public static class Service {
- private final Factory f;
- private final Consumer c;
- public Service(Factory f, Consumer c) {
- super();
- this.f = f;
- this.c = c;
- }
- public void interact(int i, E e) {
- X v = f.create(i, e);
- c.consume(v);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment