2 Stimmen

Beispiel für den Test eines RPC-Aufrufs mit GWT-TestCase und GAE

Das sind aber viele Akronyme!

Ich habe Probleme beim Testen von GWTs RPC-Mechanismus mit GWTs GWTTestCase. Ich habe eine Klasse zum Testen mit dem JunitCreator-Tool erstellt, das in GWT enthalten ist. Ich versuche, mit der eingebauten Google App Engine zu testen, indem ich das von junitCreator erstellte Testprofil "Hosted Mode" verwende. Wenn ich den Test ausführe, erhalte ich immer wieder Fehlermeldungen, wie z. B.

Starting HTTP on port 0
   HTTP listening on port 49569
The development shell servlet received a request for 'greet' in module 'com.google.gwt.sample.stockwatcher.StockWatcher.JUnit.gwt.xml' 
   [WARN] Resource not found: greet; (could a file be missing from the public path or a <servlet> tag misconfigured in module com.google.gwt.sample.stockwatcher.StockWatcher.JUnit.gwt.xml ?)
com.google.gwt.user.client.rpc.StatusCodeException: Cannot find resource 'greet' in the public path of module 'com.google.gwt.sample.stockwatcher.StockWatcher.JUnit'

Ich hoffe, dass jemand irgendwo erfolgreich Junit Test (mit GWTTestCase oder einfach nur TestCase), die für die Prüfung von gwt RPC ermöglichen ausgeführt hat. Wenn dies der Fall ist, könnten Sie bitte erwähnen, die Schritte, die Sie nahm, oder besser noch, nur posten Code, der funktioniert. Danke.

1voto

Trung Punkte 962

SyncProxy ermöglicht es Ihnen, GWT RPC-Aufrufe von Java aus zu tätigen. So können Sie Ihre GWT RPC mit regulären Testcase testen (und schneller als GwtTestcase)

Siehe
http://www.gdevelop.com/w/blog/2010/01/10/testing-gwt-rpc-services/
http://www.gdevelop.com/w/blog/2010/03/13/invoke-gwt-rpc-services-deployed-on-google-app-engine/

1voto

Ryan Shillington Punkte 19095

Ich habe das hinbekommen. Diese Antwort geht davon aus, dass Sie Gradle verwenden, aber dies könnte leicht angepasst werden, um von Ant laufen. Zunächst müssen Sie sicherstellen, dass Sie Ihre GWT-Tests von Ihren regulären JUnit-Tests trennen. Ich habe 'tests/standalone' für die regulären Tests und 'tests/gwt' für meine GWT-Tests erstellt. Am Ende erhalte ich immer noch einen einzigen HTML-Bericht, der alle Informationen enthält.

Als nächstes müssen Sie sicherstellen, dass JUnit Teil Ihres Ant-Klassenpfads ist, wie hier beschrieben:

http://gradle.1045684.n5.nabble.com/Calling-ant-test-target-fails-with-junit-classpath-issue-newbie-td4385167.html

Verwenden Sie dann etwas Ähnliches wie dieses, um Ihre GWT-Tests zu kompilieren und sie auszuführen:

    task gwtTestCompile(dependsOn: [compileJava]) << {
    ant.echo("Copy the test sources in so they're part of the source...");
    copy {
        from "tests/gwt"
        into "$buildDir/src"
    }
    gwtTestBuildDir = "$buildDir/classes/test-gwt";
    (new File(gwtTestBuildDir)).mkdirs()
    (new File("$buildDir/test-results")).mkdirs()

    ant.echo("Compile the tests...");
    ant.javac(srcdir: "tests/gwt", destdir: gwtTestBuildDir) {
        classpath {
            pathElement(location: "$buildDir/src")
            pathElement(location: "$buildDir/classes/main")
            pathElement(path: configurations.runtime.asPath)
            pathElement(path: configurations.testCompile.asPath)
            pathElement(path: configurations.gwt.asPath)
            pathElement(path: configurations.gwtSources.asPath)
        }
    }

    ant.echo("Run the tests...");
    ant.junit(haltonfailure: "true", fork: "true") {
        classpath {
            pathElement(location: "$buildDir/src")
            pathElement(location: "$buildDir/classes/main")
            pathElement(location: gwtTestBuildDir)
            pathElement(path: configurations.runtime.asPath)
            pathElement(path: configurations.testCompile.asPath)
            pathElement(path: configurations.gwt.asPath)
            pathElement(path: configurations.gwtSources.asPath)
        }
        jvmarg(value: "-Xmx512m")
        jvmarg(line: "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005")
        test(name: "com.onlyinsight.client.LoginTest", todir: "$buildDir/test-results")
        formatter(type: "xml")
    }
}

test.dependsOn(gwtTestCompile);

Zum Schluss noch ein einfacher GWT-Test:

public class LoginTest extends GWTTestCase  
{
    public String getModuleName()
    {
        return "com.onlyinsight.ConfModule";
    }

    public void testRealUserLogin()
    {
        UserServiceAsync userService = UserService.App.getInstance();

        userService.login("a", "a", new AsyncCallback<User>()
        {
            public void onFailure(Throwable caught)
            {
                throw new RuntimeException("Unexpected Exception occurred.", caught);
            }

            public void onSuccess(User user)
            {
                assertEquals("a", user.getUserName());
                assertEquals("a", user.getPassword());
                assertEquals(UserRole.Administrator, user.getRole());
                assertEquals("Test", user.getFirstName());
                assertEquals("User", user.getLastName());
                assertEquals("canada@onlyinsight.com", user.getEmail());

                // Okay, now this test case can finish.
                finishTest();
            }
        });

        // Tell JUnit not to quit the test, so it allows the asynchronous method above to run.
        delayTestFinish(10 * 1000);
    }
}

Wenn Ihre RPC-Instanz keine praktische getInstance()-Methode hat, dann fügen Sie eine hinzu:

public interface UserService extends RemoteService {

    public User login(String username, String password) throws NotLoggedInException;

    public String getLoginURL(OAuthProviderEnum provider) throws NotLoggedInException;

    public User loginWithOAuth(OAuthProviderEnum provider, String email, String authToken) throws NotLoggedInException;

    /**
     * Utility/Convenience class.
     * Use UserService.App.getInstance() to access static instance of UserServiceAsync
     */
    public static class App {
        private static final UserServiceAsync ourInstance = (UserServiceAsync) GWT.create(UserService.class);

        public static UserServiceAsync getInstance()
        {
            return ourInstance;
        }
    }
}

Ich hoffe, das hilft.

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X