Domanda

i'm pretty new to play, but an more or less experienced Java developer.(This is why i searched for some MVC Framework for Java, instead of rails/cake/django) Anyway...

My question is: Is it possible to test the Model validation(The annotated validation criteria) from within a unitTest? It is not about testing whether the Validation in CORE works correctly, it is about testig if i messed up the Annotations :-p After a some time spend googling and reading about this, i know that the validation is part of the Form api. So i tried to set up a form in my Unit test. But this did not work,

This is the code

package models;


import static org.junit.Assert.*;
import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.inMemoryDatabase;
import static play.test.Helpers.start;
import static play.test.Helpers.stop;

import java.util.HashMap;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import play.data.Form;
import play.test.FakeApplication;

public class UserTests {
    private FakeApplication fakeApplication;

    @Before
    public void startup() {
        fakeApplication = fakeApplication(inMemoryDatabase());
        start(fakeApplication);
    }

    @Test
    public void testCreateUserWorks(){
        String name = "asf";
        HashMap<String,String> values = new HashMap<String,String>();
        values.put("username", name);
        Form<User> userForm = Form.form(User.class);
        userForm = userForm.bind(values);
        System.out.println(userForm.toString());
        System.out.println(userForm.data());

        User tmp = userForm.get();
        System.out.println("muh4");
        User testUser = User.find.where().eq("username", name).findUnique();
        assertNotNull(testUser);
        System.out.println(testUser.getEmail());
        assertEquals(name, testUser.username);
        assertEquals(name+"@xxx.xxx",testUser.getEmail());
    }

    @After
    public void tearDown(){
        stop(fakeApplication);
    }

}

But when reaching userForm.get() it just fails with Test models.UserTests.testCreateUserWorks failed: java.lang.IllegalStateException: No value

userForm.toString() says this Form(of=class models.User, data={username=asf}, value=None, errors={})

I know i'm new to play, but I'm out of ideas what to try. Maybe it is something about Spring Databinding not initialized when not in the real app?!

Thank you in advance TimmeeY

And here is my Model Code

@Entity
public class User extends Model {   
    @Id
    @Constraints.MinLength(1)
    String username;
    @Transient String email;        


    public List<ValidationError> validate(){
        List<ValidationError> errors = new ArrayList<ValidationError>();

        return errors;
    }

    public static Finder<String, User> find = new Finder<String, User>(String.class, User.class);

    public String getEmail(){
        return this.username + "@xxx.xxx";
    }
}
È stato utile?

Soluzione 2

I found my mistake... I implemented a somewhat "stub" validate() method, which returned an empty array, instead of null. So the problem was not with my tests oder so. It was about me to dumb to read the manual.

Anyway. Thank you

Altri suggerimenti

That works, you just need to start your unit test with a fake application.

import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.inMemoryDatabase;
import static play.test.Helpers.start;
import static play.test.Helpers.stop;

import org.junit.After;

import play.test.FakeApplication;

public abstract class AbstractFakePlayApplication {

    private FakeApplication fakeApplication;

        @Before
    public void startup() {
        fakeApplication = fakeApplication(inMemoryDatabase());
        start(fakeApplication);
    }

    @After
    public void tearDown() {
        stop(fakeApplication);
    }

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top