-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow mocks to be produced from fields (#567)
- Loading branch information
1 parent
42a4d7c
commit 2c6b1d1
Showing
6 changed files
with
156 additions
and
1 deletion.
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
40 changes: 40 additions & 0 deletions
40
test-junit5/src/test/java/io/micronaut/test/junit5/MathFieldMockServiceTest.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 io.micronaut.test.junit5; | ||
|
||
import io.micronaut.test.annotation.MockBean; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@MicronautTest | ||
@MockitoEnabled | ||
class MathFieldMockServiceTest { | ||
|
||
@MockBean(MathServiceImpl.class) // <1> | ||
MathService mock = mock(MathService.class); // <2> | ||
|
||
@Inject | ||
MathService mathService; | ||
|
||
@ParameterizedTest | ||
@CsvSource({"2,4", "3,9"}) | ||
void testComputeNumToSquare(Integer num, Integer square) { | ||
|
||
when(mathService.compute(10)) | ||
.then(invocation -> Long.valueOf(Math.round(Math.pow(num, 2))).intValue()); | ||
|
||
final Integer result = mathService.compute(10); | ||
|
||
Assertions.assertEquals( | ||
square, | ||
result | ||
); | ||
verify(mathService).compute(10); // <4> | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
test-junit5/src/test/java/io/micronaut/test/junit5/MathMockFieldCollaboratorTest.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,45 @@ | ||
package io.micronaut.test.junit5; | ||
|
||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.client.HttpClient; | ||
import io.micronaut.http.client.annotation.Client; | ||
import io.micronaut.test.annotation.MockBean; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@MicronautTest | ||
@MockitoEnabled | ||
class MathMockFieldCollaboratorTest { | ||
|
||
@MockBean(MathServiceImpl.class) // <1> | ||
MathService mathService = mock(MathService.class); | ||
|
||
@Inject | ||
@Client("/") | ||
HttpClient client; // <2> | ||
|
||
|
||
@ParameterizedTest | ||
@CsvSource({"2,4", "3,9"}) | ||
void testComputeNumToSquare(Integer num, Integer square) { | ||
|
||
when( mathService.compute(num) ) | ||
.then(invocation -> Long.valueOf(Math.round(Math.pow(num, 2))).intValue()); | ||
|
||
final Integer result = client.toBlocking().retrieve(HttpRequest.GET("/math/compute/" + num), Integer.class); // <3> | ||
|
||
assertEquals( | ||
square, | ||
result | ||
); | ||
verify(mathService).compute(num); // <4> | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
test-junit5/src/test/java/io/micronaut/test/junit5/MockitoCondition.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,18 @@ | ||
package io.micronaut.test.junit5; | ||
|
||
import org.junit.jupiter.api.extension.ConditionEvaluationResult; | ||
import org.junit.jupiter.api.extension.ExecutionCondition; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
public class MockitoCondition implements ExecutionCondition { | ||
static final String MOCKITO_ENABLED = "mockito.test.enabled"; | ||
@Override | ||
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { | ||
boolean isEnabled = Boolean.getBoolean(MOCKITO_ENABLED); | ||
if (isEnabled) { | ||
return ConditionEvaluationResult.enabled("Mockito enabled"); | ||
} else { | ||
return ConditionEvaluationResult.disabled("Mockito disabled"); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test-junit5/src/test/java/io/micronaut/test/junit5/MockitoEnabled.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,16 @@ | ||
package io.micronaut.test.junit5; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@ExtendWith(MockitoCondition.class) | ||
public @interface MockitoEnabled { | ||
} |
36 changes: 36 additions & 0 deletions
36
test-spock/src/test/groovy/io/micronaut/test/spock/MathFieldCollaboratorSpec.groovy
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,36 @@ | ||
package io.micronaut.test.spock | ||
|
||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.test.annotation.MockBean | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
@MicronautTest | ||
class MathFieldCollaboratorSpec extends Specification { | ||
|
||
@MockBean(MathServiceImpl) // <1> | ||
MathService mathService = Mock(MathService) | ||
|
||
@Inject | ||
@Client('/') | ||
HttpClient client // <3> | ||
|
||
@Unroll | ||
void "should compute #num to #square"() { | ||
when: | ||
Integer result = client.toBlocking().retrieve(HttpRequest.GET('/math/compute/10'), Integer) // <3> | ||
|
||
then: | ||
1 * mathService.compute(10) >> Math.pow(num, 2) // <4> | ||
result == square | ||
|
||
where: | ||
num | square | ||
2 | 4 | ||
3 | 9 | ||
} | ||
} |