#Testing in springboot

1 messages · Page 1 of 1 (latest)

turbid garden
#

Hello, i want to test my api-controller using unit tests in junit but i cant seem to understand which is the correct way.

Different approaches:

  1. Mock the behaviour of the controller and call the controller-method directly. Asserting using assertequals().

  2. Call the endpoint using mockMvc? Asserting using andexpect().

I was thinking that maybe nr2 approach can be covered using api-tests in postman? Can anyone point me in the right direction? 🙂

Thanks in advance!

torpid blazeBOT
#

<@&987246399047479336> please have a look, thanks.

torpid blazeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

glad verge
#

My 2cents is, when you want to test controller methods, i think by using mockMvc you have more control on how http request will be constructed and see if is everything working as expected

rare yew
#

I mean, mockMvc is not really a unit test

#

It's more of an integration test in regards to how it is being set up and ran

#

A true unit test should be able to isolate the method you are calling

#

In a mockMvc, you are not only calling the controller class, but also the service class and so on. (this is usually slower since you need to start the entire application context)

#

You are correct also, postman is a good sub for your second part. It's extremely powerful.

#

For example

#
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response is correct", function () {
    const responseBody = pm.response.json();
    pm.expect(responseBody).to.have.keys('pdUsers','tcUsers','totalUsers')
});

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
#

Is all postman really needs

#

However, in an integration test via mockMvc, it is more complex and requires more code

#

However that is not to say mockMvc is useless. When you are using postman, you are executing the code in the real server. This includes sending data into the real database

#

In mockMvc, you have flexibility, and can simply use an in memory database for test cases

#

I think it comes down to the project specifics and what exactly you need to test.

#

also postman uses javascript YUCK