Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting NoSuchMethodException when running class despite method being in .class file

I am writing a backend for a project at my university and hit an error I do not comprehend.

I am working with IntelliJ, Gradle and use Spock as a testing environment. One if the goals of the project is to utilize Amazons NoSQL Database solution DynamoDB. To test the methods of the backend AmazonDB Local is used.

Currently I am writing tests for the various methods of the backend... or at least i tried. When I run my first test i get a NoSuchMethodException as soon as "GetBookWithTitle" method is reached. (Funny fact: the method "AddBook" seems to be working just fine).

This is the code of the Test:

def "Test adding a book to the database"(){
    when: "Adding a book"
        methods.AddBook("TestBook", "Sebastian Müller", 1990, "Gregors", "FunkyTestCover", mapper);

    then: "The book should be added"
        methods.GetBookWithTitle("TestBook", mapper) != null;
}

This is the source of the "GetBookWithTitle" method:

public Book GetBookWithTitle(String title, DynamoDBMapper mapper){
    Book result = mapper.load(Book.class, title);
    return result;
}

What I tried: - Cleaning the project with the corresponding gradle task - deleting the "build" folder of the project by hand - decompiling the .class file to make sure "GetBookWithTitle" IS present

So my question is: Has somebody out there a clue from where this error originates?

If the information provided are not sufficient I will give you more :)

like image 232
zetain Avatar asked Oct 19 '25 15:10

zetain


1 Answers

Ok i found the answer. It was something completely else and related to DynamoDB and its feature to map database items to java classes. The mapper class obviously needs a standard constructor which my Book class had not.

For those who are interested:

The DynamoDBMapper.load method calls internally Class.NewInstance(). This method can only use the standard constructor/ constructor without arguments. As there was no such constructor in my class he threw the NoSuchMethodException.

like image 91
zetain Avatar answered Oct 21 '25 06:10

zetain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!