lsp-java
lsp-java copied to clipboard
Cannot run tests in default package
Describe the bug
JUnit tests in a class in the default package cannot be executed with dap-java-run-test-method
(or any of the dap test functions).
As far as I can tell this happens because the code assumes that test classes are always in a named package, and the fully qualified name for the class is computed with an starting dot.
As far as I could test, a small change in dap-java-test-class
and dap-java-test-method-at-point
might be enough:
(defun dap-java--fully-qualified-class-name (package-name class-name)
"Compute a fully qualified class name as specified in
https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#jls-6.7"
(if (and (stringp package-name) (not (string-blank-p package-name)))
(concat package-name "." class-name)
class-name))
(defun dap-java-test-class (&optional no-signal?)
"Get class FDQN."
(-if-let* ((symbols (lsp--get-document-symbols))
(package-name (-some->> symbols
(-first (-lambda ((&DocumentSymbol :kind)) (= kind lsp/symbol-kind-package)))
lsp:document-symbol-name))
(class-name (->> symbols
(--first (= (lsp:document-symbol-kind it) lsp/symbol-kind-class))
lsp:document-symbol-name)))
(dap-java--fully-qualified-class-name package-name class-name)
(unless no-signal?
(user-error "No class found"))))
(defun dap-java-test-method-at-point (&optional no-signal?)
"Get method at point."
(-let* ((symbols (lsp--get-document-symbols))
(package-name (-some->> symbols
(-first (-lambda ((&DocumentSymbol :kind)) (= kind lsp/symbol-kind-package)))
lsp:document-symbol-name)))
(or (->> symbols
(-keep (-lambda ((&DocumentSymbol :children? :kind :name class-name))
(and (= kind lsp/symbol-kind-class)
(seq-some
(-lambda ((&DocumentSymbol :kind :range :selection-range))
(-let (((beg . end) (lsp--range-to-region range)))
(and (= lsp/symbol-kind-method kind) (<= beg (point) end)
(concat (dap-java--fully-qualified-class-name package-name class-name) "#"
(lsp-region-text selection-range)))))
children?))))
(cl-first))
(unless no-signal?
(user-error "No method at point")))))
To Reproduce Run a JUnit test class/method in a class in the default (unnamed) package.
Expected behavior The test is executed by JUnit.
Logs lsp-workspace-command-execute: ‘workspace/executeCommand’ with ‘vscode.java.resolveClasspath’ failed. (error "Failed to resolve classpath: Main class ’.CommandTest’ doesn’t exist in the workspace.")
lsp-java version Version: 20230507.517 Commit: 961f1a13e3db9a6b395584be99ef9572bb414e63
emacs version GNU Emacs 30.0.50 (build 1, aarch64-apple-darwin21.6.0, NS appkit-2113.60 Version 12.5 (Build 21G72)) of 2023-05-16
I think run test by your own mvn test phase is better.