gae上jdo单元测试代码示例

1 创建实体类Member
package com.asflex.domain;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.asflex.base.BaseBean;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Member extends BaseBean {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	
	@Persistent
	private String name;
	
	@Persistent
	private String password;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("Member--");
		sb.append("id=").append(id);
		sb.append(",name=").append(name);
		sb.append(",password=").append(password);
		
		return sb.toString();
	}
}



2 创建单元测试基类
public class BaseTestCase extends TestCase { 
	 private final LocalServiceTestHelper help = new LocalServiceTestHelper(new LocalServiceTestConfig[]{
				new LocalDatastoreServiceTestConfig()
	 });
	
	@Override 
    public void setUp() throws Exception { 
       help.setUp(); 
    } 
 
    @Override 
    public void tearDown() throws Exception { 
       help.tearDown();
    } 
}

3 创建TestMemberDao类
public class TestMemberDao extends BaseTestCase {
	
	private MemberDao memberDao = new MemberDao();
	
	private Member createMember(String name, String password) {
		Member m = new Member();
		m.setName(name == null ? "asflex" : name);
		m.setPassword(password == null ? "11111" : password);
		return m;
	}
	
	public void testMemberCRUD() {
		
		//create Member
		Member m = createMember(null, null);
		Member m99 = memberDao.create(m);				
		Long memberId = m.getId();
		assertNotNull(m.getId());
		
		// retreive Member
		Member m0 = memberDao.load(memberId);
		assertEquals("asflex", m0.getName());
		assertEquals("11111", m0.getPassword());
		
		// update Member
		m0.setName("asfel");
		memberDao.update(m0);		
		Member m1 = memberDao.findById(memberId);
		assertEquals("asfel", m1.getName());
		
	}
	
}

你可能感兴趣的:(单元测试,GAE)