It is currently 18 Jul 2025, 19:34
   
Text Size

Mockito

Moderators: North, BetaSteward, noxx, jeffwadsworth, JayDi, TheElk801, LevelX, CCGHQ Admins

Mockito

Postby glerman » 21 Jun 2015, 19:08

Hello Guys,

I've only recently learned about XMage and greatly enjoyed the product.
I happen to be a Java developer and I want to help improve the game where I can.

I'v Started by implementing a missing card: Chronozoa, but I found that testing my implementation isn't very efficient: the dev guid suggests to start the server in debug mode and connects with a client and start playing a game, using the test.dck and init.txt, thus testing the card in action.

This is called a full end-to-end test and it's very important to do, but it's usually done after some unit tests are in place to test much simpler stuff that don't need the hole system to be up, thus saving time.

The code base doesn't really have unit tests for cards and that's because the objects needed to manipulate the card in a test are complicated and hard to create. For example, the Game class.

Here is where Mockito comes to the rescure, Mockito is an open source mocking framework that makes it very easy to write unit tests.

What's mocking you ask? it allows you to ignore the objects that your testable unit is dependant on and test only your unit. This is done by creating a mock of those objects and defining how they interact with your unit in the test, thus easily creating the scenarios you to cover in the test.

It's much easier to show than to explain, so I'll be happy to provide examples in cards I implement if you guys will add Mockito for a trial run.

Best regards,
Gal Lerman
glerman
 
Posts: 3
Joined: 15 Jun 2015, 07:09
Has thanked: 0 time
Been thanked: 1 time

Re: Mockito

Postby LevelX » 21 Jun 2015, 20:21

Did you check out the Mage Tests project?

It enables you to create repeatable card tests. There are about > 900 existing now.
I'm continuously adding tests. Sometimes while looking for the reason of a bug, sometimes beacause I'm implemented a new keyword/mechanic and like to test it and be sure that future changes do not break the implementation.

So a lot of cards are simple and reassemble already existing abilities and effects. So most of the time it's not neccessary to create new tests.
But for more complex cards or newly implemented effects/abilities it's ideal always to implement related tests.

So if you can show how we can use Mockito to make the tests with the Mage Tests project more easy that would be fore sure a good thing.

Here is a very simple example how such a test wood look like now:

Code: Select all
    /**
     * Test that the tokens are put to battlefield if the last time counter is removed
     */
    @Test
    public void testChronozoa1() {
        addCard(Zone.BATTLEFIELD, playerA, "Island", 4);
        // Flying
        // Vanishing 3
        // When Chronozoa dies, if it had no time counters on it, put two tokens that are copies of it onto the battlefield.
        addCard(Zone.HAND, playerA, "Chronozoa");
       
        castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Chronozoa");
       
        setStopAt(7, PhaseStep.PRECOMBAT_MAIN);
        execute();
       
        // check that 2 Chronozoa permanents are on the battlefield
        assertPermanentCount(playerA, "Chronozoa", 2);
        Permanent token = getPermanent("Chronozoa", playerA);
        // check that they are tokens
        Assert.assertEquals("Chronozoa has to be a token", true, token instanceof PermanentToken);
    }
You can add as many test cases for Chronozoa as you like to test other scenarios.
Like Chronozoa get's destroyed with time counters or if the created tokens behave correctly.
User avatar
LevelX
DEVELOPER
 
Posts: 1677
Joined: 08 Dec 2011, 15:08
Has thanked: 174 times
Been thanked: 374 times


Return to Developers Talk

Who is online

Users browsing this forum: No registered users and 1 guest

Main Menu

User Menu

Our Partners


Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 10 minutes)
Most users ever online was 7303 on 15 Jul 2025, 20:46

Users browsing this forum: No registered users and 1 guest

Login Form