0

I am studying JUnit and developed a simple class:

package com.test;

import javax.xml.bind.DatatypeConverter;

public class MyConverter {

    public static byte[ ] base64ToByte( String s ) {

        return DatatypeConverter.parseBase64Binary( s );
    }

    public static String byteToBase64( byte[ ] s ) {

        return DatatypeConverter.printBase64Binary( s );
    }

    public static String byteToHex( byte[ ] s ) {

        return DatatypeConverter.printHexBinary( s );
    }

    public static byte[ ] hexToByte( String s ) {

        return DatatypeConverter.parseHexBinary( s );
    }

}

But when I run my test cases, this is the results I got:

Code Coverage: 80%

I noticed that if I call the class constructor, then I get 100% code coverage.
Is there a way to achieve 100% without calling a class constructor?
Should I declare a private constructor and change the class to have a singleton instance?

MiguelKVidal
  • 1,498
  • 1
  • 15
  • 23
  • Junit doesn't "do" code coverage - it's a unit testing framework. You must have used another tool to manage the code coverage and you need to specify which one and how you used it to allow for a meaningful answer. – Erwin Bolwidt Oct 19 '17 at 00:42
  • In any case, every class that doesn't have constructors defined in the source has a default constructor that is generated by the compiler, and likely is that you didn't invoke this constructor while gathering code coverage statistics. How to tell your code coverage tool to exclude this from the statistics (if at all possible) depends on the tool. – Erwin Bolwidt Oct 19 '17 at 00:44
  • @ErwinBolwidt I assumed it was statistics from JUnit. I got this in info in Eclipse after I run my Test Suite. I am using the run as "JUnit Test" from Eclipse. Do you know where I should look to identify the *code coverage tool*? – MiguelKVidal Oct 19 '17 at 00:48
  • I haven't changed anything... I believe it's **EclEmma**... – MiguelKVidal Oct 19 '17 at 00:50
  • Probably http://www.eclemma.org/ in that case. It's popular at the moment. Just try if doing `new MyConverter()` anywhere in your tests increases the coverage to 100% - then you know the reason. – Erwin Bolwidt Oct 19 '17 at 00:50
  • In real life, coverage above 80% is hardly ever worth it – Sean Patrick Floyd Oct 19 '17 at 00:52
  • @ErwinBolwidt Yes, it does. Already tested that. Firstly I assumed it was from JUnit, now that I know the culprit I will look at EclEmma doc and change my question accordingly. Thanks till now! – MiguelKVidal Oct 19 '17 at 00:54

0 Answers0