Mock actorRef.tell inside an Actor class

210 views
Skip to first unread message

sharanya

unread,
Jun 22, 2017, 12:15:07 AM6/22/17
to Akka User List

I have an actor class EmployeeActor, inside that actor, some other actor is fired using payrollRunActor.tell(). I need to write a JUnit test for EmployeeActor.java, but I don't want to fire payrollRunActor.tell(), means I want to mock it.

Is there a way to do it? I tried a lot, but real payrollRunActor is getting fired. Here is the actual code of my EmployeeActor class.


package com.test.actors;

import java.util.List;


import org.apache.commons.lang3.RandomStringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import com.test.avs.domain.boundedcontext.Employee;

import com.test.avs.domain.boundedcontext.PayrollRun;

import com.test.entity.BusinessDTO;

import com.test.periodic.actors.aggregrators.EmployeeAggregator;


import akka.actor.AbstractActor;

import akka.actor.ActorRef;

import akka.actor.ActorSystem;

import akka.actor.Props;

import akka.routing.RoundRobinPool;


public class EmployeeActor extends AbstractActor {

    private static final Logger logger = LoggerFactory.getLogger(EmployeeActor.class);

    private boolean rollup;


    public static Props props() {

        return Props.create(EmployeeActorTest.class);

    }


    private List<PayrollRun> payrollRuns;

    private String instanceId;

    private String employeeAggregatorId;

    private Employee employee;

    private ActorRef organizationAggregatorActor;

    private List<BusinessDTO> businessDTOs;


    final ActorSystem payrollRunSystem = ActorSystem.create("payrollRun");


    ActorRef employeeAggregator;


    public EmployeeActor(ActorRef organizationAggregatorActor, List<PayrollRun> payrollRuns,

            Employee employee, List<BusinessDTO> businessDTOs, boolean rollup) {

        this.payrollRuns = payrollRuns;

        this.employee = employee;

        this.organizationAggregatorActor = organizationAggregatorActor;

        this.businessDTOs = businessDTOs;

        this.rollup = rollup;

    }


    @Override

    public void preStart() throws Exception {

        instanceId = RandomStringUtils.randomAlphanumeric(6);

        employeeAggregatorId = "employeeAggregator-" + instanceId;

        employeeAggregator = getContext().system().actorOf(

                Props.create(EmployeeAggregator.class, organizationAggregatorActor, employee),

                employeeAggregatorId);

        super.preStart();

    }


    @Override

    public Receive createReceive() {

        return receiveBuilder().match(Employee.class, employee -> {


            if (rollup) {

                logger.info("Rollingup business entities.");

                employeeAggregator.tell(employee, getSelf());

            } else {

                ActorRef payrollRunActor = payrollRunSystem.actorOf(new RoundRobinPool(payrollRuns.size())

                        .props(Props.create(PayrollRunActor.class, employeeAggregator, employee, businessDTOs)));

                for (PayrollRun payrollRun : payrollRuns) {

                    **payrollRunActor.tell(payrollRun, getSelf());**

                }

            }



        }).match(PayrollRun.class, maxPaydatePayrollRun -> {

            ActorRef payrollRunActor = payrollRunSystem

                    .actorOf(Props.create(PayrollRunActor.class, employeeAggregator, employee, businessDTOs));

            **payrollRunActor.tell(maxPaydatePayrollRun, getSelf());**

        }).build();

    }

}

Arnout Engelen

unread,
Jun 22, 2017, 5:16:47 AM6/22/17
to akka...@googlegroups.com
Hi Sharanya,

Thanks for your question. First of all I noticed you're creating a new ActorSystem inside this actor. That's probably not what you want: you typically create just 1 ActorSystem per application, and use "context.actorOf" to create new (child) actors from this actor. More information on creating actors can be found at http://doc.akka.io/docs/akka/current/java/actors.html#creating-actors-with-props .

You're indeed creating a new PayrollRunActor at the line above the line highlighted by you. One way to 'mock this away' in your test might be to move creation of the PayrollRunActor to a helper method, and run your test with a subclass of this actor where the helper method is overridden by a method that returns a probe. More information on testing actors like this can be found at http://doc.akka.io/docs/akka/current/java/testing.html#using-multiple-probe-actors .


Kind regards,

Arnout

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Arnout Engelen

sharanya

unread,
Jun 23, 2017, 1:36:21 AM6/23/17
to Akka User List
Hi Arnout,

Thank you for your response. 
Actually i was trying to  create  child actors dynamically based on the some count and it might vary, like if  Employer is the parent actor and employee is the child actor .so one employer can have different no of employee .so i wanted to created child actors based on count which i get from Database.


Thank you
sharanya

Reply all
Reply to author
Forward
0 new messages