unit test vs junit tests

Posted on 2013-12-17
Last Modified: 2013-12-24
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
Question by:gudii9
  • 5
LVL 11

Expert Comment

ID: 39724612
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 you can and should write unit tests for it.
LVL 11

Expert Comment

ID: 39724625
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.

Author Comment

ID: 39724982
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

LVL 11

Expert Comment

ID: 39724998
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.
LVL 35

Assisted Solution

mccarl earned 250 total points
ID: 39725482
While mostly agree with @Angelp1ay, there a few points that I would argue.

Firstly, I agree with...

- "Unit testing" is for testing small units of code, typically only one class at a time (maybe more than one in special circumstances)

- What you are looking at testing, would NOT be called unit testing. Typically that would be called "Integration testing" and it occurs at a slightly different stage than unit testing. Generally unit tests are run whenever the code is compiled on the developers machine, and as such they should be short/quick to run, ie. they don't interact with databases, or other external systems, they just test code. Integration testing usually happens much less frequently, like when the developer manually runs the tests, or often when code gets checked in to a Source Code Control system, then a separate Build server runs the integration tests automatically. These tests can involve interactions with databases, external systems, web browsers, etc and so generally take a lot longer to run.

What I don't agree with...

- That JUnit is ONLY used for Unit testing. While yes, JUnit started off as a framework for writing unit tests (hence it's name) it is generally used for writing/executing tests at any stage of development and testing. JUnit is what currently executes all our integration testing in my current project. Yes, we have added some utilities on top of JUnit to simplify things but JUnit is still the foundation. Consider an often used product for Integration testing, Selenium, it also can be used on top of JUnit (amongst a number of others). JUnit still manages the execution of the tests, the assertions that the developer writes and the reporting of the results.
LVL 11

Expert Comment

ID: 39725730
@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):

Open in new window

LVL 11

Accepted Solution

Angelp1ay earned 250 total points
ID: 39725734
I would also suggest using the below template for unit test code. The idea being you structure your tests into 3 blocks; one doing any needed setup, one doing the action you're testing, and one checking the results are as you expect.
// Arrange
// Act
// Assert

Open in new window

Test Driven Development (TDD)
The other thing I would suggest is TDD, where you write tests upfront of writing code. Your tests form quite a nice definition of the expected behaviour. It should also form a highly readable description of the intended behaviour for other developers to read.

Typically I am not so strict in following TDD, but instead I tend to first just write down the titles of most of my tests. It gets your brain thinking about what the code should do first before you move on to how and can lead to better thought out code strategy/architecture.

I tend to iterate a little, first listing out a set of tests I will write, then writing the basics of the implementation, then implementing a few tests, moving back and forth adding tests for more edge cases and upgrading my implementation as required.

- Benefits of Unit Testing:
1) Unit tests prove that your code actually works
2) You get a low-level regression-test suite
3) You can improve the design without breaking it
4) It's more fun to code with them than without
5) They demonstrate concrete progress
6) Unit tests are a form of sample code
7) It forces you to plan before you code
8) It reduces the cost of bugs
9) It's even better than code inspections
10) It virtually eliminates coder's block
11) Unit tests make better designs
12) It's faster than writing code without tests

- Best and Worst Practices:

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
Requested array size exceeds VM limit 3 84
Strange loading of website behaviour 3 31
javap not working 8 36
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now