简体   繁体   English

Java Mockito JUnit测试/模拟类,其构造函数包含抽象的Method调用

[英]Java Mockito JUnit Test / Mock Class with a constructor that contains a abstract Method call

I've a question about unit and mocked tests. 我有一个关于单元测试和模拟测试的问题。 I want test a modul that has a dependency with a other modul. 我想测试一个与其他模块具有依赖关系的模块。 The problem is, I got this message by the Test Run: 问题是,我通过“测试运行”收到此消息:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/component/UIComponent
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.privateGetPublicMethods(Unknown Source)
    at java.lang.Class.getMethods(Unknown Source)
    at org.mockito.internal.runners.util.TestMethodsFinder.hasTestMethods(TestMethodsFinder.java:13)
    at org.mockito.internal.runners.RunnerFactory.create(RunnerFactory.java:33)
    at org.mockito.runners.MockitoJUnitRunner.<init>(MockitoJUnitRunner.java:57)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    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)

For Example: 例如:

 public void MyMethod(){     
   PickList myPickList = new PickList();
   ... some code ...
   myPickList.getSource(); 
   myPickList.getTarget();
 }

The PickList constructor contains: PickList构造函数包含:

PickList(){setRendererType(rendererValue);}

The method setRendererType ist a abstract method extends UIComponent. setRendererType方法是一个抽象方法,它扩展了UIComponent。

My questions, how can I test / mock (allowed only Java 1.7, JUnit4 and Mockito 1.10.19) this methods? 我的问题是,如何测试/模拟(仅允许Java 1.7,JUnit4和Mockito 1.10.19)这种方法?

Is it possible to mock / avoid the constructor call? 是否可以模拟/避免构造函数调用?

Can I mock deeper dependencies? 我可以模拟更深的依赖关系吗?

So couple things. 所以夫妇。

First, you can mock constructor calls with JMockit or Powermock. 首先,您可以使用JMockit或Powermock模拟构造函数调用。

Second, you should NEVER call an abstract method from a constructor. 其次,永远不要从构造函数中调用抽象方法。 This is also true for any non-final method. 对于任何非最终方法也是如此。 You should never call any non-final instance method from a constructor. 您永远不要从构造函数中调用任何非最终实例方法。 This is because you are invoking an instance method on a sub-class prior to the sub-class' constructor being fully executed. 这是因为您要在子类的构造函数完全执行之前在子类上调用实例方法。 So there might be instance fields in the sub-class that are populated by the constructor. 因此,子类中可能会有实例字段由构造函数填充。 In the call to the instance method (invoked via the super constructor) these fields are not yet populated breaking the expectation of the instance method. 在调用实例方法(通过超级构造函数调用)时,尚未填充这些字段,这违反了实例方法的期望。

Update: Looking at your question however, setRendererType is not abstract to PickList . 更新:然而,从您的问题来看, setRendererType不是PickList抽象。 It is an abstract method in UIComponent but PickList or some parent class must have implemented it so to PickList it is not abstract. 它是UIComponent的抽象方法,但PickList或某些父类必须已实现了它,因此对于PickList它不是抽象的。 However, the rule about non-final methods still applies. 但是,关于非最终方法的规则仍然适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mockito。 如何在被测类中模拟抽象方法调用? - Mockito. How to mock abstract method call in class under test? 如何使用Java中的Junit Mockito处理依赖抽象方法的测试类 - How to deal with test class who is having dependency on abstract method using junit mockito in java Mockito-在构造函数中模拟方法调用 - Mockito - mock a method call in constructor Junit Mockito模拟SessionFactory进入DAO Test类 - Junit Mockito Mock SessionFactory into DAO Test class 构造函数和抽象类中的java调用方法? - java call method in constructor and abstract class? Mockito Junit 4, Mock java 方法没有被调用 - Mockito Junit 4 , Mock java method not getting called Mockito JUnit测试:检查构造函数中的方法调用 - Mockito JUnit testing: check method call in constructor 构造函数测试类中的Null模拟,带有来自Mockito-junit-jupiter的MockitoExtension - Null mock in the constructor test class with MockitoExtension from mockito-junit-jupiter 包含对最终 class 的调用的模拟和测试方法,其中包含 static 和非 static 方法 ZD52387280E1EA213817A - Mock and Test method that contains call to final class that contains static and non static methods Java 如何验证 class 类型的模拟方法参数包含使用 mockito 的值 junit - How to verify mock method parameter of class type contains a value using mockito for junit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM