My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.junit;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWTBridge;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* Defangs {@link GWT#create(Class)} to allow unit tests to mock out Widgets and
* other UIObjects.
*/
public class GWTMockUtilities {

/**
* Replace the normal GWT.create() behavior with a method that returns null
* instead of throwing a runtime exception. This is to allow JUnit tests to
* mock classes that make GWT.create() calls in their static initializers.
* This is not for use with GWTTestCase, and is not for use testing widgets
* themselves. Rather, it is to allow pure java unit tests of classes that
* need to manipulate widgets.
*
* <p>
* <b>NOTE:</b> Be sure to call {@link #restore} in your tearDown
* method, to avoid confusing downstream tests.
*
* <p>
* Sample use:
*
* <pre>&#64;Override
* public void setUp() throws Exception {
* super.setUp();
* GWTMockUtilities.disarm();
* }
*
* &#64;Override
* public void tearDown() {
* GWTMockUtilities.restore();
* }
*
* public void testSomething() {
* MyStatusWidget mock = EasyMock.createMock(MyStatusWidget.class);
* EasyMock.expect(mock.setText("expected text"));
* EasyMock.replay(mock);
*
* StatusController controller = new StatusController(mock);
* controller.setStatus("expected text");
*
* EasyMock.verify(mock);
* }
* </pre>
*/
public static void disarm() {
GWTBridge bridge = new GWTDummyBridge();
setGwtBridge(bridge);
}

public static void restore() {
setGwtBridge(null);
}

/**
* Install the given instance of {@link GWTBridge}, allowing it to override
* the behavior of calls to {@link GWT#create(Class)}.
*/
private static void setGwtBridge(GWTBridge bridge) {
Class<GWT> gwtClass = GWT.class;
Class<?>[] paramTypes = new Class[] {GWTBridge.class};
Method setBridgeMethod = null;
try {
setBridgeMethod = gwtClass.getDeclaredMethod("setBridge", paramTypes);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
setBridgeMethod.setAccessible(true);
try {
setBridgeMethod.invoke(gwtClass, new Object[] {bridge});
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}

Change log

r8931 by p...@google.com on Oct 4, 2010   Diff
Created a branch for the GWT 2.1 release.
Go to: 
Project members, sign in to write a code review

Older revisions

r3173 by sco...@google.com on Jul 8, 2008   Diff
Merging releases/1.5@r3125:r3170 into
trunk.

svn merge -r3125:3170 https://google-
web-toolkit.googlecode.com/svn/release
...
r3126 by zun...@google.com on Jun 25, 2008   Diff
Merging releases/1.5@r3078:r3125 into
trunk.

svn merge -r3078:3125 https://google-
web-toolkit.googlecode.com/svn/release
...
r3125 by sco...@google.com on Jun 24, 2008   Diff
This change introduces a new public
class, GWTMockUtilities, aimed at
allowing developers to mock out GWT
Widgets in JUnit tests. If you're
developing in an MVC style, this
...
All revisions of this file

File info

Size: 3101 bytes, 99 lines

File properties

svn:mime-type
text/x-java
svn:eol-style
native
Powered by Google Project Hosting