我正在使用Marathon测试Java Swing应用程序。使用Jython的Java Swing应用程序的测试工具。在整个问题范围内,我只关心两个python文件。

CallingFile.py:

    testObject = getParentOfFooJPanel(get_component('...'))
    print(testObject.getClass().getSimpleName()) #this is only here for testing
                                                 #if my code works, but this is
                                                 #where I get the errors


FileWithMethod.py

    def getParentOfFooJPanel(startingComponent):
        if (startingComponent.getClass().getSimpleName() == 'FooJPanel')

            print(startingComponent.getClass().getSimpleName()) #prints what I would expect
            print(startingComponent.getParent().getClass().getSimpleName()) #prints what I would expect
            return(startingComponent.getParent())

        else:

            getParentOfFooJPanel(startingComponent.getParent())


每当我尝试在FileWithMethod.py中引用该对象时,它的行为都与我期望的一样。但是,当我返回组件(Java对象)并尝试在CallingFile.py中使用它时(现在我在这里打印简单名称),它说'NoneType'对象没有属性'getClass'。 Jython不能返回Java对象吗?如果没有,是否有解决方法?

最佳答案

Jython绝对可以返回Java对象。下面是返回对象的示例。

您的错误很可能意味着代码中的其他错误正在返回None

CallingFile.py:

from FileWithMethod import getStringReader
testObject = getStringReader("Hello World")
print(type(testObject))


FileWithMethod.py

import java.io.StringReader
def getStringReader(string):
    return java.io.StringReader(string)


运行它:

$ jython CallingFile.py
<type 'java.io.StringReader'>

07-28 02:39
查看更多