Solved

unit test vs junit tests

Posted on 2013-12-17
7
566 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 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.

760 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

21 Experts available now in Live!

Get 1:1 Help Now