Skip to content

Commit

Permalink
Merge pull request #42 from jonathan-elize/bugfix/handle-test-errors-…
Browse files Browse the repository at this point in the history
…at-start

fix: prevent false positives when test fails to start because of an error
  • Loading branch information
rcasia authored Nov 4, 2023
2 parents 68491bb + 8198aee commit 6dc433a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lua/neotest-java/core/result_builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ function ResultBuilder.build_results(spec, result, tree)
results[node_data.id] = {
status = "skipped",
}
elseif test_case.error then
results[node_data.id] = {
status = "failed",
short = test_case.error._attr.message,
}
elseif test_case.failure then
results[node_data.id] = {
status = "failed",
Expand Down
37 changes: 37 additions & 0 deletions tests/core/result_builder_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,43 @@ describe("ResultBuilder", function()
assert_equal_ignoring_whitespaces(expected, actual)
end)

async.it("builds the results for a maven test that has an error at start", function()
--given
local runSpec = {
cwd = get_current_dir() .. "tests/fixtures/maven-demo",
context = {
project_type = "maven",
test_class_path = "com.example.ExampleTest",
},
}

local strategyResult = {
code = 0,
output = "output",
}

local file_path = get_current_dir() .. "tests/fixtures/maven-demo/src/test/java/com/example/ErroneousTest.java"
local tree = plugin.discover_positions(file_path)

--when
local results = plugin.results(runSpec, strategyResult, tree)

--then
local actual = table_to_string(results)
local expected = [[
{
["{{current_dir}}tests/fixtures/maven-demo/src/test/java/com/example/ErroneousTest.java::shouldFailOnError"] = {
short = "Error creating bean with name 'com.example.ErroneousTest': Injection of autowired dependencies failed",
status = "failed"
}
}
]]

expected = expected:gsub("{{current_dir}}", get_current_dir())

assert_equal_ignoring_whitespaces(expected, actual)
end)

async.it("builds the results for gradle", function()
--given
local runSpec = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.demo.DemoApplication;

@SpringBootTest(classes = { DemoApplication.class })
public class ErroneousTest {
@Value("${foo.property}") String requiredProperty;

@Test
void shouldFailOnError(){
assertEquals("test", requiredProperty);
}
}

0 comments on commit 6dc433a

Please sign in to comment.