I try to write JUnit integration test and to test whether service method works well. It isn't.
This is the service class:
107 Replies
⌛
This post has been reserved for your question.
Hey @Tomasm21! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
This is the test:
Console output:
Am I doing anything wrong in the test? Or it cannot be tested like this?
The application works well. When requesting from the frontend with the specific
captchaResponse
string the method returns true
.
But I want to test this method like a JUnit test.
It should assert to true, but it is false. Why? I though mocked rest template already set the response.
Looks like the service method is invoked for real. Should it be like this?you are not injecting resttemplate in your service class
Isn't injected via constructor?
yes but your constructor takes in a builer
you do not have a builder
The builder is imported
import org.springframework.boot.web.client.RestTemplateBuilder;
I don't know its implementationchange the constructor to resttemplate instead
You mean:
two constructors?
no only one
or to remove the one that us using the builder?
yes modify to take in resttemplate
The same:
ahh
lol
Now I get the exception:
Should I add the bean to configuration?
the rest tamplate
lets see
Same:
In order the application to run I had to add the bean to configuration class:
From frontend the reCapture works:
Maybe the service method cannot be tested like this?
It is calling to the public URL for real.
But in mock case it shouldn't.
Strange
its because you are not injecting mock
you need to inject mock for it to work
JUnit testing is not my strong side. I do it rarely.
Ok now I get error:
I guess i should provide the secret
I should add more things
What are those stubbings? The parameters for
restTemplate.postForObject(
? I have empty string and null for the second and third parameters? And it asks that I would give right type parameters and not nulls?
I will try it.@MockitoSettings(strictness = Strictness.LENIENT)
try adding this annotation to your test class
ok.. hold on
You mean like this:
?
yes
It's already
2024-09-13 19:55:25.955 INFO 10884 --- [ main] it.akademija.recaptcha.ValidateCaptcha : RestTemplate instance: restTemplate
earlier it was: 2024-09-13 18:32:18.109 INFO 1392 --- [ main] it.akademija.recaptcha.ValidateCaptcha : RestTemplate instance: org.springframework.web.client.RestTemplate@2db0dd19
wait let me try to reproduce it at my end
Should I give you the project?
Ok. will wait
can you provide me import for eq here?
Or do you need CaptchaResponse class?
this
this, - I understand like the import string
import static org.mockito.ArgumentMatchers.eq;
. Okok so this works
without SpringBootTest... I thought so..
I removed spring crap and just manually added mock resttemplate
ok, testing
No. it's still false:
add a breakpoint
and check value for apiResponse
I never debuugged. teach me. I added the break point and launcged the test in debugging mode. Howered I don't know where to look for the apiResponse value:
there should be a variables window somewhere in right
that panel shows all variables in that
it's null
hmm i see, expand
this
and see id for resttemplateI run it once more when showing latest image
any(MultiValueMap.class)
, change this to any(Object.class)
It was already Object:
I will change to MultiValueMap.class
ok?
yes remove
@MockitoSettings(strictness = Strictness.LENIENT)
as well
we need those errorsI will test it in debug mode like this now:
stubbing
I guess I needto provide URL string, Multivalue map parameters
do you have
equals
and hashcode
in CaptchaResponse
?No.
implement and try
generated:
trying
Same:
is your
recaptchaEndpoint
null?
it should be string
it is, thats the problem...bruhchecking.. wait a moment
I thing it is that first parameter:
the anyString().
nope, your service class
you are injecting it
and its null
just set some value temporarily
and then test
during runtime it is taken from environment:
in application.properties it is:
google.recaptcha.verification.endpoint=https://www.google.com/recaptcha/api/siteverify
yes but we dont have spring here, so it is just null
thats causing the issue
So how to set it up?
always constructor inject
just like resttemplate
IS this ok:
yep
ok will test
and it test I need to write it too
what?
in test class, due to constructor change I have to add the second parameter
Right?
yes... and
well run and check
ok..
thats whats tests are for
same error
damn it
again we removed spring that @value wont work
what do you put there instead?
I will put the actual URL
the
"https://www.google.com/recaptcha/api/siteverify"
just think...
any string will work
we dont invoke that url at all
but yeah actual url will also work
Debug:
resume
And test:
🙂
Thanks
finally, lol
Is the test correct without launchig the Spring Boot container
you tell me, does it test business logic?
that you intended to test?
I have also successful controller test, So I reckon that yes
this one works
btw you can start to add spring back now
what?
yeah we got the root issue for why mockito was'nt working right the url was null
yea
now you can just add spring back
fix url issue
you mea
@SpringBootTest
?yeah
ok.. hold on
Stack Overflow
Populating Spring @Value during Unit Test
I'm trying to write a Unit Test for a simple bean that's used in my program to validate forms. The bean is annotated with @Component and has a class variable that is initialized using
@Value("${t...
Thank you, buddy.
you're welcome
Post Closed
This post has been closed by <@312509109863710732>.