0

I'm completely new to JMockIt. In the tutorial I see example codes that uses the final modifier for a @Mocked parameter e.g.

  @Test
   public void doSomethingHandlesSomeCheckedException(@Mocked final DependencyAbc abc) throws Exception
   {
    ...
   }

What does final mocked parameter mean here? Sometimes, "final" is not used. What is the difference?

JavaMan
  • 4,954
  • 4
  • 41
  • 69

2 Answers2

1

This is merely a Java language issue, nothing to do with JMockit itself. For a method parameter or local variable to be used inside an inner class (anonymous or not), the Java compiler requires it to be declared as final.

Rogério
  • 16,171
  • 2
  • 50
  • 63
0

From the JMockit tutorial:

"For a mock parameter declared in a test method, an instance of the declared type will be automatically created by JMockit and passed by the JUnit/TestNG test runner when calling the test method. Therefore, the parameter value will never be null.

For a mock field, an instance of the declared type will be automatically created by JMockit and assigned to the field, unless it's a final field. In such case, a value should be created and assigned to the field explicitly in test code. This value can be null, though, which is perfectly valid for a mocked class if only constructors and static methods are going to be called on it."

http://jmockit.googlecode.com/svn/trunk/www/tutorial/BehaviorBasedTesting.html#declaration

Keep in mind that a mock parameter/field is any annotated with @Mocked or @Injectable.

Ignacio Martin
  • 264
  • 2
  • 11
  • But why the *parameter* (mock parameter != mock field) has to be declared "final"? The tutorial only talk about final mock field, but I can't find any description related to final parameter. – JavaMan Nov 17 '13 at 13:23