【问题标题】:Unit Testing of a model in Play Framework 2.3Play Framework 2.3 中模型的单元测试
【发布时间】:2015-09-17 13:42:32
【问题描述】:

我们最近从 Play Framework 2.1 迁移到 2.3,一些单元测试停止工作。

在这个特定的单元测试中,我使用了一个从 ebean 扩展模型的对象。我确保不使用 ebean 中的任何函数(例如 find()save()update())。

不幸的是,仅仅通过创建我的对象,我得到了一个异常,因为它试图启动 Model.Finder 成员,我很确定它在迁移之前没有这样做。我该如何克服呢?

我的 setUp 函数在 new 调用时抛出异常。

@Before
public void setUp() throws Exception {  
    SignageScheduleEntry allTheTimeSchedule = new SignageScheduleEntry();
}

我的对象本身,在调试单元测试时在新的 Model.Finder 上失败:

public static Model.Finder<Long,SignageScheduleEntry> find = new Model.Finder<>(Long.class, SignageScheduleEntry.class);

    public SignageScheduleEntry() throws InvalidPeriodException {   
        ....
    }

简而言之,我想在单元测试中使用没有 ebean 废话的对象,就像任何单元测试中的任何对象一样。我怎样才能做到这一点?

谢谢!

【问题讨论】:

    标签: unit-testing playframework playframework-2.0 junit4 ebean


    【解决方案1】:

    如图:https://github.com/jamesward/play2torial/blob/master/JAVA.md#create-a-model

    您需要像这样创建一个“fakeApplication”:

    import org.junit.Test;
    
    import static play.test.Helpers.fakeApplication;
    import static play.test.Helpers.running;
    
    import static org.fest.assertions.Assertions.assertThat;
    
    import models.Task;
    
    public class TaskTest {
    
        @Test
        public void create() {
            running(fakeApplication(), new Runnable() {
                public void run() {
                    Task task = new Task();
                    task.contents = "Write a test";
                    task.save();
                    assertThat(task.id).isNotNull();
                }
            });
        }
    
    }
    

    如果这不起作用,或者这不是您想要的,另一种方法更复杂,并且来自 Play Java 文档: https://www.playframework.com/documentation/2.3.x/JavaTest#Unit-testing-models

    您基本上必须为模型创建一个包装器,并在单元测试中模拟包装器。

    【讨论】:

    • 我想创建一个假的应用程序一定很慢...... Play框架肯定落后于其他与单元测试和TDD相关的框架......无论如何,我没有选择现在!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多