0

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?

Mag
  • 146
  • 1
  • 13

1 Answers1

1

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.

soru
  • 5,464
  • 26
  • 30
  • I disabled the error in Eclipse as you suggested, but I was hoping there was a better way in Jmockit or something and leaving Eclipse unchanged... This is the only drawback I've seen with JMockIt so far, but maybe same problem with like Mockito..? – Mag Mar 05 '15 at 19:43
  • This is more of a drawback of Eclipse. In IntelliJ IDEA, in comparison, I can configure any code inspection (including these and many, many others) *per scope*. So, for test code I configure a "test" scope where several warnings that are not useful get disabled, while leaving them enabled for production code. Hopefully, Eclipse will catch up some day. – Rogério Mar 05 '15 at 20:52
  • Yes, maybe an Eclipse problem since you don't get any problem when compiling it outside Eclipse. I found that you can ignore compiler warnings in Eclipse per source folder now, in Properties for e.g. src/test folder. However Eclipse insist this an error so you still need to change it to a warning. – Mag Mar 06 '15 at 08:00