Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you absolutely need to achieve 100% code coverage - the merits of that can be debated elsewhere :) - you can achieve it using reflection in your tests. As habit, when I implement a static-only utility class, I add in a private constructor to ensure that instances of the class can't be created. For example:</p> <pre><code>/** * Constructs a new MyUtilities. * @throws InstantiationException */ private MyUtilities() throws InstantiationException { throw new InstantiationException("Instances of this type are forbidden."); } </code></pre> <p>Then your test might look something like this:</p> <pre><code>@Test public void Test_Constructor_Throws_Exception() throws IllegalAccessException, InstantiationException { final Class&lt;?&gt; cls = MyUtilties.class; final Constructor&lt;?&gt; c = cls.getDeclaredConstructors()[0]; c.setAccessible(true); Throwable targetException = null; try { c.newInstance((Object[])null); } catch (InvocationTargetException ite) { targetException = ite.getTargetException(); } assertNotNull(targetException); assertEquals(targetException.getClass(), InstantiationException.class); } </code></pre> <p>Basically, what you're doing here is getting the class by name, finding the constructors on that class type, setting it to public (the <code>setAccessible</code> call), calling the constructor with no arguments, and then ensuring that the target exception that is thrown is an <code>InstantiationException</code>.</p> <p>Anyway, as you said, the 100% code coverage requirement here is kind of a pain, but it sounds like it's out of your hands, so there's little you can do about it. I've actually used approaches similar to the above in my own code, and I did find it beneficial, but not from a testing perspective. Rather, it just helped me learn a little bit more about reflection than I knew before :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload