Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


unit test vs junit tests

Posted on 2013-12-17
Medium Priority
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
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
  • 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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 36

Assisted Solution

mccarl earned 1000 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 1000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

618 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