Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error when using EclEmma (eclipse plugin) with JMockit (only with Delegate())

I recently started using JMockit and am very new at TDD and mocking. I like to use code coverage to make sure that I have tested all the lines for a given a class/method.

I came across the following error when trying JMockit (Delegate() functionality) and code-coverage. I am including both a passing and failing test.

I am not sure if I am doing something wrong?

I would like to know if anyone else has encountered this issue and if there are any workaround or fixes available?

I am not even sure if this is a JMockit issue or EclEmma issue. Thanks.

My setup:

  • OS: Windows XP
  • JDK-JRE: 1.7.0
  • JUnit: 4.11
  • JMockit: 1.3
  • EclEmma: 2.2.1.201306092145
  • Eclipse: 3.6

Failure Trace:

java.lang.IllegalArgumentException: No compatible method found: getType(java.lang.Integer) at com.ps.jmockit.samples.DelegateCoverageTest$Dog.getAnimalType(DelegateCoverageTest.java:99) at com.ps.jmockit.samples.DelegateCoverageTest.coverageFails(DelegateCoverageTest.java:71) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.lang.reflect.Method.invoke(Method.java:601) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.lang.reflect.Method.invoke(Method.java:601) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


Code: (coverageFails() will show the error above when used with EclEmma)

package com.ps.jmockit.samples;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import mockit.Delegate;
import mockit.Expectations;
import mockit.Mocked;

import org.junit.Test;

/**
 *  As of 7/25/2013, if I try to run the EclEmma coverage tool, it fails for {@link #coverageFails()}
 */
public class DelegateCoverageTest
{

    @Mocked
    Animal  fakeAnimal;

    @Test
    public void coverageWorks()
    {
        final String fake = "Fake";

        new Expectations()
        {
            {
                DelegateCoverageTest.this.fakeAnimal.getType(1);
                result = fake;
            }
        };

        //Arrange
        final Dog dog = new Dog();

        // Act
        final String animalType = dog.getAnimalType(this.fakeAnimal);

        // Assert
        assertThat(animalType, is(fake));
    }

    @Test
    public void coverageFails()
    {
        final String one = "One";
        final String two = "Two";

        new Expectations()
        {
            {
                DelegateCoverageTest.this.fakeAnimal.getType(anyInt);
                result = new Delegate()
                {
                    @SuppressWarnings("unused")
                    String aDelegateMethod( final int input )
                    {
                        return input == 1
                                ? one
                                : two;
                    }
                };
            }
        };

        //Arrange
        final Dog dog = new Dog();

        // Act
        final String animalType = dog.getAnimalType(this.fakeAnimal);

        // Assert
        assertThat(animalType, is(one));
    }


    //----------------- SUPPORTING CLASSES -----------------//

    interface Animal
    {
        String getType(int input);
    }

    class Mammal implements Animal
    {
        @Override
        public String getType(final int input)
        {
            return "Mammal";
        }
    }

    class Dog
    {

        public String getAnimalType(final Animal animal)
        {
            return animal.getType(1);
        }
    }

}
like image 913
Pranav Shah Avatar asked Nov 13 '22 00:11

Pranav Shah


1 Answers

Seems to be a bug : I see relevant links here: https://github.com/jacoco/eclemma/issues/9 and here: https://github.com/jacoco/jacoco/issues/35

like image 113
Vrashabh Irde Avatar answered Nov 14 '22 21:11

Vrashabh Irde