Solved

unit test vs junit tests

Posted on 2013-12-17
7
569 Views
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
0
Comment
Question by:gudii9
  • 5
7 Comments
 
LVL 11

Expert Comment

by:Angelp1ay
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.
0
 
LVL 11

Expert Comment

by:Angelp1ay
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.
0
 
LVL 7

Author Comment

by:gudii9
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
0
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

by:Angelp1ay
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.
0
 
LVL 35

Assisted Solution

by:mccarl
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.
0
 
LVL 11

Expert Comment

by:Angelp1ay
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):
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

0
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 250 total points
ID: 39725734
Arrange-Act-Assert
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.

Links
- Benefits of Unit Testing:
http://sd.jtimothyking.com/2006/07/11/twelve-benefits-of-writing-unit-tests-first/
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:
http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

831 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