Mocking Scala traits containing abstract val members -


i writing swing application following martin fowler's presentation model pattern.

i create traits contain abstract declarations of methods implemented swing components:

trait labelmethods {   def settext(text: string)   //... }  trait mainview {   val somelabel: labelmethods   def setvisible(visible: boolean)   // ... }  class mainframe extends jframe mainview {   val somelabel = new jlabel labelmethods   // ... }  class mainpresenter(mainview: mainview) {   //...   mainview.somelabel.settext("hello")   mainview.setvisible(true) } 

how can mock somelabel member of mainview trait using 1 of open-source mocking frameworks (easymock, mockito, jmockit, etc.) unit testing? there mocking framework, perhaps specific scala can this?

hah! figured out on commute home :-).

scala allows val in concrete class override def in trait.

my traits become:

trait labelmethods {   def settext(text: string)   //... }  trait mainview {   def somelabel: labelmethods    // note member becomes                                  // def in trait...   def setvisible(visible: boolean)   // ... } 

my mainframe class not need change:

class mainframe extends jframe mainview {   val somelabel = new jlabel labelmethods // ...but not change                                                // in class   // ... } 

my test case code looks this:

class testmainpresenter {   @test def testpresenter {     val mocklabel = easymock.createmock(classof[labelmethods])      val mockview = easymock.createmock(classof[mainview])     easymock.expect(mockview.somelabel).andreturn(mocklabel)     //... rest of expectations mocklabel , mockview      val presenter = new mainpresenter(mockview)     //...   } } 

note have not tested this, should work :-).


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -