This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Unit testing Array

 
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

Wile running Jacoco , i found the highlighted line without code coverage. I want to know how can i write a test case to test it. It is an array, representing the index of the pojo fields where code will save the result of select query.

public class  MyClass {
.
.
.
.
private static final String SELECT = "SELECT ack_num, testform_detl_id, uniqueno, assment_year, ver_name FROM test_form_detl";
private static final int[] SELECT_IDX = { 32, 34, 35, 36, 37 };
.
.
.
.
.
}



Sorry to use quotes to highlight the red text.

Regards,
Raj
 
Marshal
Posts: 14388
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it is a field declaration, it's not something that any test code can execute directly. So even if you could write a test that asserts something about the array, I doubt it would be reported as being "covered" by a test. The fact that it is a static field declaration guarantees that it wil be executed when that class is loaded.

Are you trying to get 100% coverage? Don't. It's not worth the effort. You worrying about having test coverage for that line of code has already been a waste of time.

 
Junilu Lacar
Marshal
Posts: 14388
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

raj malhotra wrote:It is an array, representing the index of the pojo fields where code will save the result of select query.


Yikes, that is an incredibly poor design choice. Very brittle.
 
raj malhotra
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Since it is a field declaration, it's not something that any test code can execute directly. So even if you could write a test that asserts something about the array, I doubt it would be reported as being "covered" by a test. The fact that it is a static field declaration guarantees that it wil be executed when that class is loaded.

Are you trying to get 100% coverage? Don't. It's not worth the effort. You worrying about having test coverage for that line of code has already been a waste of time.



Yes I was trying for 100 % code coverage. I will try to convince my team members not to consider such cases. Thanks!!  

Regards,
Raj
 
Junilu Lacar
Marshal
Posts: 14388
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Test coverage metrics should not be a goal, it should be a guideline. It's like looking out the window and checking the sky to decide if you should bring an umbrella or not. If I see test coverage of less than 50% then I start to worry about insufficient testing. If I see test coverage of 90% or more I start to worry about wasting time by focusing too much on coverage numbers.

Coverage pretty much is subject to the Pareto Principle (80-20 rule) in my experience. Spend 80% of your testing effort covering the 20% of your code that is used 80% of the time. Focus on the code that has interesting behavior. That would be code in methods that have critical functionality. Plain old getters and setters are not that interesting.
 
Junilu Lacar
Marshal
Posts: 14388
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of spending effort trying to cover code like that, your time would be put to better use trying to address the brittle design that line of code represents. The brittleness of the design in that line is so much more of a risk than not having test coverage on it.

It's like worrying about the size of your umbrella while ignoring the big hole in the bottom of the boat that you're on.
 
author & internet detective
Posts: 39624
784
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj,
How does the code get used in Java code? From your example, it looks like unused code which probably isn't the intent.
 
raj malhotra
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Raj,
How does the code get used in Java code? From your example, it looks like unused code which probably isn't the intent.



Hi Jeanne,
This class is auto generated by a proprietary framework. During build, the framework read a UI Form document file  and emit such configuration java file for it.

At run time  based on the request payload, framework consult this Configuration class ,  create the Form specific Java Bean and perform the operations like validations, persistence etc. During db fetch, Framework will use this indexes and store the select query result  into the Java bean. Form Java bean has an array to store the data. Each index is fixed and represent a specific field as per document. It is also used in creating a response payload.

Regards,
Raj
 
Junilu Lacar
Marshal
Posts: 14388
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the form is autogenerated, then I what kind of test were thinking about writing anyway that wouldn't be at high risk of breaking when the class is regenerated?
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!