Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top