You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Sumanth <ro...@gmail.com> on 2015/05/27 09:45:19 UTC

unit testing Tapestry Services (5.4)

Hi All,

I'm trying to unit test services (these are already existing services)  and
facing a few challenges.  I have a "ProjectService" which extends a
baseService and this base service has another service which is injected
into it.

Interface IProjectService {

Event getEvents(Long key)
}

ProjectService extends BaseSerivce implements IProjectService {

getEvents(Long key) {

  clientSession = getClientSession();
}
}

BaseService {

@Inject SessionService sessionService;

public ClientSession getClientSession() {

  return sessionService.getClientSession();
}
}

// Test Code //

public class IProjectServiceTest   {

  protected static Registry registry;

  @BeforeClass
  public void beforeTest() {
    registry = new RegistryBuilder().add(AppModule.class).build();
    registry.performRegistryStartup();
  }


  @Test
  public void getEvent() {

    IProjectService projectService =
registry.getService(IProjectService.class);
    Event event = projectService.getEvent(null);

    Assert.assertEquals(event, null);
  }
}
// End ///


I'm trying to test this using testng and when i try to run this i get an
exception in AppModule
FAILED CONFIGURATION: @BeforeClass beforeTest
java.lang.IllegalArgumentException: Contribution
.wp.services.AppModule.CacheManagerCreator(MappedConfiguration) (at
AppModule.java:260) is for service 'interface
org.apache.tapestry5.services.ApplicationStateManager' qualified with
marker annotations [], which does not exist.

and if i extend IprojectServiceTest with "extend TapestryTestCase" then the
exception is
FAILED CONFIGURATION: @BeforeClass beforeTest
java.lang.IllegalArgumentException: Contribution
wp.services.AppModule.contributeComponentRequestHandler(OrderedConfiguration)
(at AppModule.java:108) is for service 'ComponentRequestHandler', which
does not exist.


 I just need to get the clientSession to get my event from a server call.

Thanks in advance,

Re: unit testing Tapestry Services (5.4)

Posted by Lance Java <la...@googlemail.com>.
The ApplicationStateManager is injected by TapestryModule.class you will
either need to add this module to the registry or mock the service in a
test module (eg using mockito). Or, you might choose to split your own
module into smaller logical groups (eg core and web) for easier testing.

You might also find the TapestryIOCJUnit4ClassRunner useful which is
specifically designed to unit test tapestry-ioc services, examples here:

https://github.com/apache/tapestry-5/blob/master/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/