Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Types as parameters to junit test with custom runner fails #88

Closed
djrichar opened this issue Oct 15, 2014 · 0 comments
Closed
Assignees
Labels

Comments

@djrichar
Copy link

i have a reproducable test case below. In a custom Junit4BlockRunner if you use a generic type as a parameter you will get the folowing exception:

java.lang.IllegalArgumentException: Type of unexpected kind: java.lang.Class<?>[]
at mockit.integration.junit4.JUnit4CustomRunnerAnnotationOnlyTest$CustomRunner$1.evaluate(JUnit4CustomRunnerAnnotationOnlyTest.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)

I'd expect it to act as if it were not a Generic type.

/*
 * Copyright (c) 2006-2014 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.integration.junit4;

import mockit.Injectable;
import mockit.Mocked;
import mockit.internal.state.TestRun;
import org.junit.Test;
import org.junit.internal.runners.statements.InvokeMethod;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;

import java.lang.annotation.Annotation;
import java.util.List;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

@RunWith(JUnit4CustomRunnerAnnotationOnlyTest.CustomRunner.class)
public final class JUnit4CustomRunnerAnnotationOnlyTest
{
    private static final Runnable myRunnable = new Runnable() {
        @Override
        public void run() {

        }
    };
    private static final Dependency myDependency = new Dependency();

   public static final class CustomRunner extends BlockJUnit4ClassRunner
   {
      public CustomRunner(Class<?> klass) throws InitializationError { super(klass); }

      @Override
      protected void validatePublicVoidNoArgMethods(
         Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors) {}

      @Override
      protected Statement methodInvoker(final FrameworkMethod method, final Object test) {
          return new Statement(){
              @Override
              public void evaluate() throws Throwable {
                  if(method.getName().matches("(?i)withInvalidMockType\\d*")) {
                      method.invokeExplosively(test, myRunnable, myDependency, new Class<?>[]{
                              Runnable.class, Dependency.class
                      });
                  }else{
                      method.invokeExplosively(test, myRunnable, myDependency);
                  }
              }
          };
      }
   }

   @Test
   public void withAnnotatedParameters(@Mocked Runnable runnable, @Injectable Dependency dep)
   {
      assertNotNull(runnable);
      assertNotNull(dep);
      assertNotSame(myRunnable, runnable);
      assertNotSame(myDependency, dep);
   }

   @Test
   public void withNonAnnotatedParameters(Runnable runnable, Dependency dep)
   {
      assertNotNull(runnable);
      assertNotNull(dep);
      assertSame(myRunnable, runnable);
      assertSame(myDependency, dep);
   }

   @Test  //successful
   public void withInvalidMockType(Runnable runnable, Dependency dep, Class[] types){
       assertNotNull(runnable);
       assertNotNull(dep);
       assertNotNull(types);
       assertSame(myRunnable, runnable);
       assertSame(myDependency, dep);
   }

    @Test //successful
    public void withInvalidMockType2(Runnable runnable, Dependency dep, Class... types){
        assertNotNull(runnable);
        assertNotNull(dep);
        assertNotNull(types);
        assertSame(myRunnable, runnable);
        assertSame(myDependency, dep);
    }

   @Test  //failed
   public void withInvalidMockType3(Runnable runnable, Dependency dep, Class<?>[] types){
       assertNotNull(runnable);
       assertNotNull(dep);
       assertNotNull(types);
       assertSame(myRunnable, runnable);
       assertSame(myDependency, dep);
   }

    @Test //failed
    public void withInvalidMockType4(Runnable runnable, Dependency dep, Class<?>... types){
        assertNotNull(runnable);
        assertNotNull(dep);
        assertNotNull(types);
        assertSame(myRunnable, runnable);
        assertSame(myDependency, dep);
    }
}
@rliesenfeld rliesenfeld self-assigned this Oct 16, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants