From 34414b2a43054aeabfc2e57acd35dd214fbc124f Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 20 Jul 2016 19:17:08 +0300 Subject: [PATCH] Refactor JMockit examples --- .../baeldung/mocks/jmockit/Collaborator.java | 2 + .../org/baeldung/mocks/jmockit/Model.java | 4 +- .../mocks/jmockit/ExpectationsTest.java | 186 ++++++++---------- .../baeldung/mocks/jmockit/PerformerTest.java | 2 + 4 files changed, 90 insertions(+), 104 deletions(-) diff --git a/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Collaborator.java b/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Collaborator.java index ef271b9aff..60da12fa7c 100644 --- a/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Collaborator.java +++ b/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Collaborator.java @@ -1,9 +1,11 @@ package org.baeldung.mocks.jmockit; public class Collaborator { + public boolean collaborate(String string){ return false; } + public void receive(boolean bool){ //NOOP } diff --git a/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Model.java b/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Model.java index c3b63d11b4..79ae24c2f5 100644 --- a/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Model.java +++ b/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Model.java @@ -1,7 +1,7 @@ package org.baeldung.mocks.jmockit; public class Model { - public String getInfo(){ - return "info"; + public String getInfo() { + return "info"; } } diff --git a/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/ExpectationsTest.java b/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/ExpectationsTest.java index 239bd814cd..1c72647133 100644 --- a/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/ExpectationsTest.java +++ b/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/ExpectationsTest.java @@ -1,21 +1,20 @@ package org.baeldung.mocks.jmockit; -import static org.junit.Assert.*; - -import java.util.ArrayList; -import java.util.List; - -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.junit.Test; -import org.junit.runner.RunWith; - import mockit.Delegate; import mockit.Expectations; import mockit.Mocked; import mockit.StrictExpectations; import mockit.Verifications; import mockit.integration.junit4.JMockit; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; @RunWith(JMockit.class) @SuppressWarnings("unchecked") @@ -23,119 +22,103 @@ public class ExpectationsTest { @Test public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception { - new Expectations() { - { - mock.methodForAny1(anyString, anyInt, anyBoolean); - result = "any"; - } - }; + new Expectations() {{ + mock.methodForAny1(anyString, anyInt, anyBoolean); + result = "any"; + }}; assertEquals("any", mock.methodForAny1("barfooxyz", 0, Boolean.FALSE)); mock.methodForAny2(2L, new ArrayList<>()); - new Verifications() { - { - mock.methodForAny2(anyLong, (List) any); - } - }; + new Verifications() {{ + mock.methodForAny2(anyLong, (List) any); + }}; } @Test public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception { - new Expectations() { - { - mock.methodForWith1(withSubstring("foo"), withNotEqual(1)); - result = "with"; - } - }; - + new Expectations() {{ + mock.methodForWith1(withSubstring("foo"), withNotEqual(1)); + result = "with"; + }}; + assertEquals("with", mock.methodForWith1("barfooxyz", 2)); mock.methodForWith2(Boolean.TRUE, new ArrayList<>()); - - new Verifications() { - { - mock.methodForWith2(withNotNull(), withInstanceOf(List.class)); - } - }; + + new Verifications() {{ + mock.methodForWith2(withNotNull(), withInstanceOf(List.class)); + }}; } @Test public void testWithNulls(@Mocked ExpectationsCollaborator mock) { - new Expectations() { - { - mock.methodForNulls1(anyString, null); - result = "null"; - } - }; - + new Expectations() {{ + mock.methodForNulls1(anyString, null); + result = "null"; + }}; + assertEquals("null", mock.methodForNulls1("blablabla", new ArrayList())); mock.methodForNulls2("blablabla", null); - - new Verifications() { - { - mock.methodForNulls2(anyString, (List) withNull()); - } - }; + + new Verifications() {{ + mock.methodForNulls2(anyString, (List) withNull()); + }}; } @Test public void testWithTimes(@Mocked ExpectationsCollaborator mock) { - new Expectations() { - { - mock.methodForTimes1(); times = 2; - mock.methodForTimes2(); - } - }; - + new Expectations() {{ + mock.methodForTimes1(); + times = 2; + mock.methodForTimes2(); + }}; + mock.methodForTimes1(); mock.methodForTimes1(); mock.methodForTimes2(); mock.methodForTimes3(); mock.methodForTimes3(); mock.methodForTimes3(); - - new Verifications() { - { - mock.methodForTimes3(); minTimes = 1; maxTimes = 3; - } - }; + + new Verifications() {{ + mock.methodForTimes3(); + minTimes = 1; + maxTimes = 3; + }}; } @Test public void testCustomArgumentMatching(@Mocked ExpectationsCollaborator mock) { - new Expectations() { - { - mock.methodForArgThat(withArgThat(new BaseMatcher() { - @Override - public boolean matches(Object item) { - return item instanceof Model && "info".equals(((Model) item).getInfo()); - } + new Expectations() {{ + mock.methodForArgThat(withArgThat(new BaseMatcher() { + @Override + public boolean matches(Object item) { + return item instanceof Model && "info".equals(((Model) item).getInfo()); + } - @Override - public void describeTo(Description description) { } - })); - } - }; + @Override + public void describeTo(Description description) { + } + })); + }}; mock.methodForArgThat(new Model()); } @Test public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) { - new StrictExpectations() { - { - mock.methodReturnsString(); - result = "foo"; - result = new Exception(); - result = "bar"; - mock.methodReturnsInt(); - result = new int[] { 1, 2, 3 }; - mock.methodReturnsString(); - returns("foo", "bar"); - mock.methodReturnsInt(); - result = 1; - } - }; - + new StrictExpectations() {{ + mock.methodReturnsString(); + result = "foo"; + result = new Exception(); + result = "bar"; + mock.methodReturnsInt(); + result = new int[]{1, 2, 3}; + mock.methodReturnsString(); + returns("foo", "bar"); + mock.methodReturnsInt(); + result = 1; + }}; + assertEquals("Should return foo", "foo", mock.methodReturnsString()); try { mock.methodReturnsString(); @@ -153,24 +136,23 @@ public class ExpectationsTest { @Test public void testDelegate(@Mocked ExpectationsCollaborator mock) { - new Expectations() { - { - mock.methodForDelegate(anyInt); - result = new Delegate() { - public int delegate(int i) throws Exception { - if (i < 3) { - return 5; - } else { - throw new Exception(); - } + new Expectations() {{ + mock.methodForDelegate(anyInt); + result = new Delegate() { + public int delegate(int i) throws Exception { + if (i < 3) { + return 5; + } else { + throw new Exception(); } - }; - } - }; - + } + }; + }}; + assertEquals("Should return 5", 5, mock.methodForDelegate(1)); try { mock.methodForDelegate(3); - } catch (Exception e) { } + } catch (Exception e) { + } } } diff --git a/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/PerformerTest.java b/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/PerformerTest.java index c99ae844c3..2b1b3be0f7 100644 --- a/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/PerformerTest.java +++ b/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/PerformerTest.java @@ -21,7 +21,9 @@ public class PerformerTest { model.getInfo();result = "bar"; collaborator.collaborate("bar"); result = true; }}; + performer.perform(model); + new Verifications() {{ collaborator.receive(true); }};