How to test module lifecyle stop hook ?

335 views
Skip to first unread message

Thibault Meyer

unread,
Feb 14, 2017, 4:06:52 AM2/14/17
to Play Framework
Hi,

I test my module with a fake application, but how I can simulate the call to ApplicationLifeCycle hook setted on the module ? (this hook is used to close some TCP connections).

Marcos Pereira

unread,
Feb 16, 2017, 1:02:56 AM2/16/17
to play-fr...@googlegroups.com
Hey Thibault,

You can create your own implementation of ApplicationLifecycle and use it at your tests, instead the default one. To use it at your tests, you can override the bind using GuiceApplicationBuilder.overrides.

Best.

On Tue, Feb 14, 2017 at 1:06 AM, Thibault Meyer <thibaul...@payintech.com> wrote:
Hi,

I test my module with a fake application, but how I can simulate the call to ApplicationLifeCycle hook setted on the module ? (this hook is used to close some TCP connections).

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/9a97b053-7dc3-49ba-aad2-df11b09db5c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcos Pereira
Software Engineer, Lightbend.com

Thibault Meyer

unread,
Feb 16, 2017, 1:47:36 AM2/16/17
to Play Framework
Hi Marcos,

I try this by using Mockito, the code compile and run, but the Java interface don't have all methods. Java interface don't have the method "stop" only the Scala version. So the module itself can use addStopHook but I don't have any method to trigger the call of the registered stop hooks.


Sincerly



Le jeudi 16 février 2017 07:02:56 UTC+1, Marcos Pereira a écrit :
Hey Thibault,

You can create your own implementation of ApplicationLifecycle and use it at your tests, instead the default one. To use it at your tests, you can override the bind using GuiceApplicationBuilder.overrides.

Best.
On Tue, Feb 14, 2017 at 1:06 AM, Thibault Meyer <thibaul...@payintech.com> wrote:
Hi,

I test my module with a fake application, but how I can simulate the call to ApplicationLifeCycle hook setted on the module ? (this hook is used to close some TCP connections).

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

Marcos Pereira

unread,
Feb 21, 2017, 5:47:15 PM2/21/17
to play-fr...@googlegroups.com
Hi Thibault, sorry for the long time to reach you back.

Can't you use play.test.Helpers.start and play.test.Helpers.start here? If I understand you properly, you were talking about play.Application not having a stop method, right?

Best.

To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/9da23356-b79e-4298-9681-d579c2b88256%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Thibault Meyer

unread,
Feb 22, 2017, 4:04:09 AM2/22/17
to Play Framework
Hi Marcos,


the main issue is regarding to Scala implementation, the Java implementation is not complete.
 

The interface ApplicationLifecycle (Java) only have one method:

void addStopHook(Callable<? extends CompletionStage<?>> hook)


But the trait ApplicationLifecycle (Scala) have two methods:

def addStopHook(hook: () => Future[_])


def stop()

 

The method stop is missing from the Java interface.


On my Junit tests, I currently use Mockito and fakeApplication



@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RedisTest {

   
private RedisModule redisModule;

   
private ApplicationLifecycle applicationLifecycle;
   
   
@Before
   
public void initializeRedisModule() {
       
if (this.redisModule == null) {
           
final Application application = Helpers.
                    fakeApplication
(new HashMap<String, Object>() {{
                        put
("redis.default.db.default", 0);
                        put
("redis.default.host", "127.0.0.1");
                        put
("redis.default.port", 6379);
                   
}});
           
this.applicationLifecycle = mock(ApplicationLifecycle.class);
           
this.redisModule = new RedisModuleImpl(
                   
this.applicationLifecycle
                    application
.configuration()
           
);
           
Assert.assertNotEquals(null, this.redisModule);
       
}
   
}

   
@After
   
public void closeRedisModule() {
       
if (this.applicationLifecycle != null) {
           
this.applicationLifecycle.stop(); // NOT AVAILABLE ON JAVA IMPLEMENTATION
       
}
   
}
}





Will Sargent

unread,
Feb 22, 2017, 8:47:20 PM2/22/17
to play-fr...@googlegroups.com
This isn't the case -- the DefaultApplicationLifecycle implementation has the stop() method, but it is not present in the ApplicationLifecycle trait.

To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/d62c8650-36ea-43bb-983e-b7c88048a879%40googlegroups.com.

Thibault Meyer

unread,
Feb 23, 2017, 1:45:10 AM2/23/17
to Play Framework
You right, so why using interface or trait if implementation does not respect it ?

Will Sargent

unread,
Feb 23, 2017, 12:05:40 PM2/23/17
to play-fr...@googlegroups.com
What do you mean?  You said earlier:

> But the trait ApplicationLifecycle (Scala) have two methods:

> def addStopHook(hook: () => Future[_])
> def stop()

That isn't the case.  The trait doesn't have a stop() method.  There's an implementation that does, but it's not on the trait.


--
Will Sargent
Engineer, Lightbend, Inc.


To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/79fdd3cd-65fd-43e5-8e8f-3bf0ab7c86a9%40googlegroups.com.

Thibault Meyer

unread,
Feb 23, 2017, 1:07:10 PM2/23/17
to Play Framework
Ok I understand. So, in Java, they is a solution to execute set stop hook from a Mock instance of ApplicationLifeCycle ?

Sincerly

Will Sargent

unread,
Feb 23, 2017, 6:31:57 PM2/23/17
to play-fr...@googlegroups.com
If you want to attach some behavior to a mock, you can use an answer:


although a fake might be more appropriate...



--
Will Sargent
Engineer, Lightbend, Inc.


To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/3e5879e6-9531-40d6-99c6-93a832542f8d%40googlegroups.com.

Thibault Meyer

unread,
Feb 24, 2017, 2:05:53 AM2/24/17
to Play Framework
I'm using FakeApplication too, but Application don't have getter to retrieve instance of ApplicationLifeCycle. Is using DI possible with FakeApplication with application.injection().instanceof(...) ?


/.../

if (this.redisModule == null) {
           
final Application application = Helpers.
                    fakeApplication
(new HashMap<String, Object>() {{
                        put
("redis.default.db.default", 0);
                        put
("redis.default.host", "127.0.0.1");
                        put
("redis.default.port", 6379);
                   
}});

           
this.applicationLifecycle = mock(ApplicationLifecycle.class);  // MOCK

           
this.redisModule = new RedisModuleImpl(
                   
this.applicationLifecycle
                    application
.configuration()
           
);
           
Assert.assertNotEquals(null, this.redisModule);
       
}
/.../
Reply all
Reply to author
Forward
0 new messages