Skip to content

Commit

Permalink
Add example of testing classes using mocks of Mockito / JMock libs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Jun 10, 2024
1 parent 1c50d51 commit 9f0cf2a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- MapStruct mapper [example](common/src/main/java/by/andd3dfx/mapper)
- [Example](common/src/main/java/by/andd3dfx/masking) of annotation-driven masker
- [Example](common/src/main/java/by/andd3dfx/sockets) of work with sockets
- [Example](common/src/main/java/by/andd3dfx/testing) of testing classes using Mockito / JMock mocks


* **custom-spring-boot-starter**
Expand Down
12 changes: 12 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
<version>3.25.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-legacy</artifactId>
<version>2.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-imposters</artifactId>
<version>2.12.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jasypt</groupId>
Expand Down
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);
}
}
8 changes: 8 additions & 0 deletions common/src/main/java/by/andd3dfx/testing/RealClass.java
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;
}
}
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));
}
}
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));
}
}

0 comments on commit 9f0cf2a

Please sign in to comment.