Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple Maven + JUnit test runner for Emacs.
;;; A quick way to run JUnit tests:
;;; `C-x t u` runs all the test cases in a unit.
;;; `C-x t c` runs a single test case.
;;;
;;; Simply add the snippet at the end of `init.el` or `.emacs`.
;;; NOTE: It assumes you are using projectile to manage the project.
;;;
;;; By Bahman Movaqar
;; runs all test cases in the current class
(defun run-test-unit ()
(interactive)
(require 'imenu)
(let* ((file-name (buffer-file-name))
(class-name (car (split-string
(car (last (split-string file-name "/")))
"\\.")))
(root (projectile-project-root))
(mvn-cmd (concat "cd " root " && "
"mvn -Dtest=" class-name " test ")))
(start-process "test-runner"
"test-runner"
"/bin/bash"
"-c"
mvn-cmd))
(switch-to-buffer "test-runner"))
;; prompts for a single test case in the current class and runs it
(defun run-test-case ()
(interactive)
(require 'imenu)
(let* ((file-name (buffer-file-name))
(class-name (car (split-string
(car (last (split-string file-name "/")))
"\\.")))
(test-case (car (imenu-choose-buffer-index "Test case: ")))
(root (projectile-project-root))
(mvn-cmd (concat "cd " root " && "
"mvn -Dtest=" class-name "#" test-case " test ")))
(start-process "test-runner"
"test-runner"
"/bin/bash"
"-c"
mvn-cmd))
(switch-to-buffer "test-runner"))
; key bindings
(global-set-key (kbd "C-x t u") 'run-test-unit)
(global-set-key (kbd "C-x t c") 'run-test-case)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment