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

On Thu, Jun 22, 2017 at 12:01 AM, sharanya <[email protected]> wrote:

> 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();
>
>     }
>
> }
>
> --
> >>>>>>>>>> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Arnout Engelen
*Senior Software Engineer*
E: [email protected]
T: https://twitter.com/raboofje

-- 
>>>>>>>>>>      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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to