Avatar of gudii9
gudii9
Flag for United States of America asked on

unit test vs junit tests

what are unit tests. How we need them being a java developer. How is it similar or different or related to junit. I have a search feature where i search on vendor name XYZ and in the next page i get 3 rows of the vendor related information in the table rows including vendor address, phone number, vendor country etc.

Does this scenario comes under the unit testing scope.


please advise
Any links resources ideas highly appreciated. Thanks in advance
Java EEJavaJSP

Avatar of undefined
Last Comment
Angelp1ay

8/22/2022 - Mon
Angelp1ay

Unit tests are tests applied to a unit.
A unit is a small independent section of your code that you can test independently.

jUnit is a test framework with which you can write and execute unit tests.

If your feature is implemented by a single or limited number of classes and you can isolate it from other dependencies (e.g. by injecting mocks, see https://code.google.com/p/mockito/) you can and should write unit tests for it.
Angelp1ay

It sounds like you're talking about a web page which executes a search, likely going all the way back to a database. It's likely your implementation is mostly executing in the db. jUnit is really more for testing code logic e.g. a java library method for converting or processing something.
gudii9

ASKER
It sounds like you're talking about a web page which executes a search, likely going all the way back to a database. It's likely your implementation is mostly executing in the db

That is correct.

Cannot I use JUNIT in this scenario. please advise
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Angelp1ay

Not easily. jUnit is really best for testing small modules of code.

If you can bypass the UI then maybe but since I would imagine your search logic is mostly in your database (and hence you can't easily isolate the tests from whatever data is stored in your database) it's not an ideal scenario.
SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Angelp1ay

@mccarl - Agree.

I was more trying to say this specific scenario is not the best example to show junit's power.

For search the logic is likely outside of java code either as a db query or a service call to a proprietary black box. In the latter case any testing is very limited since you might not even know what the 'correct' behaviour is. In the former at the very least you need to create a mock db of known data, and even then the nature of search results is vague and the logic is likely to evolve so your test cases may be expensive to write and quickly obsolete or returning failure negatives.

Likewise at the other end of the stack, I've always found user interface testing has rather poor payback - writing Selenium tests is more work than your average unit test and more fragile.

If you do decide you want to proceed then I would imagine tests like these (depending on what you're searching):
SHOULD_RETURN_EXACT_TITLE_MATCHES()
SHOULD_RETURN_EXACT_UID_MATCHES()
SHOULD_MATCH_MIDDLE_OF_STRING
SHOULD_SEARCH_FIRST_AND_LAST_NAME

Open in new window

ASKER CERTIFIED SOLUTION
Angelp1ay

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.