Search code examples
javaunit-testingjunitwicketwicket-6

Wicket unit testing Translations.utils not found


I started recently developing in wicket and wondered how to set the wicket application in the unit test itself. I am currently using the wicket 6.8 Core. 4.1.2 for the unit test.

I am wondering how to set the Locale on the application. Also actually creating the test application in a good way. What i'm trying to achieve is a test to see if a set of strings can be returned which are loaded from a translation file.

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

     public Information(){}

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

Unit Test:

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() );
    }
}

The unit test will run the code and get a unable to find property. This is just a basic test, describing the issue.

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

Tried some variations with the initialization and config. But i'm too inexperienced to know how to initialize wicket on the right way for the development unit testing.

Questions:

  1. how can I initialize wicket so it can find the locale in the session?

  2. how to redirect wicket to the correct translation file?

The production version works, but I want to build a unit test and it seemed to require the 'application'. It can be mocked but as a last resort as this is used in allot of locations , I rather want to test 'value equals value'.


Solution

  • @selckin from #wicket : Just initiate it by:

    new WicketTester(new YourApplicationClas());
    

    Actual unit test code after:

    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() );
    }
    
    }