Unit testing 如何在控制器内模拟公共方法

Unit testing 如何在控制器内模拟公共方法,unit-testing,grails,junit,Unit Testing,Grails,Junit,我有一个控制器,它有很多动作。在操作内部,它正在调用驻留在同一类中的其他方法。我怎样才能模仿这些方法,让它返回我想要的东西呢 SomeController{ def action1={ method1() //i want to mock this method. this is not a service method } public def method1(){ //some code here }

我有一个控制器,它有很多动作。在操作内部,它正在调用驻留在同一类中的其他方法。我怎样才能模仿这些方法,让它返回我想要的东西呢

SomeController{

    def action1={

       method1() //i want to mock this method. this is not a service method

       }

    public def method1(){
        //some code here
       }


    def action2={

       method2() // i want to mock this method
     }

    public def method2(){

       //some code here
     }



}// SomeController
我想模拟method1()和method2(),使其返回我定义的内容。 提前感谢您的帮助

回答我自己

 controller.metaClass.method1(){

  //define whatever require
  // some codes

 }

 controller.metaClass.method2(){

 // define whatever require
 // some code

 }
上述代码运行良好

谢谢你的时间