如何使用独立的tomcat runner进行集成测试?

bq9c1y66  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(324)

我们的项目目前没有使用spring框架。
因此,它正在基于独立的tomcat运行程序进行测试。
但是,由于集成启用了测试,例如 @SpringBootTest 不可能,tomcat是预先操作的,httpapi测试是使用spock执行的。
有没有办法把这个变成这样 @SpringBootTest ?
tomcatrunner公司

private Tomcat tomcat = null;
    private int port = 8080;
    private String contextPath = null;
    private String docBase = null;
    private Context rootContext = null;

    public Tomcat8Launcher(){
        init();
    }

    public Tomcat8Launcher(int port, String contextPath, String docBase){
        this.port = port;
        this.contextPath = contextPath;
        this.docBase = docBase;
        init();
    }

    private void init(){
        tomcat = new Tomcat();
        tomcat.setPort(port);
        tomcat.enableNaming();
        if(contextPath == null){
            contextPath = "";
        }
        if(docBase == null){
            File base = new File(System.getProperty("java.io.tmpdir"));
            docBase = base.getAbsolutePath();
        }
        rootContext = tomcat.addContext(contextPath, docBase);
    }

    public void addServlet(String servletName, String uri, HttpServlet servlet){
        Tomcat.addServlet(this.rootContext, servletName, servlet);
        rootContext.addServletMapping(uri, servletName);
    }

    public void addListenerServlet(ServletContextListener listener){
        rootContext.addApplicationListener(listener.getClass().getName());
    }

    public void startServer() throws LifecycleException {
        tomcat.start();
        tomcat.getServer().await();
    }

    public void stopServer() throws LifecycleException {
        tomcat.stop();
    }

    public static void main(String[] args) throws Exception {
        System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
        System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, "org.apache.naming");

        Tomcat8Launcher tomcatServer = new Tomcat8Launcher();
        tomcatServer.addListenerServlet(new ConfigInitBaseServlet());
        tomcatServer.addServlet("restServlet", "/rest/*", new RestServlet());
        tomcatServer.addServlet("jsonServlet", "/json/*", new JsonServlet());
        tomcatServer.startServer();
    }

spock api测试示例

class apiTest extends Specification {
    //static final Tomcat8Launcher tomcat = new Tomcat8Launcher()
    static final String testURL = "http://localhost:8080/api/"

    @Shared
    def restClient

    def setupSpec() {
        // tomcat.main()
        restClient = new RESTClient(testURL)
    }

    def 'findAll user'() {
        when:
        def response = restClient.get([path: 'user/all'])

        then:
        with(response){
            status == 200
            contentType == "application/json"
        }
    }
}

如果从下面的注解中删除注解,测试将不起作用。 // static final Tomcat8Launcher tomcat = new Tomcat8Launcher() 这条线在顶部指定为api测试。 // tomcat.main() 这一行是api test setupspec()方法指定的
我不知道为什么,但是在tomcat运行之后只记录日志,测试方法没有执行。
有办法解决这个问题吗?

ruoxqz4g

ruoxqz4g1#

我建议创建一个spock扩展来封装您需要的所有内容。请参阅编写spock文档的自定义扩展以及内置扩展以获取灵感。

相关问题