How to create and run a Unit Test

1) Create a new test class
class MyClassTest extends AxUnit_TestCaseBase
2) Create a test method
public void testMyMethod()
{
}
3) Run the test by right clicking the MyClassTest AOT node and choose Add-Ins->Run Test


Using the AxUnit assertion syntax

public void testMyMethod()
{

    assert.that(actual, is.equalTo(expected));
    
    assert.that(2, is.not().equalTo(3));

    assert.that(2 == 2);

    assert.that(0.3333, is.equalTo(0.33).within(2).decimals());

    assert.that(actual, is.true());

    assert.that(actual, is.false());

    assert.that(3, is.greaterThan(2));
}

Use Matchers for advanced assertions

public void testMyMethod()
{

    assert.that(myArray, _.hasItem(_.either(_.equalTo('foo')).or('bar')));    

    assert.that(myArray, _.hasItem(_.anyOf('a', 'b', 'c')));

    assert.that(mystring, _.contains('foo'));

    assert.that(myObject, _.is(_.instanceOf(classnum(SomeClass))));
}

You can easily add your own custom matchers.
Just extend the AxUnit_MatcherBase class and implement the match(..) and describeTo(..) methods

class AxUnit_MyMatcher extends AxUnit_MatcherBase
{
    public boolean match(anytype _valueToMatch)
    {
        return true; //implement your matching logic here
    }

    public void describeTo(AxUnit_DescriptionListener _listener)
    {
        _listener.appendText('a value matching my logic '); //Describe the match logic, which the framework will display on mismatch 
        _listener.appendValue(someValue);
    }
}

How to use the mock framework

Suppose you want to unit test a MailDispatcher class that sends mails using a MailServer and you don't want the test to send any real mails.
This can be done by creating a "fake" mail server that your test can control.

public void testMailDispatch()
{
    MailServer     fakeMailServer;
    MailDispatcher mailDispatcher;
    ;

    fakeMailServer = expect.newFakeFor(classnum(MailServer));   //The framework will automatically create and maintain a fake class that implement / extend the real interface / class.
    expect.on(fakeMailServer)
          .oneCallTo(fakeMailServer.sendMail(with.aStringThat(_.contains('@')),  // Set an expectation, that the method 'sendMail' should be called exactly one time with a first parameter contaning '@'
                                             with.aString()))                    // and a second parameter with any string
          .thenIt(will.return(true));                                            // If the 'sendMail' method is called with the expected parameters, it will return the boolean value 'true'

    mailDispatcher = new MailDispatcher(fakeMailServer);
    mailDispatcher.dispatchMails();

    assert.thatExpectationsAreSatisfied();   //Assert that all the expectations set above have been met
}

As the 'sendMail' method is not called in this case, the test will fail with this explanation:

[XUT_Testing.testMailDispatch] Failure: Expected exactly 1 call to FakeMailServer.sendMail(..
a string containing '@',
any value)
but no calls where made

Expect methods to be called in a specific order

public void testCloseServerAfterSendingMail()
{
    AxUnit_ExpectationSequence sendBeforeClosing;
    MailServer                 fakeMailServer;
    ;

    sendBeforeClosing = expect.newSequenceNamed('Send mail before closing server');
    
    fakeMailServer = expect.newFakeFor(classnum(MailServer));
    expect.on(fakeMailServer)
          .oneCallTo(fakeMailServer.sendMail('foo@bar.dk', 'mail body'))
          .inSequence(sendBeforeClosing);
          
    expect.on(fakeMailServer)
          .oneCallTo(fakeMailServer.close())
          .inSequence(sendBeforeClosing);

    //execute the failing business logic
    fakeMailServer.close();
    fakeMailServer.sendMail('foo@bar.dk', 'mail body');

    assert.thatExpectationsAreSatisfied();
}

Creating a stub

This example will configure the 'fakeMailServer' to do nothing when called and return default values.
public void testUsingStubbedOutMailServer()
{
    MailServer     fakeMailServer;
    MailDispatcher mailDispatcher;
    ;

    fakeMailServer = expect.newFakeFor(classnum(MailServer));
    expect.on(fakeMailServer).any().calls();

    mailDispatcher = new Maildispatcher(fakeMailServer);
    mailDispatcher.dispatchMails();

    assert.that(....);
}

Last edited Apr 29, 2011 at 5:12 PM by JesperJensen, version 8

Comments

No comments yet.