is code using DateTimeFormat not testable?

1,199 views
Skip to first unread message

Shawn Brown

unread,
Sep 23, 2011, 7:25:53 AM9/23/11
to google-web-toolkit
I'm just trying to test my code that has DateTimeFormat in it.
However I see ...

java.lang.ExceptionInInitializerError
at com.google.gwt.i18n.client.DateTimeFormat.getDefaultDateTimeFormatInfo(DateTimeFormat.java:808)
at com.google.gwt.i18n.client.DateTimeFormat.getFormat(DateTimeFormat.java:488)
at com.google.gwt.i18n.client.DateTimeFormat.getLongDateTimeFormat(DateTimeFormat.java:690)
...
Caused by: java.lang.UnsupportedOperationException: ERROR:
GWT.create() is only usable in client code! It cannot be called, for
example, from server code. If you are running a unit test, check that
your test case extends GWTTestCase and that GWT.create() is not called
from within an initializer or constructor.
at com.google.gwt.core.client.GWT.create(GWT.java:91)
at com.google.gwt.i18n.client.LocaleInfo.<clinit>(LocaleInfo.java:36)
... 35 more


Is there a list of what I can not use in Tests since GWT.create
forbids it? This seems to be a reoccurring theme in GWT and I am
tired of writing code that subsequently needs to be yanked as it's not
testable.

Shawn

Jesper Rønn-Jensen

unread,
Oct 19, 2011, 3:22:28 AM10/19/11
to Google Web Toolkit
In my opinion, your question is extremely relevant

> is code using DateTimeFormat not testable?

I found your thread here because I converted some code from
SimpleDateFormat to gwt DateTimeFormat. And this provoked the error
when running the tests

java.lang.NullPointerException
at
com.google.gwt.i18n.client.LocaleInfo.ensureDateTimeFormatInfo(LocaleInfo.java:
201)
at
com.google.gwt.i18n.client.LocaleInfo.getDateTimeFormatInfo(LocaleInfo.java:
159)
at
com.google.gwt.i18n.client.DateTimeFormat.getDefaultDateTimeFormatInfo(DateTimeFormat.java:
808)
at
com.google.gwt.i18n.client.DateTimeFormat.getFormat(DateTimeFormat.java:
625)


What is the best practice for having testable code to convert strings
to Date? I am looking for code that works both in client and in JUnit
tests



On Sep 23, 1:25 pm, Shawn Brown <big.coffee.lo...@gmail.com> wrote:
> I'm just trying to test my code that hasDateTimeFormatin it.
> However I see ...
>
> java.lang.ExceptionInInitializerError
>         at com.google.gwt.i18n.client.DateTimeFormat.getDefaultDateTimeFormatInfo(Date TimeFormat.java:808)
>         at com.google.gwt.i18n.client.DateTimeFormat.getFormat(DateTimeFormat.java:488 )
>         at com.google.gwt.i18n.client.DateTimeFormat.getLongDateTimeFormat(DateTimeFor mat.java:690)

Thomas Broyer

unread,
Oct 19, 2011, 5:43:40 AM10/19/11
to google-we...@googlegroups.com
I believe the right way is to use a mock of DateTimeFormat in your unit test.

You shouldn't be testing how DateTimeFormat behaves: are you writing tests checking java.util.List behavior?

Сергей Целовальников

unread,
Jul 17, 2013, 11:08:09 AM7/17/13
to google-we...@googlegroups.com, google-web-toolkit
This can help test DateTimeFormat in server side

    public static com.google.gwt.i18n.shared.DateTimeFormat createGwtDTFormat(String pattern) throws Exception
    {
        Constructor<com.google.gwt.i18n.shared.DateTimeFormat> c = com.google.gwt.i18n.shared.DateTimeFormat.class
                .getDeclaredConstructor(String.class, DateTimeFormatInfo.class);
        c.setAccessible(true);
        return c.newInstance(pattern, new DateTimeFormatInfoImpl_en());
    }


пятница, 23 сентября 2011 г., 17:25:53 UTC+6 пользователь Shawn написал:

ckuetbach

unread,
Aug 29, 2013, 10:33:04 AM8/29/13
to google-we...@googlegroups.com
Ithink there is a bug in The DateTimeFormat GWT version 2.5.1:

I use the DateTimeFormat from the shared package. As far as I know, classes from the shared package can be used at client and at serverside. But the method

  private static DateTimeFormatInfo getDefaultDateTimeFormatInfo() {
    // MUSTFIX(jat): implement
    return LocaleInfo.getCurrentLocale().getDateTimeFormatInfo();
  }

uses LocaleInfo from the client-package. if I use the GWTMockutilities I run into a NullPointerexception

ckuetbach

unread,
Aug 29, 2013, 10:35:36 AM8/29/13
to google-we...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages