Executable models, as the name implies, are models that are complete and precise enough to be executed. One of the key benefits is that you can evaluate your model very early in the development life cycle. That allows you to ensure the model is generally correct and satisfies the requirements even before you have committed to a particular implementation platform.
One way to perform early validation is to automatically generate a prototype that non-technical stakeholders can play with and (manually) confirm the proposed model does indeed satisfy their needs (like this).
Another less obvious way to benefit from executable models since day one is automated testing.
The requirements
For instance, let’s consider an application that needs to deal with money sums:
- REQ1: a money sum is associated with a currency
- REQ2: you can add or subtract two money sums
- REQ3: you can convert a money sum to another currency given an exchange rate
- REQ4: you cannot combine money sums with different currencies
The solution
A possible solution for the requirements above could look like this (in TextUML):
package money; class MixedCurrency end; class Money attribute amount : Double; attribute currency : String; static operation make(amount : Double, currency : String) : Money; begin var m : Money; m := new Money; m.amount := amount; m.currency := currency; return m; end; operation add(another : Money) : Money; precondition (another) raises MixedCurrency { return self.currency = another.currency } begin return Money#make(self.amount + another.amount, self.currency); end; operation subtract(another : Money) : Money; precondition (another) raises MixedCurrency { return self.currency = another.currency } begin return Money#make(self.amount - another.amount, self.currency); end; operation convert(anotherCurrency : String, exchangeRate : Double) : Money; begin return Money#make(self.amount * exchangeRate, anotherCurrency); end; end; end.
Now, did we get it right? I think so, but don’t take my word for it.
The proof
Let’s start from the beginning, and ensure we satisfy REQ1 (a money sum is a pair <amount, currency>:
[Test] operation testBasic(); begin var m1 : Money; m1 := Money#make(12, "CHF"); Assert#assertEquals(12, m1.amount); Assert#assertEquals("CHF", m1.currency); end;
It can’t get any simpler. This test shows that you create a money object providing an amount and a currency.
Now let’s get to REQ2, which is more elaborate – you can add and subtract two money sums:
[Test] operation testSimpleAddAndSubtract(); begin var m1 : Money, m2 : Money, m3 : Money, m4 : Money; m1 := Money#make(12, "CHF"); m2 := Money#make(14, "CHF"); m3 := m1.add(m2); Assert#assertEquals(26, m3.amount); Assert#assertEquals("CHF", m3.currency); /* if m1 + m2 = m3, then m3 - m2 = m1 */ m4 := m3.subtract(m2); Assert#assertEquals(m1.amount, m4.amount); Assert#assertEquals(m1.currency, m4.currency); end;
We add two values, check the result, them subtract one of them from the result and expect the get the other.
REQ3 is simple as well, and specifies how amounts can be converted across currencies:
[Test] operation testConversion(); begin var m1 : Money, result : Money; m1 := Money#make(3, "CHF"); result := m1.convert("USD", 2.5); Assert#assertEquals(7.5, result.amount); Assert#assertEquals("USD", result.currency); end;
We ensure conversion generates a Money object with the right amount and the expected currency.
Finally, REQ4 is not a feature, but a constraint (currencies cannot be mixed), so we need to test for rule violations:
[Test] operation testMixedCurrency(); begin try Money#make(12, "CHF").add(Money#make(14, "USD")); /* fail, should never get here */ Assert#fail("should have failed"); catch (expected : MixedCurrency) /* success */ end; end;
We expect the operation to fail due to a violation of a business rule. The business rule is identified by an object of a proper exception type.
There you go. Because we are using executable models, even before we decided what implementation platform we want to target, we already have a solution in which we have a high level of confidence that it addresses the domain-centric functional requirements for the application to be developed.
Can you say “Test-driven modeling”?
Imagine you could encode all non-technical functional requirements for the system in the form of acceptance tests. The tests will run against your models whenever a change (to model or test) occurs. Following the Test-Driven Development approach, you alternate between encoding the next requirement as a test case and enhancing the model to address the latest test added.
Whenever requirements change, you change the corresponding test and you can easily tell how the model must be modified to satisfy the new requirements. If you want to know why some aspect of the solution is the way it is, you change the model and see the affected tests fail. There is your requirement traceability right there.
See it by yourself
Would you like to give the mix of executable modeling and test-driven development a try? Sign up to AlphaSimple now, then open the public project repository and clone the “Test Infected” project (or just view it here).
P.S.: does this example model look familiar? It should – it was borrowed from “Test Infected: Programmers Love Writing Tests“, the classical introduction to unit testing, courtesy of Beck, Gamma et al.
Testing Requirements « Dark Views
August 30, 2011 at 12:53am[...] rafael.chaves’s blog post “Modeling requirements the pragmatic way (or When xUML meets xUnit)” for a more detailed introduction to the idea. Rate this: Share this:Like this:LikeBe the [...]
Damien Cassou
August 30, 2011 at 4:29amI’m sorry my question has nothing to do with the topic of the post:
what in your examples is specific to modeling and would be different when programming?
I still don’t understand the difference between modeling and programming.
rafael.chaves
August 30, 2011 at 8:29amI think it is a relevant question, Damien, and one that I was actually expecting.
For one, the example model is very simple, so it is not like it requires more features that you won’t find in ordinary programming languages. If it dealt with associations, state transitions, events, things that UML supports natively but need to be emulated in programming languages, that would be more evident.
But after all modeling and programming (in Java or assembly) are just different tones of the same gradient, so if you ignore the nuances (like the intended use cases), it is hard to see any differences between them.
rafael.chaves
August 30, 2011 at 8:48amAnother thing is that no matter the technical architecture of the application, both model and tests will continue to be much simpler to understand than the implementation code, because at the model level we ignore the technical architecture (you won’t see implementation concerns such as database technology, transactions etc). You sure could achieve that with an implementation oriented language (see DDD), but it is not as natural.
MDD meets TDD (part II): Code Generation | abstratt's blog
October 31, 2011 at 11:43pm[...] at Abstratt we are big believers of model-driven development and automated testing. I wrote here a couple of months ago about how one could represent requirements as test cases for executable [...]
Andreas Leue
November 11, 2011 at 4:48pmHi Rafael,
I left a longer comment at MDSN
Andreas
http://www.modeldrivensoftware.net/profiles/blogs/mdd-meets-tdd-mapping-requirements-as-model-test-cases