1

Spring MVC でのハンドラー マッピングの存在をテストしようとしています。これは、特定のリクエストを非標準のハンドラー マッピングで処理する必要があるいくつかのカスタム ケースを抽象化するのに役立ちます。

「/*/registration/register/custom」をマッピングするという簡単な言い方がわかりませんが、存在しますか?

何か案は?

マルク

4

1 に答える 1

0

マッピングをテストする簡単な方法:

import java.net.HttpURLConnection;
import java.net.URL;
import junit.framework.TestCase;
import org.junit.Test;
public class HomeControllerTest extends TestCase{

    @Test
    public void test() {
        assertEquals(true, checkIfURLExists("http://localhost:8080/test"));
    }


    public static boolean checkIfURLExists(String targetUrl) {
        HttpURLConnection httpUrlConn;
        try {
            httpUrlConn = (HttpURLConnection) new URL(targetUrl).openConnection();
            httpUrlConn.setRequestMethod("GET");

            // Set timeouts in milliseconds
            httpUrlConn.setConnectTimeout(30000);
            httpUrlConn.setReadTimeout(30000);

            // Print HTTP status code/message for your information.
            System.out.println("Response Code: " + httpUrlConn.getResponseCode());
            System.out.println("Response Message: " + httpUrlConn.getResponseMessage());

            return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
            return false;
        }
    }
}

Spring ドキュメントから: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/#unit-testing-spring-mvc

9.2.2.2 スプリング MVC

org.springframework.test.web パッケージには ModelAndViewAssert が含まれており、Spring MVC の ModelAndView オブジェクトを扱う単体テスト用に JUnit 4+ や TestNG などと組み合わせて使用​​できます。

Spring MVC コントローラーの単体テスト Spring MVC コントローラーをテストするには、ModelAndViewAssert を org.springframework.mock.web パッケージの MockHttpServletRequest、MockHttpSession などと組み合わせて使用​​します。

于 2012-10-06T09:01:00.380 に答える