Junit测试不会使用Google App Engine保留实体

时间:2013-10-15 14:33:49

标签: java google-app-engine jpa junit

我正在使用GAE开发项目。我正在和Junit一起编写一个集成测试,它不会保存实体。我在类路径中包含了JAR,我在这里复制了实体类,测试类和persistence.xml文件。

的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", "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 ad testCreaUtente方法不会持久保存实体。 测试testListaUtenti失败,因为Utenti数字预期为4但是为0。

1 个答案:

答案 0 :(得分:1)

在新创建的类实例上调用每个@Test带注释的方法。所以基本上你不能在方法之间坚持。您应该将此代码放入一个方法中。