0% found this document useful (0 votes)
9 views

Using and Testing Classes Slides

The document provides an overview of working with classes and objects in programming, emphasizing the distinction between abstract classes and instantiated objects. It discusses the creation of objects, the use of static members, and the importance of unit testing to validate code functionality. Additionally, it highlights the structure of unit tests and the advantages they offer in software development.

Uploaded by

nouhaila.naamane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Using and Testing Classes Slides

The document provides an overview of working with classes and objects in programming, emphasizing the distinction between abstract classes and instantiated objects. It discusses the creation of objects, the use of static members, and the importance of unit testing to validate code functionality. Additionally, it highlights the structure of unit tests and the advantages they offer in software development.

Uploaded by

nouhaila.naamane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Using and

Testing Classes

Gill Cleeren
CTO Xpirit Belgium

@gillcleeren
Overview
Working with objects
Adding static members
Exploring the interface
Writing unit tests
Working with Objects
From Classes to Objects

Classes are abstract description, not real objects

Objects are created on heap and give values

Reference points to object on heap


From Classes to Objects

Object 1
Color: pink

Object 2
Color: blue

Class Object 3
Color is a field Color: green
Classes Are Reference Types

Stack Heap

int a = 1;
a=1

int b = 5;
b=5 o (obj)
object o =
new object(); o (ref)
Creating a New Object

Assignment operator

Create variable Create object

Product product = new Product();

Variable type Variable name


Variable Variable Constructor
type name Class arguments

Product product = new Product(123, "Eggs");

Using a Constructor with Parameters


t Invoking a method
product.UseProduct();

product.Name = "Milk"; t Changing a property

t Returning a value from a method


int number = product.UpdateStock();
Product product = new ();

New shorthand Syntax


Demo

Creating objects
public class Product
{
public int Id { get; set; }
public string Name { get; set; }

public Product()
{
}
}

Using Object Initialization


Product p = new Product() { Name = "Eggs", Id = 123 };

Using Object Initialization


Demo

Using object initializers


Adding Static Members
Working with objects
Objects get their own copy of data
of a class

Data can also be stored at class level

Static members are used for this


public class Product
{
public static int StockTreshold = 5;
}

Adding static Data


public partial class Product
{
public static int StockTreshold = 5;

public static void ChangeStockTreshold(int newStockTreshhold)


{
//Do something with static data here
}
}

Adding a static Method


Product.StockTreshold = 10;
Product.ChangeStockTreshold(5);

Using a static Member


Only accessible through class since it’s created at class-level
Adding static members to the Product class
Exploring the Interface of the Application
Demo

Looking at the console application


Adding Support for Loading Data
“Hey, it’s Bethany! Would it be possible that
we load in the current inventory from a file?”
“Yes, we can develop that!”
A New Requirement

Start from a file with the current inventory


Have a class that reads the file and create
Products à Repository
Repository contains all code to abstract away
interaction with the data
Loading data using a repository
Writing Tests for the Class
Adding Unit Tests

Code to test other code

Test small, isolated portions of the code (typically methods)

Validate result
Advantages of Unit Tests

Find bugs Change without Improve quality Documentation


fear of breaking of the code
something
Structure of a Unit Test

Arrange Act Assert


Writing a Unit Test

public void IncreaseStock_CorrectAmountInStock()


{
//Arrange
Product product = new Product();

//Act
product.IncreaseStock(100);

//Assert
Assert.Equal(100, product.AmountInStock);
}
Demo

Adding tests for the Product class


Summary
Objects are instantiated based on a class
Classes are reference types
Using unit tests, we can validate the
correct working of a class
Up Next:

Working with Class Hierarchies

You might also like