Skip to content
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

@Mock doesn't work when using nima test #14

Closed
SentryMan opened this issue Sep 27, 2023 · 5 comments
Closed

@Mock doesn't work when using nima test #14

SentryMan opened this issue Sep 27, 2023 · 5 comments

Comments

@SentryMan
Copy link
Collaborator

Got an actual problem this time, when using @Mock and injecting an Http Client the client is injected into the test class but the
mocked bean does not replace the one in the scope.

@SentryMan
Copy link
Collaborator Author

hmm, would this be an inject bug or an avaje nima bug?

@SentryMan
Copy link
Collaborator Author

once again, the error is mine, it seems I get it working if I don't add static when injecting the client

@rob-bygrave
Copy link
Contributor

it seems I get it working if I don't add static when injecting the client

This does not make sense to me in that both static and non-static/instance are valid scopes.

Did this test use @Mock or @Inject? It could be that I'm assuming HttpClient is used with @Inject and maybe that is confusing ?

@SentryMan
Copy link
Collaborator Author

Did this test use @mock or @Inject? It could be that I'm assuming HttpClient is used with @Inject and maybe that is confusing ?

looked like this:

@InjectTest
TestyMcTest {
 @Inject static HttpClient client;
 @Mock Service mocky; //<- doesn't get added to beanscope
}

@rob-bygrave
Copy link
Contributor

rob-bygrave commented Sep 27, 2023

@mock Service mocky; //<- doesn't get added to beanscope

Right, so the HttpClient is static scope ... and that means that the http server that it will hit/access is also "wired" to the static scope.

So the @Mock Service mocky will be injected as a mock into the instance scope ... but IF the test is using the httpClient, that httpClient will hit the static scoped server instance which will NOT include that mock.

If it was the other way around [in terms of static scope and instance scope] like:

 @Inject HttpClient client;
 static @Mock Service mocky;

Or the same scope [both static or both instance scope] ... then tests can use both.

doesn't get added to beanscope

This statement is NOT correct. It is getting added to a beanScope ... it's that there are MULTIPLE BeanScopes (the static one and the instance one). The static scoped HttpClient will hit the static scoped Helidon instance (and that instance is created once at static class beforeAll time ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants