Wicket单元测试Translations.utils未找到

时间:2017-02-02 13:18:27

标签: java unit-testing junit wicket wicket-6

我最近开始在wicket开发,并想知道如何在单元测试中设置wicket应用程序。我目前正在使用wicket 6.8 Core。 4.1.2单元测试。

我想知道如何在应用程序上设置Locale。 实际上也是以一种好的方式创建测试应用程序。 我试图实现的是测试是否可以返回从转换文件加载的一组字符串。

 //from the Class.
 public Class Information(){

     public Information(){}

     public String getInfo(){
       return TranslationUtil.getTranslation("RandomFieldNameTobeTranslated");
     }
 }

单元测试:

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

public class InformationTest(){
    @Before
    public void setup() throws Exception(){
      //needs to be setup in someway it can be used by the test.
        tester = new WicketTester();
        WebApplication web = tester.getApplication();
        web.usesDeploymentConfig();
        web.configure();
        web.initApplication();
        Session.get().setLocale(new Locale("en_us"));
    }

    @Test
    public test(){
       Information information = new Information();
       assertEquals("Foo is Foo", "Foo", information.getInfo() );
    }
}

单元测试将运行代码并无法找到属性。 这只是一个基本测试,描述了这个问题。

java.util.MissingResourceException: 
Unable to find property: 'RandomFieldNameTobeTranslated'. Locale: null, style: null

尝试了初始化和配置的一些变化。但是,我没有经验知道如何以正确的方式初始化wicket进行开发单元测试。

问题:

  1. 如何初始化wicket以便它可以在会话中找到语言环境?

  2. 如何将wicket重定向到正确的翻译文件?

  3. 生产版本有效,但我想构建一个单元测试,它似乎需要“应用程序”。它可以被嘲笑,但作为最后的手段,因为它用于分配地点,我宁愿测试价值等于价值'。

1 个答案:

答案 0 :(得分:1)

来自#wicket的@selckin: 只需通过以下方式启动它:

new WicketTester(new YourApplicationClas());

实际单位测试代码:

import org.junit.Before;
import org.junit.Test;
import java.util.Locale;

public class SchedulerModelTest {
@Before
public void setup() throws Exception {
    new WicketTester(new ReconAdminApplication());
    Session.get().setLocale(new Locale("en_us")); //always set the locale.
}

@Test
public test(){
   Information information = new Information();
   assertEquals("Foo is Foo", "Foo", information.getInfo() );
}

}