-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fail on @MockBean being used with class #1015
Comments
hi, sure. I forgot to share but this should be enough |
Oddly...this seems to pass for me 🤔 @MicronautTest
@Property(name = "spec.name", value = "MockBeanClassTest")
class MockBeanClassTest {
@Inject
MainService mainService;
@Inject
ServiceThatIsAClass service;
@MockBean(ServiceThatIsAClass.class)
ServiceThatIsAClass serviceThat() {
return Mockito.mock(ServiceThatIsAClass.class);
}
@Test
void testDoSomething() {
mainService.doSomething();
// verify that the method was called 1 time
Mockito.verify(service, Mockito.times(1)).doSomething();
}
@Singleton
@Requires(property = "spec.name", value = "MockBeanClassTest")
public static class MainService {
private final ServiceThatIsAClass service;
public MainService(ServiceThatIsAClass service) {
this.service = service;
}
void doSomething() {
service.doSomething();
}
}
@Singleton
@Requires(property = "spec.name", value = "MockBeanClassTest")
public static class ServiceThatIsAClass {
public void doSomething() {
System.out.println("Doing something");
}
}
} I'll try moving the classes to be non-internal and see if that breaks things 🤔 |
I wouldn't complain if you actually find out a solution for #535 instead 😅 |
I'll see if I can come up with a non-lombok reproducer for that 🤔 |
Your reproducer test passes if I make the method public 🤔 diff --git a/src/main/java/com/example/ServiceThatIsAClass.java b/src/main/java/com/example/ServiceThatIsAClass.java
index d59b498..bd923ef 100644
--- a/src/main/java/com/example/ServiceThatIsAClass.java
+++ b/src/main/java/com/example/ServiceThatIsAClass.java
@@ -5,7 +5,7 @@ import jakarta.inject.Singleton;
@Singleton
public class ServiceThatIsAClass {
- void doSomething() {
+ public void doSomething() {
System.out.println("Doing something");
}
|
ok, I have just tired to create Spock reproducer that is more close to our codebase but it still passes. I'll try to dig deeper to figure out what is the difference between the reproducer and our codebase |
ok, I was trying to make it fail and I think I found the common source off issues that is forgetting to add the value of the
if the mocked bean is an interface it fails with duplicate bean but with the bean being a class it executes silently and it never replaces the bean. Knowing this, would it be possible to make the |
Ahhhh... Ok, getting closer... When we add the class to the micronaut-test/test-core/src/main/java/io/micronaut/test/annotation/MockBean.java Lines 44 to 48 in 2c6b1d1
If we change your example to the following:
It works again... I think we should make the value of However this is a breaking change that would break applications where peiople are using it successfully with interfaces... It could be considered for Micronaut 5.0 though 🤔 |
Thanks @timyates! Under normal circumstances, the number of
If not possible to have errors in |
Feature description
At the moment, beans that are not injected by interface (e.g. they don't implement any interface) cannot be replaced using
@MockBean
(see #535). This is a commons source of frustration as there is no warning or failure from the compiler when someone do so. I suggest that whenever the@MockBean
is used on a field that is not an interface or a method that does not return an interface then a compilation error is being thrown to inform the developer that the@MockBean
annotation will have no effect.The text was updated successfully, but these errors were encountered: