Java assignment help
Constructing the Domain Layer
© REGIS university
1
CS444 – Software Engineering
1
The Domain Layer
The Domain Layer
Contains the “entities” of the app – software classes corresponding to real-world conceptual classes from the domain model
Also contains software classes representing entities that are not conceptual classes, but needed to make the program work, e.g., a Login class
Technique: in NetBeans:
Create a “domain” package to host the domain classes
Create the domain classes in the domain package needed for each use case in the Project Charter
Login, Create Account, CRUD operations, your additional use case
Create unit tests for each class
Create the Domain Package
Right click on your application package (e.g., cs444)
Choose New -> Java Package …
Name the new package: <project name>.domain
Example: CS444Hemmes.domain
3
Create the “domain” package
Results
4
new nested domain pkg
Creating Domain Layer Classes
We need to create domain classes for the use cases of our application
This generally means creating classes that will hold data obtained from the UI windows
For example, we need domain classes for:
Login, Account, CRUD operations, your additional use case
To create a domain layer class:
Right click on the domain package
Choose New -> Java Class
Give it a name (e.g., Login), then click Finish
Example (next slide)
5
Creating the Login Class
6
1. right click the domain package
2. New
3. Java Class
4. name the class Login
5. Finish
New Java Class popup
The new, empty Login class appears in the editor pane
Now add properties
The Login Class
7
package statement
new empty Login class
The Login Class
We add the username/password fields to the class
Now add the behavior
8
The Login Class
Add get and set methods:
Add a validate method
9
The Login Class
Add a validate() method:
Check the username and password for null or empty string “” values
Note: this example is insufficient for your application. You need to check for other password attributes, which might include length, strength, repetition, etc., for it to be considered valid. What you choose is up to you, but it needs to be more than simply a null value.
Next create a unit test
10
Unit Testing
Create a unit test for the Login class
In the navigation pane, right click on the Login class
Choose: Tools -> Create / Update Tests
11
3. OK
2. uncheck
1. uncheck
LoginTest
The LoginTest is created with a stubbed out method: testSomeMethod()
Now update the test method
12
LoginTest
Rename the test method to be: testValidate() and enter the code for the test. Try a number of cases for which validate() returns false, followed by one for which it returns true:
13
Run the Test
Right click on the test, and choose either …
Run File
Test File
The test output window displays the results
14
One Final Note about Testing
In general, we test “interesting” behavior and not “trivial” behavior
For domain classes, we don’t have a lot of interesting behavior, so we focus our test on the validate() method
In the preceding example we don’t create tests for setters and getters
15
The Account Class
Repeat the steps to create an Account class
Right click on the domain package
Choose New -> Java Class …
In the popup, give the class a name, Account, the click Finish
16
The Account Class
The new, empty Account class appears in the editor pane
17
The Account Class
Add the properties to the class
18
The Account Class
Add the get and set methods
19
The Account Class
Add a validate() method
As with the Login class, consider if this is sufficient to validate the fields, or if you should have additional checks. Our goal is to produce software that is robust, and validation of input values is a significant part of robustness
20
AccountTest
Create an Account unit test
Right click on the Account class
Choose Tools -> Create / Update Tests
In the popup deselect:
Method Access Levels
Generated Code
21
1. uncheck
2. uncheck
3. OK
AccountTest
The AccountTest is created with a stubbed out method: testSomeMethod()
Update the test method
22
AccountTest
Rename the test method to be: testValidate() and enter the code for the test
23
Run the Test
Right click on the test, and choose either …
Run File
Test File
The test output window displays the results
24