Difference between revisions of "Classes"
imported>Aeric |
imported>Aeric |
||
Line 39: | Line 39: | ||
# Click the name of the class to delete | # Click the name of the class to delete | ||
# Click '''[Delete]''' | # Click '''[Delete]''' | ||
=== Creating Unit Tests === | |||
{{:Creating Unit Tests}} | |||
''Learn more:'' [[Unit Test Framework]] | |||
== Editing Classes in Eclipse == | == Editing Classes in Eclipse == |
Revision as of 21:20, 9 August 2011
Designer > Logic > Classes
A class is a standard Java class. Consider writing classes to create business logic that is not provided by the basic building blocks of the platform.
Learn more: Classes in the Developer Suite
About Classes
You can make Java API calls in the Java code in a class, and you can create instances of classes in Java code.
There are some restrictions on the things you can do in a Java class, as described in Governors on Java Code .
In most cases, a page communicates with a class through a controller class. (You can instantiate a class directly in a page, but that is considered a bad practice.)
- Learn more: Working with Pages and Classes
Working with Classes
From the Classes page, a number of features are provided to view, create and manage classes.
View Classes
- Click Designer > Logic > Classes
- Classes are displayed in a View.
- Use standard Searching and Filtering operations to determine which records are displayed.
- Use the action buttons:
- [New Class] - Add a Class
- [Run All Tests] - Run all Unit Tests defined in the classes
Add a Class
Prerequisite Before you create a class, you need to decide what package to put it in. Here are a few notes to help you make that decision:
- Packages let you organize classes into different directories according to their functionality, their usability, or any other category that makes sense. (The only rule is that classes in one package have a qualitatively different kind of functionality compared with those in another package.)
- Packages help to avoid class name collision. (The same class name can be used in different packages.)
- Classes in the same package can access each others package-protected fields and methods, as well as their public members, without doing an import. So classes that cooperate with each other extensively generally belong in the same package.
- Classes that are part of a different package can be accessed with an import declaration.
To add a class:
- Click > Customization > Developer Resources > Classes
- Click [New Class]
- Fill in the class properties.
- Click [Next]
A class template appears. - Provide the code for the class.
- Click [Save]
Class properties
- Package
- The package name
- The platform supplies this part: com.platform.{namespace},
where {namespace} is defined in the Developer Configuration settings of the current tenancy. - You supply this part: {packagename}
- The result is the fully-qualified package path for the class: com.platform.{namespace}.{packagename}
- The platform supplies this part: com.platform.{namespace},
- Class Name
- The name of the class. Must consist of alphanumeric characters (a-z,A-Z,0-9) or underscores (_). Must start with an alphabetic character (a-z,A-Z) or underscore (_).
- Java code
- Enter Java code in the text area.
Edit a Class
To edit a class:
- Click Designer > Logic > Classes
- Click the name of the class to edit
- Click [Edit]
- Edit the Java code in the text area
- When done, click [Save]
Delete a Class
To delete a class:
- Click Designer > Logic > Classes
- Click the name of the class to delete
- Click [Delete]
Creating Unit Tests
Any method in a Java class can be a test method, as long as it is tagged with the @TestMethod annotation. Within the test method, use assert statements like this one to compare expected results to actual results: RunTest.assertEquals(expected, actual).
Here's a template for a test method:
- <syntaxhighlight lang="java" enclose="div">
/**
* javadoc comment */
@TestMethod public void testSomeBehavior() throws Exception {
String expect = "It's working!"; String actual = someBehavior(); // Invoke the method you're testing
RunTest.assertEquals(expect, actual);
} </syntaxhighlight>
See also: Code Sample:Test of Search using Java API
- Considerations
- A single test method can contain multiple assertions.
- Each successful assertion adds to the success count and the count of total tests.
- A test method may contain no assertions at all. In that case, it runs to completion, but the test is not counted as a success.
- A test may fail either because an exception occurs, or because an assertion fails.
- In either case, the message is recorded. (For an exception, a stack trace is also recorded.)
- Whether an assertion succeeds or fails, the method continues running. It is only interrupted by an exception.
- If multiple assertions fail, all of the failure messages are reported.
- If one or more assertions fail, and then an exception occurs, all of the messages are reported, along with the exception.
- The test method (testSomeBehavior, above) must be public. If it isn't, an IllegalAccessException occurs when the @TestMethod annotation causes the Unit Test Framework to attempt execution.
- The RunTest.assertEquals() method takes Strings as arguments (and only Strings).
Important:
When running tests, the UI is never affected. So your tests always run to completion without pausing for user interactions, regardless of the code contained in the executed methods. These cases in particular are executed without having any visible effect in the UI:- Functions.showMessage - Ordinarily brings up a dialog.
- setTargetPage in a controller - Ordinarily specifies the next page the user will see.
- Any request sent from a controller - Ordinarily specifies the controller code or JSP page that will be visited next.
Learn more: Unit Test Framework
Editing Classes in Eclipse
Use the Eclipse Plug-In to add, edit or delete classes.