Skip to content

JavaAnalysis.get_test_methods() gives incorrect name when comment placed between @Test annotation and non-public test method #147

@NateLevin1

Description

@NateLevin1

Describe the bug
The get_test_methods() method of the JavaAnalysis analyzer in the Python SDK gives an incorrect test method name of void when a comment is placed between the JUnit @Test annotation and a test method that is not marked public.

To Reproduce
Steps to reproduce the behavior:

  1. Run the below code:
from cldk import CLDK
src = """class ExampleTest {
    @Test
    // this valid comment breaks test method name detection; try removing it
    void exampleTest() { }
}
"""
ja = CLDK(language="java").analysis(source_code=src)
tests = ja.get_test_methods()
print(tests)

Expected behavior
The output should be a dictionary along the lines of {'exampleTest': '...'}
Instead, the following is output: {'void': '...'}

Additional context
Doing either of the following allows the analyzer to properly detect the name:

  1. Adding a public qualifier (e.g. edit line 5 to be public void exampleTest() { }
  2. Removing the comment (e.g. remove line 4)

This pattern can be seen in production code; for example, in this snippet from the Apache commons-lang project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions