1.开发DataModel
在app\moders 下新建User.java
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标识是一个JPA entity,继承自play.db.jpa.Model,提供了JPA实现
类的字段,会自动映射到DB表中,默认表明是"User",如果要修改表明,在类上添加标签"@Table(name="blog_user")"
2.测试
运行
>play test yape
或在Eclipse中运行,Test Yet Another Blog Engine
访问 http://localhost:[email protected], 进入测试模式
选择Test, Start执行,成功会标记为绿色,失败会有提示
3.写测试用例
修改 /test/BasicTest.java
@Testpublic void createAndRetrieveUser() { //Create a new user and save it new User("[email protected]", "####", "Alex").save(); //Retrieve the user with email address User user = User.find("byEmail", "[email protected]").first(); //Test assertNotNull(user); assertEquals("Alex", user.fullname); }
创建User,查找User,进行断言
User继承自Model,提供了save\find等方法
User.java添加connect方法
public static User connect(String email, String passowrd) { return find("byEmailAndPassword", email, passowrd).first();}
添加测试用例
@Testpublic void tryConnectAsUser() { // Create a new user and save it new User("[email protected]", "####", "Bob").save(); // Test assertNotNull(User.connect("[email protected]", "####")); assertNull(User.connect("[email protected]", "$$$$")); assertNull(User.connect("[email protected]", "####"));}
..