• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

maven sonar how to create test class for unit testing using jacoco

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to learn how to use maven with Jacoco in sonar to create unit tests for my system also to use code coverage. this is for white box automated testing. I would like to emphasize that this is my first time using Maven, Jacoco, and Sonar. Thus, everything is all new to me. at this moment, I am still learning about pom.xml configuration as well. Now back to my main problem is that I've noticed that all maven projects have the main src and a test src. now the main src is the well the main src of the project or the real code while the test src is for JUnit testing. Now I would like to know is that is the test src also necessary for Jacoco testing? As I've read Jacoco and JUnit are different and that Jacoco will create its own testing results somewhere in the directory. So I'm a bit confused about this matter, and if it is really necessary how do I create my own test src since the examples provided are hard to understand in my current level of understanding of Maven. I do hope that someone would be able to point me in the right direction for this as I would really like to figure this out as quickly as possible. Also, I've read some blogs that stated if you want to conduct Jacoco testing you just have to edit the pom.xml file to integrate Jacoco in your project and that it would do the rest.

This is the link I'm following at the moment: sonar maven jacoco, I am also currently using their project as my basis and it worked fine when I executed every command, but now I am trying to use it on my system and I don't have any test source for my main source and that is my problem now.

I hope you can help put me in the right direction on this.

Thank you
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I sense some confusion here.
First off Jacoco is a code coverage tool: JAva COde COverage
You don't actually create unit tests in it.
It runs your existing JUnit tests and then tells you how much of your REAL code you exercised with your test code.

The setup you have sounds fine.

>Now I would like to know is that is the test src also necessary for Jacoco testing?
Yes, in that JUnit tests are necessary for running Jacoco.

So write your JUnit tests - it will give you a report of how many tests it ran, and did they pass or fail.
Put Jacoco into the pom file , and it will also spit you out a report on your code coverage.

Thats pretty much it - an extra report so you can judge how good/bad your code is.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:

So write your JUnit tests - it will give you a report of how many tests it ran, and did they pass or fail.
Put Jacoco into the pom file , and it will also spit you out a report on your code coverage.

Thats pretty much it - an extra report so you can judge how good/bad your code is.



Thank you for clarifying this to me. I would like to ask now is are there any automated tools that will create a test case based on my code? I do now that Netbeans provide this feature but it only creates a template based on my code and that I have to populate each function manually. As I do not know how to proceed with that I wonder if there are automated tools that can do that for me?

Thank you
 
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your question indicates a deeper problem to me. I may be wrong but by writing your tests after the fact, i.e., after you have written your production code, it seems to me that your focus is mainly on increasing the code coverage metric. Your question about an automated way to fill in the autogenerated test templates make me suspect that you just want to satisfy a requirement to have at least some minimum percentage of code coverage so you can say that you're done with this code and move on to work on the next thing. When you are in this kind of mindset, you are missing the point of writing unit tests and using coverage entirely. This is how a metric like code coverage is misused and abused.

The point is not to get that minimum test coverage metric. The point is to see if there are sections in your code that might be a source for bugs later on. Production code that is not covered by test code is a possible source of bugs, obviously, so due diligence warrants that you look at that section of code carefully and see if it needs to be covered by a test.

However, this does not mean that code that is already covered by tests will not have any bugs in it. Tests can only reveal the presence, not the absence of bugs (Dijkstra).

The real value of tests is seen when they fail. A failing test tells you that there is something wrong with your code and you have to go and fix it. Tests are less valuable when you use them to play games with management's need to have a sense of comfort about the "quality" of code.

When you write tests after the fact, your mindset is biased more to see them pass rather than fail. When a test fails, you are more likely to do something to just make the failure go away rather than looking more closely at the production code to see if there really is a problem you overlooked before. Hence you will find tests getting @Ignored or deleted altogether and only "well-behaved" tests left active.

Test coverage is a measure of quantity (how *many* potential problems you have accounted for with tests), not quality (how good the code is). In fact, there are many projects that have high test coverage numbers but still have very poor quality code. I'm not just making that up either. This is the result of misusing the code coverage metric.

There may come a day when there will be automated tools that can analyze your code and figure out what you actually want the code to do rather than what the code is actually doing. Until we have that kind of advanced AI technology, humans will still need to fill in those templates that our currently naive code analysers are limited to generating for you. Be careful what you wish for. The day they have programs that can actually think for you is the day they give you your walking papers.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic