[Lift] Re: How can I use unit tests with S.?(key) ?

2009-03-11 Thread marius d.

Can you try

S.initIfUninitted(liftSession) {

   do your code here
}

?
... but you'd still need to create a dumb LiftSession


Br's,
Marius


On Mar 11, 8:40 pm, Joachim A. wallaby.po...@googlemail.com wrote:
 Hi,
 I have a unit test which call a method which uses the locale method S.?
 (String). It throws a nullpointer, though.

 Did I forget to setup things somewhere? Or can't I use S.? without a running
 request?
 The call to S.? works fine in a running web application.

 Thanks a lot,
 Joachim

 The beginning of the backtrace is:

 java.lang.NullPointerException
         at net.liftweb.http.S$.$qmark$bang(S.scala:288)
         at net.liftweb.http.S$.$qmark(S.scala:263)
         at net.liftweb.http.S$.$qmark(S.scala:277)
         at
 com.ansorgit.frontend.SubscriptionMgr$.subscribeToNewsletter(SubscriptionMgr.scala:65)
         at
 com.ansorgit.frontend.SubscriptionMgrTest$$anonfun$testAlreadySubscribed$1.apply(SubscriptionMgrTest.scala:37)
         at
 com.ansorgit.frontend.SubscriptionMgrTest$$anonfun$testAlreadySubscribed$1.apply(SubscriptionMgrTest.scala:33)
         at
 net.liftweb.mapper.DB$$anon$1.net$liftweb$mapper$DB$$anon$$doWith(DB.scala:117)
         at
 net.liftweb.mapper.DB$$anon$1$$anonfun$net$liftweb$mapper$DB$$anon$$doWith$1.apply(DB.scala:118)
         at
 net.liftweb.mapper.DB$$anon$1$$anonfun$net$liftweb$mapper$DB$$anon$$doWith$1.apply(DB.scala:118)
         at net.liftweb.mapper.DB$.use(DB.scala:305)
         at
 net.liftweb.mapper.DB$$anon$1.net$liftweb$mapper$DB$$anon$$doWith(DB.scala:118)
         at net.liftweb.mapper.DB$$anon$1.apply(DB.scala:124)
         at com.ansorgit.BaseDBTest.runInDb(BaseTest.scala:44)
         at
 com.ansorgit.frontend.SubscriptionMgrTest.testAlreadySubscribed(SubscriptionMgrTest.scala:33)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at
 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: How can I use unit tests with S.?(key) ?

2009-03-11 Thread etorreborre

Hi,

I don't know how this could help because I've been away from lift code
for a long time. But I remember that I tried to use mocks with JMock
and specs in order to specify the example site (\liftweb\sites
\example\src\test\scala\net\liftweb\example\snippet).

The idea was to simulate a session:

inSession {
 // do your test here
}

This works with the following code (see the WikiSpec.scala file):


class MockRequest extends Specification with JMocker with ClassMocker
{
  var request: Req = mock[Req]
  var httpRequest: HttpServletRequest = mock[HttpServletRequest]
  var session: LiftSession = mock[LiftSession]
  def createMocks = {
request = mock[Req]
httpRequest = mock[HttpServletRequest]
session = mock[LiftSession]
expect {
  0.atLeastOf(httpRequest).getCookies
  0.atLeastOf(request).request.willReturn(httpRequest)
}
  }
  def inSession(f: = Any) {
S.init(request, session) {
  f
}
  }
  def unsetParameter(name: String) {
expect {
  0.atLeastOf(request).param(equal(name)).willReturn(None)
}
  }
  def setParameter(name: String, value: String) {
expect {
  0.atLeastOf(request).param(equal(name)).willReturn(Some(value))
}
  }
}



Unfortunately, I remember that we had compilation issues when
upgrading to scala-2.7.2 and as a result, most of the code is
commented out.

I've recently started to integrate Mockito and Powermock to specs and
that may prove a decent way to support unit testing using S methods.

Eric.



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---