Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 在Groovy中创建模拟JMX服务器和bean_Unit Testing_Groovy_Jmx - Fatal编程技术网

Unit testing 在Groovy中创建模拟JMX服务器和bean

Unit testing 在Groovy中创建模拟JMX服务器和bean,unit-testing,groovy,jmx,Unit Testing,Groovy,Jmx,我们正在监视一个具有大量自定义MBean和属性的自定义应用程序。我们已经在实验室用服务器进行了很长时间的功能测试,但现在我们想要创建不依赖实际远程连接的适当单元测试 就我个人而言,我根本无法理解如何使用Groovy JMX builder启动本地MBeanServer,并使用属性和操作列表公开本地MBean 有人愿意提供这样一个模型的例子吗?我为jmxbuilder找到的现有示例并没有让我明白这一点,但它看起来应该是可能的。以下是我使用的示例(Java中的示例,而不是Groovy…) 有关这些其

我们正在监视一个具有大量自定义MBean和属性的自定义应用程序。我们已经在实验室用服务器进行了很长时间的功能测试,但现在我们想要创建不依赖实际远程连接的适当单元测试

就我个人而言,我根本无法理解如何使用Groovy JMX builder启动本地MBeanServer,并使用属性和操作列表公开本地MBean

有人愿意提供这样一个模型的例子吗?我为jmxbuilder找到的现有示例并没有让我明白这一点,但它看起来应该是可能的。

以下是我使用的示例(Java中的示例,而不是Groovy…)

有关这些其他方法的详细信息,请参见my。

以下是我使用的方法(例如Java,而不是Groovy…)


有关其他方法的详细信息,请参见my。

斯波克测试的简单示例

  • 将groovy bean导出到JMX
  • 导入和使用这个bean
守则:

class JMXSpec extends Specification {

    class MyBean {
        def property
    }

    def "launch and test a mbean"() {
        given: "a mbean exported"

            def jmx = new JmxBuilder()
            jmx.export {
                // Export every properties/methods of 'MyBean' 
                // under 'Groovy.Test:name=MyBean'
                bean(
                    target:new MyBean(property:"test"),
                    name:"Groovy.Test:name=MyBean"
                )

            }

        and: "a mbean imported from the local server"
            def mbean = new GroovyMBean(ManagementFactory.getPlatformMBeanServer(), 
                "Groovy.Test:name=MyBean")
        expect:
            "test" == mbean.Property
    }
}

Spock测试的一个简单示例

  • 将groovy bean导出到JMX
  • 导入和使用这个bean
守则:

class JMXSpec extends Specification {

    class MyBean {
        def property
    }

    def "launch and test a mbean"() {
        given: "a mbean exported"

            def jmx = new JmxBuilder()
            jmx.export {
                // Export every properties/methods of 'MyBean' 
                // under 'Groovy.Test:name=MyBean'
                bean(
                    target:new MyBean(property:"test"),
                    name:"Groovy.Test:name=MyBean"
                )

            }

        and: "a mbean imported from the local server"
            def mbean = new GroovyMBean(ManagementFactory.getPlatformMBeanServer(), 
                "Groovy.Test:name=MyBean")
        expect:
            "test" == mbean.Property
    }
}

此外,对于后人来说,这里有一个稍微修改的例子,模仿常见的“queryNames”方法

import groovy.jmx.builder.JmxBuilder
import spock.lang.Specification
import javax.management.ObjectName
import java.lang.management.ManagementFactory


class SandboxJmxBuilderSpec extends Specification {
    class MyBean {
        def property
    }

    def "launch and test a mbean"() {
        given: "a mbean exported"

        def jmx = new JmxBuilder()
        jmx.export {
            // Export every properties/methods of 'MyBean'
            // under 'Groovy.Test:name=MyBean'
            bean(
                    target:new MyBean(property:"test"),
                    name:"Groovy.Test:name=MyBean"
            )

        }

        and: "a mbean imported from the local server"
        def mbs = ManagementFactory.getPlatformMBeanServer()
        def queryString = "Groovy.Test:name=MyBean"
        def discovery = mbs.queryNames(new ObjectName(queryString), null)

        expect:
        "test" == new GroovyMBean(mbs, discovery[0]).Property
    }
}

此外,对于后人来说,这里有一个稍微修改的例子,模仿常见的“queryNames”方法

import groovy.jmx.builder.JmxBuilder
import spock.lang.Specification
import javax.management.ObjectName
import java.lang.management.ManagementFactory


class SandboxJmxBuilderSpec extends Specification {
    class MyBean {
        def property
    }

    def "launch and test a mbean"() {
        given: "a mbean exported"

        def jmx = new JmxBuilder()
        jmx.export {
            // Export every properties/methods of 'MyBean'
            // under 'Groovy.Test:name=MyBean'
            bean(
                    target:new MyBean(property:"test"),
                    name:"Groovy.Test:name=MyBean"
            )

        }

        and: "a mbean imported from the local server"
        def mbs = ManagementFactory.getPlatformMBeanServer()
        def queryString = "Groovy.Test:name=MyBean"
        def discovery = mbs.queryNames(new ObjectName(queryString), null)

        expect:
        "test" == new GroovyMBean(mbs, discovery[0]).Property
    }
}

谢谢你的这篇文章。从概念上来说,这当然有帮助。您是否创建了bean和属性?我仍然希望Groovy开发人员有一天能够在Groovy中提供一个完整的解决方案。我认为这是一个非常常见的用例。谢谢你的这篇文章。从概念上来说,这当然有帮助。您是否创建了bean和属性?我仍然希望Groovy开发人员有一天能够在Groovy中提供一个完整的解决方案。我认为这是一个非常常见的用例。哇,这可能正是我需要的,谢谢!在这里学到了很多。当您执行bean的jmx.export时,您可以“瞄准”任何pojo或pogo的实例,并给出名称,非常酷!我可以获得一个“MBeanServerConnection”对象,然后使用QueryNames吗?另外,我注意到你在mbean.Property上使用了大写字母P,它是有效的,而.Property不起作用,那里发生了什么?我以前从未在mbean属性名称中使用过大小写。我不知道大写字母。这是mbean属性注册的方式,但我真的不知道为什么。您可以使用带有
MBeanServerConnection
的GroovyMBean,但它不支持查询:您必须执行查询,然后创建一个名为foundWow的GroovyMBean,这可能正是我需要的,谢谢!在这里学到了很多。当您执行bean的jmx.export时,您可以“瞄准”任何pojo或pogo的实例,并给出名称,非常酷!我可以获得一个“MBeanServerConnection”对象,然后使用QueryNames吗?另外,我注意到你在mbean.Property上使用了大写字母P,它是有效的,而.Property不起作用,那里发生了什么?我以前从未在mbean属性名称中使用过大小写。我不知道大写字母。这是mbean属性注册的方式,但我真的不知道为什么。您可以将GroovyMBean与
MBeanServerConnection一起使用,但它不支持查询:您必须执行查询,然后使用找到的名称创建GroovyMBean