vendredi 8 septembre 2017

Why do I get a Reflection exception NoSuchMethodException if the tests runs fine.

I'm using Reflection to Mock a private method (I don't want to discuss if that makes sense or not). I wrote my test and JUnit says that my test it's Successful but in my console I get this:

java.lang.NoSuchMethodException: com.siemens.it.blackduck_client_tools.ProtexManager.processRequiredFile(com.blackducksoftware.sdk.codecenter.deeplicense.data.File)

    at java.lang.Class.getDeclaredMethod(Class.java:2130)
    at com.siemens.it.blackduck_client_tools.testProtexManagerProcessRequiredFile.processRequiredFileTest(testProtexManagerProcessRequiredFile.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Anyone know why? I'll let my testClass source code here it may help. I've tryed much of the Internet helps and ways to solve this but none have worked for me.

import static org.junit.Assert.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import com.blackducksoftware.sdk.codecenter.deeplicense.data.File;

public class testProtexManagerProcessRequiredFile {

  @Mock
  ProtexManager PxManager;


  @Before
  public void inicializa() {
    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void processRequiredFileTest() throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, InstantiationException {

    Method method;
    try {
      method = ProtexManager.class.getDeclaredMethod("processRequiredFile", File.class);
      method.setAccessible(true);

      File FileExample = new File();
      String NameExample = "Nome";
      File outputs = new File();
      outputs = (File) Mockito.when(method.invoke(PxManager, FileExample,NameExample)).thenReturn(FileExample);
      assertNotNull(outputs);
      assertEquals(outputs, method.invoke(PxManager, FileExample,NameExample));

    } catch (NoSuchMethodException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
   System.out.println("Teste Concluido.");


  }

}

And thank you all for helping me in my doubts.





Aucun commentaire:

Enregistrer un commentaire