Skip to content

Commit e4cc304

Browse files
authored
feat: add JavaTestRunAllTests and JavaTestDebugAllTests commands (#467)
1 parent d4050eb commit e4cc304

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

lua/java-test/api.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,38 @@ function M:find_current_test_method()
157157
end
158158
end
159159

160+
---Run all tests in the workspace
161+
---@param report java-test.JUnitTestReport
162+
---@param config java-dap.DapLauncherConfigOverridable
163+
function M:execute_all_tests(report, config)
164+
log.debug('running all tests')
165+
166+
local projects = self.test_client:find_java_projects()
167+
168+
if #projects < 1 then
169+
notify.warn('No Java projects found')
170+
return
171+
end
172+
173+
-- Discover test classes from all projects
174+
local all_tests = {}
175+
for _, project in ipairs(projects) do
176+
local packages = self.test_client:find_test_packages_and_types(project.jdtHandler)
177+
for _, pkg in ipairs(packages or {}) do
178+
-- Package children are the test classes
179+
for _, test_class in ipairs(pkg.children or {}) do
180+
table.insert(all_tests, test_class)
181+
end
182+
end
183+
end
184+
185+
if #all_tests < 1 then
186+
notify.warn('No tests found in workspace')
187+
return
188+
end
189+
190+
log.debug('found ' .. #all_tests .. ' test classes')
191+
self:run_test(all_tests, report, config)
192+
end
193+
160194
return M

lua/java-test/init.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,34 @@ function M.run_current_method()
7171
.run()
7272
end
7373

74+
function M.run_all_tests()
75+
log.info('run all tests')
76+
77+
return runner(function()
78+
local test_api = JavaTestApi:new({
79+
client = lsp_utils.get_jdtls(),
80+
runner = DapRunner(),
81+
})
82+
return test_api:execute_all_tests(M.get_report(), { noDebug = true })
83+
end)
84+
.catch(get_error_handler('failed to run all tests'))
85+
.run()
86+
end
87+
88+
function M.debug_all_tests()
89+
log.info('debug all tests')
90+
91+
return runner(function()
92+
local test_api = JavaTestApi:new({
93+
client = lsp_utils.get_jdtls(),
94+
runner = DapRunner(),
95+
})
96+
return test_api:execute_all_tests(M.get_report(), {})
97+
end)
98+
.catch(get_error_handler('failed to debug all tests'))
99+
.run()
100+
end
101+
74102
function M.view_last_report()
75103
if M.last_report then
76104
M.last_report:show_report()

plugin/java.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ local cmd_map = {
3535
end,
3636
},
3737

38+
JavaTestRunAllTests = {
39+
function()
40+
require('java-test').run_all_tests()
41+
end,
42+
},
43+
44+
JavaTestDebugAllTests = {
45+
function()
46+
require('java-test').debug_all_tests()
47+
end,
48+
},
49+
3850
JavaTestViewLastReport = {
3951
function()
4052
require('java-test').view_last_report()

0 commit comments

Comments
 (0)