我可以在 groovy 中使用映射强制来模拟具有带参数的构造函数的类吗?
测试中的 Java 示例类
public class Sample {
public void printPie() {
System.out.println("Pie.");
}
}
public class SampleCont {
String _PIE;
public SampleCont() {
_PIE = "pie";
}
public void printPie() {
System.out.println(_PIE);
}
}
public class SampleContArg {
String _PIE;
public SampleContArg(String pie) {
_PIE = pie;
}
public void printPie() {
System.out.println(_PIE);
}
}
groovy 测试类
public class TestPie extends GroovyTestCase{
void test_Sample()
{
def mock = [printPie: {print "NotPie"}] as Sample
mock.printPie()
}
void test_SampleCont()
{
def mock = [printPie: {print "NotPie"}] as SampleCont
mock.printPie()
}
void test_SampleContArg()
{
def mock = [printPie: {print "NotPie"}] as SampleContArg
mock.printPie()
}
}
前两个测试编译并运行良好。 后一个测试由于异常而未运行:
java.lang.ArrayIndexOutOfBoundsException: 0
at SampleContArg_groovyProxy.<init>(Script1.groovy:4)
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.codehaus.groovy.runtime.MetaClassHelper.doConstructorInvoke(MetaClassHelper.java:595)
at groovy.lang.MetaClassImpl.doConstructorInvoke(MetaClassImpl.java:2359)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1255)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1185)
at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:809)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeNewN(ScriptBytecodeAdapter.java:230)
at Script1.run(Script1.groovy:9)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:543)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:518)
at groovy.util.ProxyGenerator.instantiateAggregate(ProxyGenerator.java:201)
at groovy.util.ProxyGenerator.instantiateAggregateFromBaseClass(ProxyGenerator.java:70)
at groovy.util.ProxyGenerator.instantiateAggregateFromBaseClass(ProxyGenerator.java:66)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.asType(DefaultGroovyMethods.java:3811)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:51)
at org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod.invoke(NewInstanceMetaMethod.java:54)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:766)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:754)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.asType(ScriptBytecodeAdapter.java:622)
at com.hp.vse.cp.cli.agentless.ssh.TestPie.test_SampleContArg(TestPie.groovy:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
对于上面的第三个类,我仍然可以使用 groovy 映射强制来模拟该类吗? 如果是这样,我做错了什么?
Java example classes under test
public class Sample {
public void printPie() {
System.out.println("Pie.");
}
}
public class SampleCont {
String _PIE;
public SampleCont() {
_PIE = "pie";
}
public void printPie() {
System.out.println(_PIE);
}
}
public class SampleContArg {
String _PIE;
public SampleContArg(String pie) {
_PIE = pie;
}
public void printPie() {
System.out.println(_PIE);
}
}
The groovy test class
public class TestPie extends GroovyTestCase{
void test_Sample()
{
def mock = [printPie: {print "NotPie"}] as Sample
mock.printPie()
}
void test_SampleCont()
{
def mock = [printPie: {print "NotPie"}] as SampleCont
mock.printPie()
}
void test_SampleContArg()
{
def mock = [printPie: {print "NotPie"}] as SampleContArg
mock.printPie()
}
}
The first two tests compile and run fine. The latter test doesn't run due to an exception:
java.lang.ArrayIndexOutOfBoundsException: 0
at SampleContArg_groovyProxy.<init>(Script1.groovy:4)
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.codehaus.groovy.runtime.MetaClassHelper.doConstructorInvoke(MetaClassHelper.java:595)
at groovy.lang.MetaClassImpl.doConstructorInvoke(MetaClassImpl.java:2359)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1255)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1185)
at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:809)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeNewN(ScriptBytecodeAdapter.java:230)
at Script1.run(Script1.groovy:9)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:543)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:518)
at groovy.util.ProxyGenerator.instantiateAggregate(ProxyGenerator.java:201)
at groovy.util.ProxyGenerator.instantiateAggregateFromBaseClass(ProxyGenerator.java:70)
at groovy.util.ProxyGenerator.instantiateAggregateFromBaseClass(ProxyGenerator.java:66)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.asType(DefaultGroovyMethods.java:3811)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:51)
at org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod.invoke(NewInstanceMetaMethod.java:54)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:766)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:754)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.asType(ScriptBytecodeAdapter.java:622)
at com.hp.vse.cp.cli.agentless.ssh.TestPie.test_SampleContArg(TestPie.groovy:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
In the case of the third class above, can I still use groovy map coercion to mock the class? If so, what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有 ProxyGenerator.instantiateAggregateFromBaseClass 方法能够接受构造函数参数。 但不幸的是,as 运算符不会为您调用它。
所以,目前的答案是否定的。
仅供参考,“as”将调用 DefaultGroovyMethods.asType() 方法之一,但它们都不会调用您想要的 instantiateAggregateFromBaseClass 。
There is the method ProxyGenerator.instantiateAggregateFromBaseClass that be able to accept constructor arguments. But unfortunately, the as operator will not call it for you.
So, the answer is no, at the moment.
FYI, "as" will invoke one of DefaultGroovyMethods.asType() methods, but none of them calls the instantiateAggregateFromBaseClass you wanted.