java.lang.IllegalStateException: missing behavior definition for the preceding method call getMessag

Posted by user362199 on Stack Overflow See other posts from Stack Overflow or by user362199
Published on 2010-06-09T08:34:09Z Indexed on 2010/06/09 11:12 UTC
Read the original article Hit count: 484

Filed under:
|
|
|

Hi All,

I'm using EasyMock(version 2.4) and TestNG for writing UnitTest.

I have a following scenario and I cannot change the way class hierarchy is defined.

I'm testing ClassB which is extending ClassA.

ClassB look like this

public class ClassB extends ClassA {

public ClassB() { super("title"); }

@Override
public String getDisplayName() 
{
    return ClientMessages.getMessages("ClassB.title");
}

}

ClassA code

public abstract class ClassA { private String title;

public ClassA(String title)
{
    this.title = ClientMessages.getMessages(title);
}

public String getDisplayName()
{
    return this.title;
} }

ClientMessages class code

public class ClientMessages { private static MessageResourse messageResourse;

public ClientMessages(MessageResourse

messageResourse) { this.messageResourse = messageResourse; } public static String getMessages(String code) { return messageResourse.getMessage(code);

} }

MessageResourse Class code

public class MessageResourse { public String getMessage(String code) { return code; }

}

Testing ClassB

import static org.easymock.classextension.EasyMock.createMock;

import org.easymock.classextension.EasyMock;
import org.testng.Assert;
import org.testng.annotations.Test;

public class ClassBTest
{
    private MessageResourse mockMessageResourse = createMock(MessageResourse.class);
    private ClassB classToTest;
    private ClientMessages clientMessages;


    @Test
    public void testGetDisplayName()
    {

    EasyMock.expect(mockMessageResourse.getMessage("ClassB.title")).andReturn("someTitle");

        clientMessages = new ClientMessages(mockMessageResourse);

        classToTest = new ClassB();

        Assert.assertEquals("someTitle" , classToTest.getDisplayName());
        EasyMock.replay(mockMessageResourse);
    }
}

When I'm running this this test I'm getting following exception:

java.lang.IllegalStateException: missing behavior definition for the preceding method call getMessage("title")

While debugging what I found is, it's not considering the mock method call mockMessageResourse.getMessage("ClassB.title") as it has been called from the construtor (ClassB object creation).

Can any one please help me how to test in this case.

Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about unit-testing