-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of testing classes using mocks of Mockito / JMock libs
Moved from repo: https://github.com/andrei-punko/java-interview-coding
- Loading branch information
1 parent
1c50d51
commit 9f0cf2a
Showing
6 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
common/src/main/java/by/andd3dfx/testing/ClassThatShouldBeTested.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package by.andd3dfx.testing; | ||
|
||
public class ClassThatShouldBeTested { | ||
|
||
RealClass realClass; | ||
|
||
public int someMethod(int x, int y) { | ||
return realClass.someMethod(x, y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package by.andd3dfx.testing; | ||
|
||
public class RealClass { | ||
|
||
public int someMethod(int x, int y) { | ||
return 0; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
common/src/test/java/by/andd3dfx/testing/ClassThatShouldBeTestedUsingJMockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package by.andd3dfx.testing; | ||
|
||
import org.jmock.Expectations; | ||
import org.jmock.Mockery; | ||
import org.jmock.imposters.ByteBuddyClassImposteriser; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class ClassThatShouldBeTestedUsingJMockTest { | ||
|
||
/** | ||
* To mock real class, not interface, we need to set imposteriser into mockery | ||
*/ | ||
private Mockery mockery = new Mockery() {{ | ||
setImposteriser(ByteBuddyClassImposteriser.INSTANCE); | ||
}}; | ||
private ClassThatShouldBeTested classThatShouldBeTested; | ||
|
||
@Before | ||
public void setup() { | ||
classThatShouldBeTested = new ClassThatShouldBeTested(); | ||
classThatShouldBeTested.realClass = mockery.mock(RealClass.class); | ||
} | ||
|
||
@Test | ||
public void someMethod() { | ||
mockery.checking(new Expectations() { | ||
{ | ||
oneOf(classThatShouldBeTested.realClass).someMethod(2, 5); | ||
will(returnValue(3)); | ||
} | ||
}); | ||
int result = classThatShouldBeTested.someMethod(2, 5); | ||
|
||
assertThat("Wrong result", result, is(3)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
common/src/test/java/by/andd3dfx/testing/ClassThatShouldBeTestedUsingMockitoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package by.andd3dfx.testing; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ClassThatShouldBeTestedUsingMockitoTest { | ||
|
||
@InjectMocks | ||
ClassThatShouldBeTested classThatShouldBeTested; | ||
@Mock | ||
RealClass realClassMock; | ||
|
||
@Test | ||
public void someMethod() { | ||
when(realClassMock.someMethod(2, 5)).thenReturn(3); | ||
|
||
int result = classThatShouldBeTested.someMethod(2, 5); | ||
|
||
assertThat("Wrong result", result, is(3)); | ||
} | ||
} |