如何将node.js合并到javatesting中?

我在看Mercado Libre Java SDK ,注意到他们的资源库中有一个名为“mockapi”的文件夹。 看起来他们已经用JavaScript编写了一个完整的模拟API,用来testing他们的Java客户端。 但是,我还是不太明白它是如何工作的。 我已经发布了一些在app.js中使用的javascript来做到这一点。

var express = require('express'); var fs = require('fs'); var app = express.createServer(); app.configure(function(){ app.use(express.methodOverride()); app.use(express.bodyParser()); }); app.post('/oauth/token', function(req, res) { if(req.query["grant_type"]=="authorization_code") { if(req.query["code"]=="bad code") { res.send({"message":"Error validando el parámetro code","error":"invalid_grant","status":400,"cause":[]}, 400); } else if(req.query["code"]=="valid code without refresh token") { res.send({ "access_token" : "valid token", "token_type" : "bearer", "expires_in" : 10800, "scope" : "write read" }); } else if(req.query["code"]=="valid code with refresh token") { res.send({ "access_token" : "valid token", "token_type" : "bearer", "expires_in" : 10800, "refresh_token" : "valid refresh token", "scope" : "write read" }); } else { res.send(404); } } else if(req.query['grant_type']=='refresh_token') { if(req.query['refresh_token']=='valid refresh token') { res.send({ "access_token" : "valid token", "token_type" : "bearer", "expires_in" : 10800, "scope" : "write read" }); } } }); app.listen(3000); fs.writeFileSync('/tmp/mockapi.pid', process.pid); 

另外,看着他们的文件package.json看起来他们正在使用节点。 该文件在下面列出。

 { "name": "mockapi", "version": "1.0.0", "dependencies": { "express" : "2.5.x" }, "engine": "node ~> 0.8.x" } 

然而,在看完他们的pom文件之后,看起来他们看起来并没有Node作为依赖关系。我主要完成了Java开发,所以服务器端JavaScript对我来说依然是陌生的。 这是如何工作的? 为什么他们不必在他们的POM文件中包含一个依赖项? 我也应该注意到,大多数testing都失败了,因为我无法与localhost:3000build立连接。 我必须安装node.js才能运行testing吗?

编辑:我已经添加了testing下面的post以及一些其他testing的Junittesting

 package com.mercadolibre.sdk; import java.io.IOException; import org.junit.Assert; import org.junit.Test; import com.ning.http.client.FluentStringsMap; import com.ning.http.client.Response; public class MeliTest extends Assert { @Test public void testGetAuthUrl() { assertEquals( "https://auth.mercadolibre.com.ar/authorization?response_type=code&client_id=123456&redirect_uri=http%3A%2F%2Fsomeurl.com", new Meli(123456l, "client secret") .getAuthUrl("http://someurl.com")); } @Test(expected = AuthorizationFailure.class) public void testAuthorizationFailure() throws AuthorizationFailure { Meli.apiUrl = "http://localhost:3000"; new Meli(123456l, "client secret").authorize("bad code", "http://someurl.com"); } @Test public void testAuthorizationSuccess() throws AuthorizationFailure { Meli.apiUrl = "http://localhost:3000"; Meli m = new Meli(123456l, "client secret"); m.authorize("valid code with refresh token", "http://someurl.com"); assertEquals("valid token", m.getAccessToken()); assertEquals("valid refresh token", m.getRefreshToken()); } @Test public void testGetWithRefreshToken() throws MeliException, IOException { Meli.apiUrl = "http://localhost:3000"; Meli m = new Meli(123456l, "client secret", "expired token", "valid refresh token"); FluentStringsMap params = new FluentStringsMap(); params.add("access_token", m.getAccessToken()); Response response = m.get("/users/me", params); assertEquals(200, response.getStatusCode()); assertFalse(response.getResponseBody().isEmpty()); } public void testPost() throws MeliException { Meli m = new Meli(123456l, "client secret", "valid token"); FluentStringsMap params = new FluentStringsMap(); params.add("access_token", m.getAccessToken()); Response r = m.post("/items", params, "{\"foo\":\"bar\"}"); assertEquals(201, r.getStatusCode()); } 

您的问题的简短答案是,是的,您需要安装节点,并使其在您的path中运行testing。 这就是为什么你没有得到3000的连接。

您对“不知道如何工作”的评论 – 简短的回答是,节点中运行的JavaScript代码本身就是一个Web服务器。 节点不是像Tomcat,Glassfish等那样使用的容器。一般来说,上面粘贴的Javascript代码使用Express(最广泛使用的节点Web框架,连同Connect给你一些Servlet API的function)。 创build的服务器响应单个types的请求,POST到/oauth/token

这是如何工作的?

它看起来像JavaScript的devise是一个模拟服务器; 即响应由客户端库代码发送的请求。 你添加的unit testing似乎是这样做的。

为什么他们不必在他们的POM文件中包含一个依赖项?

目的是为了运行这些testing,您应该手动设置testing服务器。 此外,声明一个Maven依赖于一个(非Java)软件的必须安装并启动进行unit testing是没有意义的。

(你也许可以让Maven做这样的事情,但我不确定这是一个好主意,显然,项目开发者并没有这样做……)

这个理论是由Makefile支持的。

我必须安装node.js才能运行testing吗?

我build议你尝试一下,看看…和/或阅读并理解Makefile及其意义。


但是,严肃地说,你可能应该问这些作者的这些问题。 自述文件说:

“如果您有使用开发人员站点中描述的标准通信渠道的问题,可以联系我们”

并有一个链接到该网站。

一旦你想出了,通过loggingtesting程序,向他们发送一个拉取请求,以改善他们的开发者文档。