简体   繁体   中英

JUnit Eclipse VS Ant

I am trying to launch my Junit test by an Ant Task, as below :

<target name="TestDaoImpl">
        <mkdir dir="${junit.output.dir}"/>
        <junit fork="yes" printsummary="withOutAndErr">
            <jvmarg line="${conf.dir}"/>
            <formatter type="xml"/>
            <test name="my.package.TestKSLDaoImpl" todir="${junit.output.dir}"/>
            <classpath refid="My.classpath"/>
        </junit>
 </target>

In my test I am using PowerMockito, for this two cases :

PowerMockito.whenNew(Convert.class).withAnyArguments().thenReturn(convert);
PowerMockito.mockStatic(MyService.class);

And Mockito:

Mockito.when(convert.getXmlKsl(folder)).thenReturn(xmlStr); 

Actually when I ran my test in Eclipse, I didn't get any errors. But when I launch it by Ant Task, I got this errors :

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
   It is a limitation of the mock engine.

at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:495)

The error is here :

PowerMockito.mockStatic(MyService.class);

===> Mockito.when(MyService.getInstance(myId)).thenReturn(myService);

I am using this jars :

JUnit 4
cglib-nodep-2.2.2.jar
javassist-3.18.1-GA.jar
mockito-all-1.9.5.jar
objenesis-2.1.jar
powermock-mockito-1.5.4-full.jar

Are there any conflict with ant and PowerMockito? Why test run well by eclipse but don't by Ant?

Why tests might pass in Eclipse but fail when run on the command line:

The cause of these types of issues, a build passing on either the command-line or the IDE but not on both, is typically related to differences in the classpath. Typically command-line tools and IDEs will resolve dependencies differently. Additionally most IDEs also come with their own version of JUnit that might not match what is specified by the project.

To resolve this you will want to analyse the differences in the classes being used by the command-line tool, in this case Ant, and your IDE. Keep in mind that the order in which dependencies appear on the command line is important when a class is included in multiple dependencies.

The "all" designation means that all of the dependent classes are prepackaged into the jar, this can causes issues if you include a newer versions of the dependencies then what is pre-packaged. For example if you have dependencies on Mockito-All 1.9.5, which has Hamcrest 1.1 prepackaged, and Hamcrest 1.3, you will have two different copies of the same jar on the classpath. This not always a problem unless you are trying to use a method that is in one version but not the other. Usually which ever version is listed first is used.

There are some known dependency issues with some version of JUnit 4, Mockito-all 1.9.5 and Hamcrest. I would recommend specifying: JUnit 4.11, Mockito-core 1.9.5 and Hamcrest 1.3. You will want to check to make sure that Eclipse is using these versions and not substituting its own versions.

Conflicts between Ant and PowerMockito:

Ant is a build tool and should not have any conflicts with PowerMock. It may be worth you time and effort to upgrade to a more modern build tool with better dependency management. Both Maven and Gradle are well supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM