Play Framework implements a complete APP (2), frameworkapp
1. Develop DataModel
Create User. java under app \ moders
package models;import java.util.*;import javax.persistence.*;import play.db.jpa.*;@Entitypublic class User extends Model {public String email;public String password;public String fullname;public String isAdmin;public User(String email, String password, String fullname) {this.email = email;this.password = password;this.fullname = fullname;}}
@ Entity the ID is a JPA entity inherited fromPlay. db. jpa. Model, Provides JPA implementation
Class fields are automatically mapped to the DB table. The default value is "User". If you want to modify the value, add a label to the class"@ Table (name = "blog_user ")"
2. Test
Run
>play test yape
Or run Test Yet Another Blog Engine in Eclipse.
Access http: // localhost: 9000/@ tests to enter the test mode.
Select Test and Start for execution. If the execution succeeds, it is marked as green. If the execution fails, a prompt is displayed.
3. Write Test Cases
Modify/Test/BasicTest. java
@Testpublic void createAndRetrieveUser() { //Create a new user and save it new User("alex@gmail.com", "####", "Alex").save(); //Retrieve the user with email address User user = User.find("byEmail", "alex@gmail.com").first(); //Test assertNotNull(user); assertEquals("Alex", user.fullname); }
Create a User, search for the User, and assert
The User inherits from the Model and provides methods such as save \ find.
User. java add connect Method
public static User connect(String email, String passowrd) {return find("byEmailAndPassword", email, passowrd).first();}
Add Test Cases
@Testpublic void tryConnectAsUser() { // Create a new user and save it new User("bob@gmail.com", "####", "Bob").save(); // Test assertNotNull(User.connect("bob@gmail.com", "####")); assertNull(User.connect("bob@gmail.com", "$$$$")); assertNull(User.connect("tom@gmail.com", "####"));}
..