Uncovered not always bad 37

Posted by Brett Schuchert Thu, 31 May 2007 05:10:00 GMT

Let’s say for the sake of argument that you are:
  • Using a code coverage tool such as Cobertura, Emma
  • You are actually looking at the coverage of your tests (say you’re looking for stale code in our unit tests – [and you’re thinking gosh, I wish I had some unit tests so I could have some unused code in my unit tests])
  • You are testing that unit under test generates an exception
In JUnit 4 we might write the following:
@Test(expected=RuntimeException.class)
public void methodThatWeExpectAnException() {
    throw new RuntimeException();
}

This test will pass. Yes it’s trivial, of course it would pass. (In reality the single line of code would instead send a message to some object that ultimately would need to generate a RuntimeException for a “real” test to pass.) Fine. That’s not the point.

So what’s the problem with this? Nothing, except that some coverage tools will report the last “line” (the close curly-brace) as not being covered since we did not exit the method cleanly.

Here’s a way to rewrite the above test so that you can assure coverage:
@Test
public void methodThatWeExpectWillThrowAnException() {
    boolean expectedThrown = false;

    try {
        throw new RuntimeException();
    } catch (RuntimeException e) {
        expectedThrown = true;
    }

    assertTrue(expectedThrown);
}

This version is a bit longer, isn’t it?

Here are some comments I’d like to hear from you:
  • Is it any better?
  • Does is express our intent any better?
  • Isn’t it just silly to run coverage tools on your test code?
  • Is anybody having Pascal flashbacks? (If you don’t get this question…you poor &*$^@~).
Comments

Leave a response

  1. Avatar
    Rytmis about 2 hours later:

    Flashbacks? Lucky &*$^@~... Pascal is a thing some of us have to deal with on a near-daily basis!

    The second version has a lot of syntax that really doesn’t do anything to clarify the intent.

  2. Avatar
    Si about 4 hours later:

    Personally, I prefer the first version, as I’m a big believer in ”’Good’ is good enough.”

    The intent of the first test is perfectly clear.

  3. Avatar
    Jason about 7 hours later:

    I don’t see the value in making the code bigger just to get a coverage report number higher. To me it would make more sense to spend some time with the coverage report and investigate things that aren’t covered as to why, because as you demonstrated sometimes there’s a good reason.

  4. Avatar
    YAChris about 11 hours later:

    Interesting to see how wildly out-of-favor comments are these days. You could just add a comment line before the close-brace in the first case, saying, Gee, this isn’t covered, but it’s no big deal... if that kind of thing was the fashion these days.

  5. Avatar
    Dean Wampler about 13 hours later:

    I would keep the first syntax and get my tests out of the coverage count. Being anal retentive, I hate having uncovered lines in my coverage reports and I don’t personally think it’s important for the tests to be tested for coverage.

    Actually, if you don’t buy the anal-retentive argument, there is another good reason to strive for 100% coverage; you don’t waste your time reviewing the same uncovered lines every time (which are “crying wolf…”). You’ll eventually just stop checking and then you won’t notice when new code that really should have coverage is not covered. To me, it’s potentially a slippery slope like starting to not care if a few unit tests are failing.

  6. Avatar
    Jason about 19 hours later:

    That’s a great point Dean. We should strive for 100% coverage because then anything that’s not covered is what we should investigate.

  7. Avatar
    Ben Rady about 22 hours later:

    Yeah, I wouldn’t tweak the structure of my tests just to increase the coverage in my test code. What does increasing/decreasing/constant test code coverage indicate anyway? Seems just like noise to me.

    Now, I will say that I don’t like the JUnit 4 exception syntax because there doesn’t seem to be a way to check what the exception message is (or maybe I just haven’t found it yet)

  8. Avatar
    Uncle Bob 1 day later:

    I’d fix the freakin tool.

    ;-)

    Or rather, I would not warp my code because of the way cobertura, or emma, behaves. I like the first version, and would either fix the tool, or tolerate the coverage miss.

    Actually, there’s no way to cover all the production code anyway. Interfaces, for example, aren’t covered by most tools. So there will always be some code that is missed. A few extra closing braces don’t bother me.

    I like covering my tests. I have found some very interesting surprises by looking at the portions of my tests that are covered and the portions that are not.

  9. Avatar
    Brett Schuchert 2 days later:

    I think fixing the tool is a good way to go. I do think the above fix is way too much. On the other hand I really like covering my tests as well.

    I’m under the impression we want to refactor our code. When I hear this, I hear we should do so for both “production” code as well as “test” code. If that’s the case, then I don’t see why we should not cover our tests.

  10. Avatar
    Eric Bodden 5 days later:

    I think you are asking the wrong question. The more interesting seems to be to me: How bad are those code coverage tools??? I think the least they should be able to do is some decent control flow analysis, if not even data flow analysis. If a tool does not report 100% coverage for your three lines example, I would dump it!

  11. Avatar
    Timo Rantalaiho 13 days later:

    While I don’t like coverage filling, I like making assertions on the expected exception message.

  12. Avatar
    Mike 15 days later:

    Prefer #2, as I usually check the message of the thrown exception: try { throwSomething(); fail(“FooException expected”) } catch (FooException expected) { assertEquals(“Foo”,expected.getMessage()); }

  13. Avatar
    moncler over 3 years later:

    To me it would make more sense to spend some time with the coverage report and investigate things that aren’t covered as to why, because as you demonstrated sometimes there’s a good reason.

  14. Avatar
    Pandora over 4 years later:

    I built a castle in the swamp and it sunk. I built a second castle and it sunk too. I built a third castle and it burned down and then sunk.

  15. Avatar
    Backup iPhone over 4 years later:

    Well. when you want to finish this. You should learn it.

  16. Avatar
    http://www.blacktowhiteiphone4.com over 4 years later:

    They also sold 4.19 Million iPads, 3.89 million Macs and 9.05 million iPods. Pretty massive. Read the full press release after the jump.

  17. Avatar
    coach factory outlet over 4 years later:

    thanks for your sharing, I appreciate this. keep up the good work

  18. Avatar
    Criminal Records over 4 years later:

    This is such a good information. Keep posting useful and informative articles.

  19. Avatar
    cable ties over 4 years later:

    very good info in this article.

  20. Avatar
    dswehfhh over 4 years later:

    We are the professional clothes manufacturer and clothes supplier, so we manufacture kinds of custom clothes manufacturer. welcome you to come to our china clothes manufacturer and clothes factory.

  21. Avatar
    jaychouchou over 4 years later:

    To be, or not to be- that is a question.Whether ipad bag tis nobler in the mind to suffer The slings and Game Controllers arrows of outrageous fortune Or to take arms against a sea of troubles, And USB Gadgets by opposing end them!!!!!!!!!!!!!!!!!!!!!!

  22. Avatar
    Laura over 4 years later:

    Speak your minc. It is common that ladies wear leggings to match their long shirts or short dresses.

  23. Avatar
    Vince Clancy over 4 years later:

    This is another informative post. I like it. Thanks. roof sarasota

  24. Avatar
    okey oyunu oyna over 4 years later:

    Actually, this is very nice informative article..

    Gerçek ki?ilerle sohbet ederek okey oyunu oyna ve internette online oyun oynaman?n zevkini ç?kar.

  25. Avatar
    real estate advertising over 4 years later:

    I really like your article Thank you :-)

  26. Avatar
    Monster Beats By Dr. Dre Studio over 4 years later:

    The Monster Beats By Dr. Dre Studio Headphones are classic shoes.You will be proud of owning them. Don’t hesitate!Take Monster Beats By Dr. Dre Solo Headphones home!

  27. Avatar
    cookies gift baskets over 4 years later:

    it needs a bokmark so i can come back to it later ,nice stuff

  28. Avatar
    abercrombie and fitch over 4 years later:

    If Abercrombie & Fitch is really concerned that the Situation and company have appeared on air in its garments, it doesn’t appear that angry.Bienvenue pour parcourir Abercrombie France site en ligne, vente en ligne New Style Belstaff en France, vous pouvez acheter la qualite superieure et escompte.Teen apparel retailer Abercrombie & Fitch Co. is offering to pay Michael “The Situation” Sorrentino not to wear its merchandise.Abercrombie Paris est le vêtement de la marque originale et riche d’une histoire enracinee dans le grand air et de la cote Est Ivy League.

  29. Avatar
    karin over 4 years later:

    I saw this one and fell in love! I must admit i was dubious about ordering a dress from a overseas company, but i can’t praise Lightinthebox enough for there professionalism, quality, communications, and swift delivery. I ordered a custom size and the dress was delivered in just over 2 weeks to the UK. One word sums up the quality of this dress… Wow!!! The material is really good quality and the craftsmanship is second to none

  30. Avatar
    luggage racks for cars over 4 years later:

    It’s good solution of the problem.

  31. Avatar
    robert.robot369@gmail.com over 4 years later:

    This is very much satisfied by the great info is visible in this blog and I am searching the great info is visible in this blog Program Coordinator Job Description|Hospital Administrator Job Description|Cook Job Description|Registered Nurse Job Description

  32. Avatar
    Tips For Bowling over 4 years later:

    I am 73 years old. I was born in Jerusalem. I’m the first prime minister of Israel to be born here. I am the only former general to become a prime minister.

  33. Avatar
    UGG Kensington over 4 years later:


    The autumn collection of UGG Kensington are now in stock . The range features Kensington boots.
    UGG Kensington Boots catalog has boots, sneakers and slippers are for everyone that wants to keep warn and dry with the ultimate style. UGGs Kensington are the hot boots right now yet there are lots more Ugg cozy feet products to choose from. The winter season selection has an exiting choice in a number of colors.

    Kensington UGG Boots Kensington UGGs are fashionable biker type boots that are amazingly comfy. Totally Kensington UGG, 100 % cool. They really are warm too because the insole is authentic sheepskin and so are the lined uppers. The external soles are a particular EVA with synthetic rubber pod inserts, so no chance of slipping and they last forever too.

  34. Avatar
    alwadifa over 4 years later:

    I liked you blog so im going bookmark it with my prefered websites, you have posted an amazing posts so thank you I liked you blog so im going bookmark it with my prefered websites, you have posted an amazing posts so thank you

  35. Avatar
    christian louboutin over 4 years later:

    The professional design make you foot more comfortable. Even more tantalizing,this pattern make your legs look as long as you can,it will make you looked more attractive.Moveover,it has reasonable price.If you are a popular woman,do not miss it.

    Technical details of Christian Louboutin Velours Scrunch Suede Boots Coffee:

    Color: Coffee
    Material: Suede
    4(100mm) heel
    Signature red sole x

    Fashion, delicate, luxurious Christian louboutins shoes on sale, one of its series is Christian Louboutin Tall Boots, is urbanism collocation. This Christian louboutins shoes design makes people new and refreshing. Red soles shoes is personality, your charm will be wonderful performance.

  36. Avatar
    louboutin sales over 5 years later:

    Uncovered not always bad 35 hoo,good article!!I like the post!25

  37. Avatar
    bladeless fans over 5 years later:

    Uncovered not always bad 36 good post166

Comments