27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Advanced Topics 249<br />

}<br />

}<br />

public override void Initialize() { this.salary = 0; }<br />

// ...<br />

<strong>Model</strong> program Payroll1 and Payroll2 behave similarly. In the case of objectoriented<br />

Payroll2, the N<strong>Model</strong> framework will create the field map for salary<br />

automatically. The field map will record the associated value of each reachable<br />

instance of class Employee. An object is reachable if it occurs within a global state<br />

variable, or if it occurs within an instance field of a reachable object. Reachability<br />

will be discussed in more detail in Section 15.2.<br />

When inherited by type T the LabeledInstance class provides the following<br />

features:<br />

• An implicit field map for instance fields of T, as explained above.<br />

• A serialized term label for each instance of T. For example, the first instance of<br />

class Employee may appear as the term Employee(1) inside of an action argument.<br />

The role of the term label in composition is discussed in Section 15.3. The role<br />

of the term label in test harnessing is discussed in Section 15.4.<br />

• A parameter generator for type T named new. You can use the attribute [Domain<br />

("new")] on an action parameter whenever a new object should appear. This is<br />

covered in Section 15.2.<br />

• A static factory method called Create. This factory method is used instead of<br />

C# operator new in contexts where a instance of T needs to be created within a<br />

method. This is an advanced feature; most model programs will use the parameter<br />

generator [Domain("new")] as a way to introduce new objects instead of the<br />

Create factory method.<br />

• A virtual method Initialize. The model program should override this method<br />

for type T and reset all instance fields to their initial state.<br />

In the following sections we present some topics that relate to the use of LabeledInstance<br />

for object-oriented modeling.<br />

15.2 Creating instances<br />

Whenever you model objects, you have to deal with the issue of instantiation. How<br />

do instances come into existence? How are they deleted?<br />

For example, we might extend our employee example as follows with actions<br />

that add and remove employees from the system. Here we program in an objectoriented<br />

style, but without using LabeledInstance, managing the IDs and the field<br />

map explicitly in our code.<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!