Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6637521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:19:32+00:00 2026-05-25T23:19:32+00:00

prerequisites: I’m using the latest version of the Play! framework , and the Java

  • 0

prerequisites: I’m using the latest version of the Play! framework, and the Java version (not Scala).

I need to publish a message to a message queue when a user is created, and I’d like to test that behaviour. My issue is making this easily testable.

The Controller approach

In other frameworks, what I would’ve done would be to use constructor injection in the controller and pass in a mocked queue in my tests; however, with Play! the controllers are static, that means I can’t do new MyController(mockedQueue) in my tests.

I could use Google Guice and put an @Inject annotation on a static field in my controller, but that doesn’t feel very nice to me, as it either means I have to make the field public to be replaced in the test, or I have to use a container in my tests. I’d much prefer to use constructor injection, but Play! doesn’t seem to facilitate that.

The Model approach

It’s often said your logic should be in your model, not your controller. That makes sense; however, we’re not in Ruby here and having your entities interact with external services (email, message queues etc…) is considerably less testable than in a dynamic environment where you could just replace your MessageQueue static calls with a mocked instance at will.

If I make my entity call off to the queue, how is that testable?

Of course, both these situations are unnecessary if I do end-to-end integration tests, but I’d rather not need a message queue or SMTP server spun up for my tests to run.

So my question is: How do I model my Play! controllers and/or models to facilitate testing interactions with external services?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:19:33+00:00Added an answer on May 25, 2026 at 11:19 pm

    As I see, there’s not a clean solution for this.

    You could use an Abstract Factory for your dependencies. This factory could have setter methods for the objects it produces.

    public class MyController {
        ...
        private static ServiceFactory serviceFactory = ServiceFactory.getInstance();
        ...
        public static void action() {
            ...
            QueueService queue = serviceFactory.getQueueService();
            ...
        }
    

    }

    Your test would look like this:

    public void testAction() {
        QueueService mock = ...
        ...
        ServiceFactory serviceFactory = ServiceFactory.getInstance();
        serviceFactory.setQueueService(mock);
        ...
        MyController.action();
        verify(mock);
    }
    

    If you don’t want to expose the setter methods of the factory, you could create an interface and configure the implementing class in your tests.

    Another option would be the use o PowerMock for mocking static methods. I’ve used before and it works relatively fine for most cases. Just don’t overuse it, or you’re in maintenance hell…

    And finally, since your willing to use Guice in your application, this could be a viable option.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a Makefile where prerequisites using target specific variables version= target1:
I am using gcc -MM to generate prerequisites of dependencies rules for compiling source
VS2008's Setup Projects have a handful of built-in prerequisites (e.g. .NETfx version, VSTO, PowerPacks)
#include<iostream> #include<string> using namespace std; class Prerequisites { public: void orderClasses(string* Input); }; void
Googling reveals many technical details and framework/OS prerequisites, but it seems to be hard
I am using ClickOnce and I would like to add prerequisites to my applications.
I am using clickonce to distribute my VB.net windows application. It has several prerequisites,
I need to check SQL Server versions as prerequisites for my software. I found
I've got a multi layer nested form User->Tasks->Prerequisites and in the same form User->Tasks->Location
Are there any prerequisites I need to know before I build a website/web application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.