0

I am trying to assert list of User defined objects which are returning by mule flow like below testcase.

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Assert;
import org.junit.Test;
import org.mule.api.MuleMessage;
import org.mule.api.client.LocalMuleClient;
import org.mule.tck.junit4.FunctionalTestCase;
import org.mule.transport.http.ReleasingInputStream;


public class UnitedTest  extends FunctionalTestCase{

    @Override
    protected String getConfigFile() {
        return "src/main/app/united.xml";

    }
    @Test
    public void sampletest() throws Exception {
        LocalMuleClient client =  muleContext.getClient();
        MuleMessage response = client.send("http://localhost:8083/united/CLE", "", null, 1000000);
            Assert.assertNotNull(response)  ;
            System.out.println(response.getPayload());
            Object obj = response.getPayload();
            ArrayList<Object> payloadObj = (ArrayList<Object>) obj;
            Object resObj = (Object) payloadObj.get(0);

            if(resObj instanceof Flight){

                Flight flght = (Flight)resObj;
                Assert.assertNotNull(flght);

            }
    }
    }

I am getting classCastException.

java.lang.ClassCastException: org.mule.transport.http.ReleasingInputStream cannot be cast to java.util.ArrayList
    at UnitedTest.sampletest(UnitedTest.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:46)
    at org.junit.internal.runners.statements.FailOnTimeout$1.run(FailOnTimeout.java:28)

Any suggestions how to assert size of arraylist and each object members?

Paco Abato
  • 3,920
  • 4
  • 31
  • 54

2 Answers2

0

I don't see how you can pass a java object in HTTP. You'll need to serialize somehow. Probably to xml, json or similar.

Víctor Romero
  • 5,107
  • 2
  • 22
  • 32
  • Hi Victor, i am not passing java object in HTTP. sending a request with an parameter and rest web service returns a JSON object and we are converting in to java object further to do apply some business logic. its working fine when we run actual mule flow but thing is how to get list of objects from org.mule.transport.http.ReleasingInputStream when we run junit to perform assertion – Hemadri Rangisetty Feb 25 '15 at 14:17
0

Well there are a few things to comment there. The assertion you are trying to do (should the object was correct) will only validate the type of the payload, which is not much of an assertion. May be you can try to do some sort of comparison. If you just need to validate the type of the payload I you could also try the method isAssignableFrom, as it will cover the class and its parent (check this link java: Class.isInstance vs Class.isAssignableFrom).

But anyway regarding your particular error, the exception is telling you you can not do this cast:

  ArrayList<Object> payloadObj = (ArrayList<Object>) obj;

That is because after you use the client to call the http endpoint it's returning an input stream, which you should first consume before do such casting.

If the http endpoint is not really important for your test you could do a flow ref. If not you could check some of the mule transformers.

HTH

Community
  • 1
  • 1
Dds
  • 712
  • 5
  • 7