Problem mocking a controller's service

34 views
Skip to first unread message

captainObvious

unread,
Dec 14, 2010, 8:15:07 PM12/14/10
to mockito-flex
I am trying to test my controller's service-to-server calls without
actually invoking the server.

I am declaring [Mock] for the service and also assigning the mock
service to my controller.

However, it is still making the BlazeDS call to the server.

Can anyone see where I'm going wrong?

Thanks !!!

======================================================================

package controller.test
{
import service.UserService;
[Mock(type="service.UserService")]

import model.User;
//[Mock(type="model.User")]

public class UserControllerTestSwiz extends SwizTestCase
{
public var beanie:Bean;

[Rule]
public var mockitoRule:IMethodRule = new
MockitoRule();

public function UserControllerTestSwiz()
{
super();
}

private var _userController:UserController;
//private var mockUser:User;
private var _user:User;
private var mockUserService:UserService;

[Before]
public function setUp():void
{
_userController = new UserController();

this.configSwiz("beanie");

swizConfig = new SwizConfig();
swizConfig.eventPackages = "event.*";
beanProviders = [new
BeanProvider( [_userController] )];

//mockUser = mock(User);
//_user = new User();
mockUserService = mock(UserService);

_userController.userService = mockUserService;
}

[After]
public function tearDown():void
{
_userController = null;
}

[Test(async)]
public function testCreateUser() : void
{
Async.handleEvent( this, swiz.dispatcher,
UserEvent.CREATE_USER_COMPLETE, checkEvent );
Async.handleEvent( this, swiz.dispatcher,
UserEvent.CREATE_USER_ERROR, errorEvent );

_user = UserFactory.createUser();

var eventy : UserEvent = new
UserEvent( UserEvent.CREATE_USER_REQUESTED );
eventy.user = _user;
//eventy.user = mockUser;
swiz.dispatcher.dispatchEvent( eventy );
}

private function checkEvent( event : UserEvent,
passThroughData :
Object ) : void
{
// put assert here
}

private function errorEvent( event : UserEvent,
passThroughData :
Object ) : void
{
// put assert here
}
}

Robert Penner

unread,
Dec 15, 2010, 2:16:08 AM12/15/10
to mockit...@googlegroups.com
You need to put the metadata above the property, not below.

Robert

> --
> You received this message because you are subscribed to the Google Groups "mockito-flex" group.
> To post to this group, send email to mockit...@googlegroups.com.
> To unsubscribe from this group, send email to mockito-flex...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mockito-flex?hl=en.
>
>

Kris

unread,
Dec 15, 2010, 3:21:06 AM12/15/10
to mockit...@googlegroups.com
Hi,

I think the metadata in this case is fine. It's actually under import not the property. You can put [Mock] metadata either over a property to ensure it's prepared and assigned or over a class to ensure preparation only.

It's really hard to say what happens behind the scene. The test case looks good. I'd make sure that swiz really uses your UserController instance. I can see you create the controller within the test but I'm not so sure where you put it in the swiz context (is it the beanProviders assignment?). I'd try to make sure that it is your instance of controller that's being called from swiz.

Regards,
Kris

captainObvious

unread,
Dec 15, 2010, 7:18:54 PM12/15/10
to mockito-flex
I think Mockito-Flex is confusing Swiz or vice versa.

If I assign the mock BEFORE configuring swiz, I get a null object
reference for the dispatcher.

If I assign the mock AFTER configuring swiz, the test runs but calls
the real userService versus the mock one.

Runs the test but does not assign the mockUserService (makes real call
to the server):
======================================================================================
public class UserControllerTestSwiz extends SwizTestCase
{
[Rule]
public var mockitoRule:IMethodRule = new MockitoRule();

public function UserControllerTestSwiz()
{
super();
}

private var _userController:UserController;
//private var mockUser:User;
private var _user:User;

[Mock(type="service.UserService")]
private var mockUserService:UserService;

[Before]
public function setUp():void
{
//mockUserService = mock(UserService);
//_userController.userService = mockUserService;

_userController = configSwiz("userController");

mockUserService = mock(UserService);
_userController.userService = mockUserService;
}

[After]
public function tearDown():void
{
_userController = null;
}

[Test(async)]
public function testCreateUser() : void
{
_user = UserFactory.createUser();
//Async.handleEvent( this, swiz.dispatcher,
UserEvent.CREATE_USER_ERROR, errorEvent );

//
Async.proceedOnEvent(this,swiz.dispatcher,UserEvent.CREATE_USER_REQUESTED,
5000,timeoutHandler);
//
Async.proceedOnEvent(this,swiz.dispatcher,UserEvent.CREATE_USER_COMPLETE,
5000,timeoutHandler);

//Async.handleEvent( this, swiz.dispatcher,
UserEvent.CREATE_USER_COMPLETE, checkEvent, 1000, _user,
timeoutHandlerCreateUserComplete );

var eventy : UserEvent = new
UserEvent( UserEvent.CREATE_USER_REQUESTED );
eventy.user = _user;
swiz.dispatcher.dispatchEvent( eventy );
}

private function checkEvent( event : UserEvent, passThroughData :
Object ) : void
{
// put assert here
}

/*
protected function verifyUpdate(event:Event,
passThroughData:Object):void {
var updatedEmployee:Employee = passThroughData as Employee;
Assert.assertEquals(pm.tempEmployee.firstName,
updatedEmployee.firstName);
}
*/

private function errorEvent( event : UserEvent, passThroughData :
Object ) : void
{
// put assert here
}

private function timeoutHandlerCreateUserComplete(passThroughData :
Object):void
{
Assert.fail("Timeout waiting for CreateUserComplete");
}

protected function
dispatchingEventNeverOccurred( passThroughData:Object ):void
{
Assert.fail( 'event is not dispatched');
}
}



Fails with null object reference error when trying to dispatch event
in the test case:
======================================================================================
public class UserControllerTestSwiz extends SwizTestCase
{
[Rule]
public var mockitoRule:IMethodRule = new MockitoRule();

public function UserControllerTestSwiz()
{
super();
}

private var _userController:UserController;
//private var mockUser:User;
private var _user:User;

[Mock(type="service.UserService")]
private var mockUserService:UserService;

[Before]
public function setUp():void
{
mockUserService = mock(UserService);

_userController = configSwiz("userController");

//mockUserService = mock(UserService);
_userController.userService = mockUserService;
}

[After]
public function tearDown():void
{
_userController = null;
}

[Test(async)]
public function testCreateUser() : void
{
_user = UserFactory.createUser();
//Async.handleEvent( this, swiz.dispatcher,
UserEvent.CREATE_USER_ERROR, errorEvent );

//
Async.proceedOnEvent(this,swiz.dispatcher,UserEvent.CREATE_USER_REQUESTED,
5000,timeoutHandler);
//
Async.proceedOnEvent(this,swiz.dispatcher,UserEvent.CREATE_USER_COMPLETE,
5000,timeoutHandler);

//Async.handleEvent( this, swiz.dispatcher,
UserEvent.CREATE_USER_COMPLETE, checkEvent, 1000, _user,
timeoutHandlerCreateUserComplete );

var eventy : UserEvent = new
UserEvent( UserEvent.CREATE_USER_REQUESTED );
eventy.user = _user;
swiz.dispatcher.dispatchEvent( eventy );
}

private function checkEvent( event : UserEvent, passThroughData :
Object ) : void
{
// put assert here
}

/*
protected function verifyUpdate(event:Event,
passThroughData:Object):void {
var updatedEmployee:Employee = passThroughData as Employee;
Assert.assertEquals(pm.tempEmployee.firstName,
updatedEmployee.firstName);
}
*/

private function errorEvent( event : UserEvent, passThroughData :
Object ) : void
{
// put assert here
}

private function timeoutHandlerCreateUserComplete(passThroughData :
Object):void
{
Assert.fail("Timeout waiting for CreateUserComplete");
}

protected function
dispatchingEventNeverOccurred( passThroughData:Object ):void
{
Assert.fail( 'event is not dispatched');
}
}


Stack trace for the null object reference:
======================================================================================
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
**[ at controller.test::UserControllerTestSwiz/
testCreateUser(UserControllerTestSwiz.as:78) ]
at Function/http://adobe.com/AS3/2006/builtin::apply
at flex.lang.reflect::Method/apply(Method.as:210)
at org.flexunit.runners.model::FrameworkMethod/
invokeExplosively(FrameworkMethod.as:201)
at org.flexunit.internals.runners.statements::InvokeMethod/
evaluate(InvokeMethod.as:72)
at org.flexunit.internals.runners.statements::ExpectAsync/
evaluate(ExpectAsync.as:595)
at org.flexunit.internals.runners.statements::RunBeforesInline/
handleSequenceExecuteComplete(RunBeforesInline.as:41)
at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:
119)
at org.flexunit.internals.runners.statements::AsyncStatementBase/
sendComplete(AsyncStatementBase.as:76)
at org.flexunit.internals.runners.statements::StatementSequencer/
sendComplete(StatementSequencer.as:172)
at org.flexunit.internals.runners.statements::StatementSequencer/
handleChildExecuteComplete(StatementSequencer.as:145)
at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:
119)
at org.flexunit.internals.runners.statements::InvokeMethod/
evaluate(InvokeMethod.as:75)
at org.flexunit.internals.runners.statements::SequencerWithDecoration/
executeStep(SequencerWithDecoration.as:100)
at org.flexunit.internals.runners.statements::StatementSequencer/
handleChildExecuteComplete(StatementSequencer.as:141)
at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:
119)
at org.flexunit.internals.runners.statements::InvokeMethod/
evaluate(InvokeMethod.as:73)
at org.flexunit.internals.runners.statements::SequencerWithDecoration/
executeStep(SequencerWithDecoration.as:100)
at org.flexunit.internals.runners.statements::StatementSequencer/
handleChildExecuteComplete(StatementSequencer.as:141)
at org.flexunit.internals.runners.statements::StatementSequencer/
evaluate(StatementSequencer.as:109)
at org.flexunit.internals.runners.statements::RunBeforesInline/
evaluate(RunBeforesInline.as:36)
at org.flexunit.internals.runners.statements::RunAftersInline/
evaluate(RunAftersInline.as:39)
at org.flexunit.internals.runners.statements::StatementSequencer/
executeStep(StatementSequencer.as:98)
at org.flexunit.internals.runners.statements::StatementSequencer/
handleChildExecuteComplete(StatementSequencer.as:141)
at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:
119)
at org.mockito.integrations.flexunit4::AssignMocks/
assignMocks(AssignMocks.as:45)
at org.mockito.integrations.flexunit4::AssignMocks/
evaluate(AssignMocks.as:25)
at org.flexunit.internals.runners.statements::StatementSequencer/
executeStep(StatementSequencer.as:98)
at org.flexunit.internals.runners.statements::StatementSequencer/
handleChildExecuteComplete(StatementSequencer.as:141)
at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:
119)
at Function/org.mockito.integrations.flexunit4:PrepareMocks/
private:prepareMocks/
org.mockito.integrations.flexunit4:repositoryPreparedHandler(PrepareMocks.as:
44)
at Function/<anonymous>(AsmockMockery.as:79)
at flash.events::EventDispatcher/dispatchEventFunction
at flash.events::EventDispatcher/dispatchEvent
at CompletedEventDispatcher/addEventListener(ProxyRepositoryImpl.as:
224)
at org.mockito.impl::AsmockMockery/prepareClasses(AsmockMockery.as:86)
at org.mockito::Mockito/prepareClasses(Mockito.as:236)
at org.mockito.integrations.flexunit4::PrepareMocks/
prepareMocks(PrepareMocks.as:40)
at org.mockito.integrations.flexunit4::PrepareMocks/
evaluate(PrepareMocks.as:23)
at org.flexunit.internals.runners.statements::StatementSequencer/
executeStep(StatementSequencer.as:98)
at org.flexunit.internals.runners.statements::StatementSequencer/
handleChildExecuteComplete(StatementSequencer.as:141)
at org.flexunit.internals.runners.statements::StatementSequencer/
evaluate(StatementSequencer.as:109)
at org.flexunit.internals.runners.statements::MethodRuleBase/
proceedToNextStatement(MethodRuleBase.as:21)
at org.mockito.integrations.flexunit4::MockitoRule/
evaluate(MockitoRule.as:50)
at org.flexunit.internals.runners.statements::StackAndFrameManagement/
handleTimerComplete(StackAndFrameManagement.as:135)
at flash.events::EventDispatcher/dispatchEventFunction
at flash.events::EventDispatcher/dispatchEvent
at flash.utils::Timer/tick


Superclass SwizTestCase:
======================================================================================
public class SwizTestCase extends AutowiredTestCase
{
public var swizBeans:Beans;

[Dispatcher]
public var dispatcher:IEventDispatcher;

public function SwizTestCase() {
super();
}

/**
* Returns a bean of the type found in the BeanProvider with the
* supplied ID value and initializes everything.
*
* @param beanId id of bean in BeanProvider
* @return bean found from supplied id
*/
public function configSwiz(beanId:String):* {
// initialize Swiz

swizConfig = new SwizConfig();
swizConfig.eventPackages = "event.*";

swizBeans = new Beans(); //<= Beans is the name
of our BeanProvider
beanProviders = [swizBeans]; //<= This initializes
everything,or you're getting null in the dispatcher (it's
notinitialized)
constructSwizContext(); //<= initialize
return lookupBean(beanId);
}

public function getSwizBean(id:String):* {
return lookupBean(id);
}

public function lookupBean(beanId:String):* {
var bean:*;
// lookup the bean
try {
bean = swizBeans[beanId];
}
catch (e:Error) {
throw new Error("Unable to locate bean with id: " + beanId);
}
return bean; //<= gets
you the singleton instance of the bean specified by the id

Kris

unread,
Dec 16, 2010, 2:45:52 AM12/16/10
to mockit...@googlegroups.com
Could you please clean up you examples so that there are only meaningful sections? It's really hard to follow.

Regards,
Kris

captainObvious

unread,
Dec 16, 2010, 4:54:42 AM12/16/10
to mockito-flex
Is this better?

public class UserControllerTestSwiz extends SwizTestCase
{
[Rule]
public var mockitoRule:IMethodRule = new MockitoRule();

public function UserControllerTestSwiz()
{
super();
}

private var _userController:UserController;
private var _user:User;

[Mock(type="service.UserService")]
private var mockUserService:UserService;

[Before]
public function setUp():void
{
mockUserService = mock(UserService);

_userController = configSwiz("userController");

_userController.userService = mockUserService;
}

[After]
public function tearDown():void
{
_userController = null;
}

[Test(async)]
public function testCreateUser() : void
{
_user = UserFactory.createUser();

Async.handleEvent( this, swiz.dispatcher,
UserEvent.CREATE_USER_COMPLETE, checkEvent, 1000, _user,
timeoutHandlerCreateUserComplete );

var eventy : UserEvent = new
UserEvent( UserEvent.CREATE_USER_REQUESTED );
eventy.user = _user;
swiz.dispatcher.dispatchEvent( eventy );
}

private function checkEvent( event : UserEvent, passThroughData :
Object ) : void
{
// put assert here
}

private function timeoutHandlerCreateUserComplete(passThroughData :
Object):void
{
Assert.fail("Timeout waiting for CreateUserComplete");
}
}

Kris

unread,
Dec 18, 2010, 4:06:05 PM12/18/10
to mockit...@googlegroups.com
Yes much better now. Thanks!

So you say that in case 1 a dispatcher is null? But mockito does not
deal with swiz dispatcher at all. Case wher you assign after config
seems reasonable since your beans get wired with the real service not
mock.

I'll try your example and try to give you some clue.

Regards

Reply all
Reply to author
Forward
0 new messages