超类没有空构造函数,但没有弹簧集成就没有给出参数

时间:2017-12-08 13:13:58

标签: java jbehave thucydides

java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
    at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:721)
    at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
    at net.thucydides.core.steps.StepFactory.createProxyStepLibrary(StepFactory.java:155)
    at net.thucydides.core.steps.StepFactory.instantiateNewStepLibraryFor(StepFactory.java:109)
    at net.thucydides.core.steps.StepFactory.instantiateNewStepLibraryFor(StepFactory.java:101)
    at net.thucydides.core.steps.StepFactory.getStepLibraryFor(StepFactory.java:67)
    at net.thucydides.core.steps.StepAnnotations.instantiateAnyUnitiaializedSteps(StepAnnotations.java:50)
    at net.thucydides.core.steps.StepAnnotations.instanciateScenarioStepFields(StepAnnotations.java:41)
    at net.thucydides.core.steps.StepAnnotations.injectScenarioStepsInto(StepAnnotations.java:23)
    at net.thucydides.jbehave.ThucydidesStepFactory.createInstanceOfType(ThucydidesStepFactory.java:80)
    at org.jbehave.core.steps.StepCreator.stepsInstance(StepCreator.java:82)
    at org.jbehave.core.steps.StepCreator$ParameterisedStep.perform(StepCreator.java:550)
    at org.jbehave.core.embedder.StoryRunner$FineSoFar.run(StoryRunner.java:499)
    at org.jbehave.core.embedder.StoryRunner.runStepsWhileKeepingState(StoryRunner.java:479)
    at org.jbehave.core.embedder.StoryRunner.runScenarioSteps(StoryRunner.java:443)
    at org.jbehave.core.embedder.StoryRunner.runCancellable(StoryRunner.java:305)
    at org.jbehave.core.embedder.StoryRunner.run(StoryRunner.java:219)
    at org.jbehave.core.embedder.StoryRunner.run(StoryRunner.java:180)
    at org.jbehave.core.embedder.StoryManager$EnqueuedStory.call(StoryManager.java:229)
    at org.jbehave.core.embedder.StoryManager$EnqueuedStory.call(StoryManager.java:201)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:2)

我不确定该问题是否已解决,但我设法理解了我的问题:

java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

具有以下代码:

public class CglibTest {
    @Test
    public void shouldCreateDynamicProxy() {
        Enhancer enh = new Enhancer();
        enh.setSuperclass(CgPlay.class);
        enh.setCallback((FixedValue) () -> "fixedValueFromProxy");
        CgPlay cgPlay = (CgPlay) enh.create();
        System.out.println(cgPlay.fooStringPar(null));
    }

    class CgPlay {
        public String fooStringPar(String s) {
            return "fooStringPar";
        }
    }
}

事实证明,CGLib的CgPlay类有问题,因为 非静态成员内部类。使CgPlay类成为静态类或非成员类可以解决问题。

答案 1 :(得分:0)

你有一个类通过使用super()调用其实现的类的构造函数,其中没有参数。

你的超类没有任何参数的构造函数。

示例:

public class Cat implements Animal {
    public Cat() {
        super();
    }
}

public class Animal {
    final String name;
    public Animal(String name) {
        this.name = name;
    }
}

Cat类中的super()将调用没有Animal类参数的构造函数。 Animal类没有这样的构造函数,因此会抛出像你的错误。

如果您让像Spring这样的框架尝试自动装配一个没有无参数构造函数的对象,也会导致这种情况。

请分享投掷错误的类。