Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot run tests in default package #437

Open
aci2n opened this issue May 24, 2023 · 1 comment
Open

Cannot run tests in default package #437

aci2n opened this issue May 24, 2023 · 1 comment

Comments

@aci2n
Copy link

aci2n commented May 24, 2023

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: 961f1a1

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

@sincebyte
Copy link

I think run test by your own mvn test phase is better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants