我正在使用 GAE 开发一个项目。我正在用 Junit 编写一个不保存实体的集成测试。我已将 JAR 包含在类路径中,并在此处复制实体类、测试类和 persistence.xml 文件。
持久性.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="transactions-optional">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
</properties>
</persistence-unit>
</persistence>
Utente.java
package it.bfm.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.google.appengine.api.datastore.Key;
@Entity
public class Utente {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Key key;
private String nome;
private String cognome;
private String username;
private String password;
private String email;
public Utente(String nome, String cognome, String user, String password,
String email){
this.nome = nome;
this.cognome = cognome;
this.username = user;
this.password = password;
this.email = email;
}
public Key getKey(){
return this.key;
}
public void setNome(String nome){
this.nome = nome;
}
public String getNome(){
return this.nome;
}
public void setCognome(String cognome){
this.cognome = cognome;
}
public String getCognome(){
return this.cognome;
}
public void setUser(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
public void setPassword(String password){
this.password = password;
}
public String getPasswrd(){
return this.password;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return this.email;
}
}
UtenteTest.java
package it.bfm.test;
import it.bfm.business.UtenteImpl;
import it.bfm.business.UtenteInterface;
import it.bfm.entity.Utente;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import junit.framework.TestCase;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
public class UtenteTest extends TestCase{
private final static LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
private static UtenteInterface utImpl = new UtenteImpl();
@BeforeClass
public static void setUpUtenti() {
utImpl.creaUtente("Utente1", "Test1", "test1", "test1", "test1@test.it");
utImpl.creaUtente("Utente2", "Test2", "test2", "test2", "test2@test.it");
utImpl.creaUtente("Utente3", "Test3", "test3", "test3", "test3@test.it");
}
@Before
public void setUp(){
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
@Test
public void testCreaUtente() {
utImpl.creaUtente("Utente4", "Test4", "test4", "test4", "test4@test.it");
}
@Test
public void testListaUtenti() {
List<Utente> utenti = null;
utenti = utImpl.listaUtenti();
Assert.assertEquals(4, utenti.size());
}
@Test
public void testCercaUtenteByEmail() {
Utente utente;
String emailTest = "test1@test.it";
String nomeTest = "Utente1";
String cognomeTest = "Test1";
utente = utImpl.cercaUtenteByEmail(emailTest);
Assert.assertEquals(utente.getNome(), nomeTest);
Assert.assertEquals(utente.getCognome(), cognomeTest);
}
@Test
public void testLogin() {
Utente utente;
String usernameTest = "test1";
String passTest = "test1";
String nomeTest = "Utente1";
String cognomeTest = "Test1";
utente = utImpl.login(usernameTest, passTest);
Assert.assertEquals(utente.getNome(), nomeTest);
Assert.assertEquals(utente.getCognome(), cognomeTest);
}
}
问题是 setUpUtenti 和 testCreaUtente 方法不会持久化实体。测试 testListaUtenti 失败,因为 Utenti 编号应为 4 但为 0。