Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
6
Help us understand the problem. What is going on with this article?
@quwahara

JavaScriptをJUnitとNashornで試験する

More than 5 years have passed since last update.

はじめに

JavaScript Codeのうち、UIに関係無いような処理だけでも、Build時に試験できないかと思い、簡単な実装を試してみました。

環境

$ gradle -version

------------------------------------------------------------
Gradle 2.2.1
------------------------------------------------------------

Build time:   2014-11-24 09:45:35 UTC
Build number: none
Revision:     6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4a

Groovy:       2.3.6
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.8.0_45 (Oracle Corporation 25.45-b02)
OS:           Mac OS X 10.10.4 x86_64

実装

webjarのSugar.jsを読み込んで、JavaScriptで試験対象の処理を行い、JUnitで処理結果を確認しています。

(7/28追記)
Rhinoでの実装も加えました。

HogeTest.java
import static org.junit.Assert.assertEquals;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;


public class HogeTest {

    @Test
    public void test() throws Exception {
        ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext();
        try {
            ScriptEngineManager factory = new ScriptEngineManager();
            ScriptEngine engine = factory.getEngineByName("nashorn");

            Resource r = appCtx.getResource("classpath:/META-INF/resources/webjars/sugar/1.4.1/sugar-full.min.js");
            String sugarJs = IOUtils.toString(r.getInputStream(), "UTF-8");

            String input = "HogeTest";
            String s = "";
            s += sugarJs + "\n";
            s += "var input = '" + input + "'" + "\n";
            s += "var actual = input.dasherize()" + "\n";

            engine.eval(s);

            assertEquals("hoge-test", engine.get("actual"));

        } finally {
            appCtx.close();
        }
    }

    @Test
    public void testWithRhino() throws Exception {
        ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext();
        try {
            Context cx = Context.enter();
            try {
                Scriptable scope = cx.initStandardObjects();

                Resource r = appCtx.getResource("classpath:/META-INF/resources/webjars/sugar/1.4.1/sugar-full.min.js");
                String sugarJs = IOUtils.toString(r.getInputStream(), "UTF-8");

                String input = "HogeTest";
                String s = "";
                s += sugarJs + "\n";
                s += "var input = '" + input + "'" + "\n";
                s += "var actual = input.dasherize()" + "\n";

                cx.evaluateString(scope, s, "<cmd>", 1, null);

                assertEquals("hoge-test", scope.get("actual", null));

            } finally {
                Context.exit();
            }
        } finally {
            appCtx.close();
        }
    }

}

所感

思いの外、時間がかかる。

参考

Source Repository
https://github.com/quwahara/test-javascript-with-junit-example

Java Platform, Standard Edition Nashorn User's Guide, 1 Introduction
https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/intro.html#sthref14

Spring Resource loader with getResource() example
By mkyong
http://www.mkyong.com/spring/spring-resource-loader-with-getresource-example/

Rhino Examples
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Examples

6
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
quwahara
Twitter is @quwahara

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
6
Help us understand the problem. What is going on with this article?