Search code examples
eclipsejmockitsuppress-warnings

Eclipse generate "allocated object never used" errors in jmockit test


Using JMockIt 1.12 and Eclipse Luna and I get "The allocated object is never used" errors. I tried:

@Test
public void testNullCase() {

    new NonStrictExpectations() {{ 
         TestClass.getPlug();   
         result = null;
        }
...
    };

To use SuppressWarnings I had to use something ugly like this:

@Test
public void testNullCase() {
    @SuppressWarnings("unused")
    NonStrictExpectations dummy = new NonStrictExpectations() {{ 
         TestClass.getPlug();   
         result = null;
        }
...
    };

How to do this in a nicer way or am I missing something using JMockIt?


Solution

  • This warning can be turned off via:

    • opening the dialog Window/Preferences
    • going to section Java/Compiler/Errors&Warnings/Potential Programming problems
    • disabling Unused object allocation.

    If you want to make that change persistent outside a particular Eclipse workspace, you can use the workspace mechanic. Or you could move the @SuppressWarnings("unused") to the test class to disable it for all tests in the class.