-1

I am using EasyMock to write unit tests for my blackjack game. But I get java.lang.IllegalArgumentException: Cannot subclass final class class com.blackjack.game.cards.Card error.

Although, I feel I am doing it the right way. Here is my testclass :

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.easymock.EasyMock;
import org.easymock.EasyMockRunner;
import org.easymock.EasyMockSupport;

@RunWith(EasyMockRunner.class)
public class handTest extends EasyMockSupport{
    private Hand hand;
    private Card seven, nine, ten, jack, ace;

    @Before
    public void setUp() throws Exception
    {
        seven = EasyMock.createNiceMock(Card.class);
        EasyMock.expect(seven.getValue()).andStubReturn(7);
        EasyMock.expect(seven.toString()).andStubReturn("seven value: 7");

    }

    @Test
    public void testTotalHandValueByAddingNumbers() {
        replayAll();
        hand.addCard(seven);

        assertEquals(27, hand.getTotal());


    }
}

I would be glad, if somebody could help me run this. It's been a while since I have written unit tests.

Edit: I have both of Objenesis and cglib in my classpath.

Thanks

AYa
  • 421
  • 3
  • 9
  • 21

2 Answers2

0

Are you sure you have the other libraries required for class mocking?

cglib (2.2) and Objenesis (1.2) must be in the classpath to perform class mocking

Little Yusuf
  • 76
  • 14
0

In all probability your class com.blackjack.game.cards.Card is final class

and createNiceMock or in that sense EasyMock cannot be used to mock the final classes.

You will need to use Powermock for this case, have a look at this resource for more inputs https://dzone.com/articles/mock-final-class#mock-final-class

Hope this helps!

Good luck!

Vihar
  • 3,626
  • 2
  • 24
  • 47