Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm writing an authentication system which uses XML formatted messages between client/server. A successful username/password authentication needs to return a randomly generated session ID to the client:

<?xml version="1.0"?>
<amhAPI>
    <auth>
        <sessionId>728c91ea-2ead-4fa9-93c5-3a8453174bea</sessionId>
    </auth>
</amhAPI>

How do I write a unit test which can confirm the format of the success message, since I can't simply do a straight string match due to the random component?

share|improve this question
add comment

2 Answers 2

You really can't, at least if you don't have any direct control over the component's source.

You can generate one value, and then use a regex or similar method to test the format while being agnostic of the exact value, but since this is ostensibly random, you would need to run it a near-infinite number of times in order to guarantee that every possible output is valid.

If you do have control over the component, then you should separate out the part that does the authentication with the part that generates a session ID, using composition or inheritance. In the former case, pass in your own generator, and verify that it returns the same (constant) value that your generator does; in the latter case, you write a fake derived class that overrides the generation method and returns a constant value, which you then compare the output to.

This is actually just good design in general, not specifically designing for testability. If this class does the authentication and the session creation and the XML serialization then that's too many responsibilities for one class; instead, separate out each feature into its own class, and if you then need to compose them into a single "Session Authenticator" aggregate, then you can test the aggregate with mocks, since you've already tested the individual components (authentication + session ID + XML serialization + etc.).

share|improve this answer
    
Thanks, that does clear things up. –  HorusKol May 31 '11 at 2:15
add comment

in the test, return a constant

or just check the string format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

within the message struture

If you want to test the randomness of the IDs returned, that is a separate test independent of the XML message wrapper

share|improve this answer
    
But what about the rest of the message container? And surely returning a constant voids the test, since that is not what the function is supposed to return... –  HorusKol May 31 '11 at 1:51
    
@HorusKol: sorry of course check the xml wrapper also; see edits –  Steven A. Lowe May 31 '11 at 2:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.