Skip to content

Incorrect type after throws keyword leads to compilation errors #1093

@mmvpm

Description

@mmvpm

Description

Consider a case when we have a custom class named like a standard exception. It cause compilation error when we try to generate test code for it.

To Reproduce

Consider the following class:

public class ClassNotFoundException {
    public Class<?> getClass(String className) throws java.lang.ClassNotFoundException {
        return Class.forName(className);
    }
}

The generated test looks like as follows:

@Test
@DisplayName("getClass: className = blank string -> throw ClassNotFoundException")
public void testGetClassThrowsCNFEWithBlankString() throws ClassNotFoundException  {
    ClassNotFoundException classNotFoundException = new ClassNotFoundException();

    assertThrows(ClassNotFoundException.class, () -> classNotFoundException.getClass("   "));
}

This code does not compile because the declaration of the test method must be replaced with the following:

public void testGetClassThrowsCNFEWithBlankString() throws java.lang.ClassNotFoundException

And the last line must be replaces with:

assertThrows(java.lang.ClassNotFoundException.class, () -> classNotFoundException.getClass("   "));

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorctg-bugIssue is a bug

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions