2013-10-15 86 views
0

我正在使用GAE開發項目。我正在寫一個與Junit集成的測試,它不保存實體。我已將JAR包含在classpath中,並在此處複製實體類,測試類和persistence.xml文件。Junit測試不會將實體與Google App Engine持久存在

的persistence.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", "[email protected]"); 
     utImpl.creaUtente("Utente2", "Test2", "test2", "test2", "[email protected]"); 
     utImpl.creaUtente("Utente3", "Test3", "test3", "test3", "[email protected]"); 
    } 

    @Before 
    public void setUp(){ 
     helper.setUp(); 
    } 

    @After 
    public void tearDown() { 
     helper.tearDown(); 
    } 

    @Test 
    public void testCreaUtente() { 
     utImpl.creaUtente("Utente4", "Test4", "test4", "test4", "[email protected]"); 
    } 

    @Test 
    public void testListaUtenti() { 
     List<Utente> utenti = null; 
     utenti = utImpl.listaUtenti(); 
     Assert.assertEquals(4, utenti.size()); 
    } 

    @Test 
    public void testCercaUtenteByEmail() { 
     Utente utente; 
     String emailTest = "[email protected]"; 
     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); 
    } 
} 

在p問題在於setUpUtenti和testCreaUtente方法不會保留實體。 測試testListaUtenti失敗,因爲Utenti數字預期爲4,但爲0.

+0

持久性代碼沒有顯示,所以人們不得不猜測它爲什麼不被持久化。日誌在說明時說什麼? – DataNucleus

回答

1

每個@Test帶註釋的方法在新創建的類實例上調用。所以基本上你不能堅持不同的方法。你應該把這個代碼放到一個方法中。

+0

謝謝彼得。我還有一個問題:例外是「java.lang.AssertionError:expected:<4>但是:<0>」。在每次執行之前,Junit是否執行用@BeforeClass註解的方法? – Stefano

+0

'@ BeforeClass'必須在靜態方法上使用,它只能在類中的所有測試之前運行一次:http://junit.sourceforge.net/javadoc/org/junit/BeforeClass.html –