Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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 https://code.google.com/p/mockito/) 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 165
difference between sorce folder and folder in eclipise 3 28
Chrome and Firefox Java 5 49
hashmap order 17 36
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

789 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