javacompiler-errorssyntax-error

Why I get "The method is undefined for the type" during compiling java code


I have cloned supervisor's code on java from github which comes on with some Greeting.test file and other files and after I had done writing Greeting.java code in the src/test/java and run the code, I keep receiving message that says

"message": "The method createGreeting(String) is undefined for the type Greeting"

It is a graddle build failure related message and I am figuring how can I solve this out so that as I commit the code to github there may not be errors?

I tried commenting the lines in the Greeting.test which were generating the error but it even brought more errors and I also tried to follow some different solutions on stack overflow like adding the Greeting class behind the method that initiates the createGreeting but it still could not work. I'd expect that it enables the building successful but it did not do so.

These are the Greeting.test codes

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

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

public class GreetingTest {

    @Test
    public void testCreateGreeting() {
        Greeting greeting = new Greeting();
        String name = "Alice";
        assertEquals("Hello, Alice!", greeting.createGreeting(name), "Greeting message should match expected output.");
    } 

    @Test
    public void testMainOutput() {
        ByteArrayInputStream in = new ByteArrayInputStream("Alice\n".getBytes());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setIn(in);
        System.setOut(new PrintStream(out));

        String[] args = {};
        Greeting.main(args);

        String consoleOutput = out.toString();
        assertTrue(consoleOutput.contains("Hello, Alice!"), "The output should contain the correct greeting.");
    }

}

and these are the codes for Greeting.java class that I have created:

import java.util.Scanner;

public class Greeting {
    public static void main(String[] args) {

        Scanner userInput = new Scanner(System.in);

        System.out.println("Please enter your first name : ");
        String userName = userInput.next();

        System.out.println("Hello " + userName);

    }
}

Solution

  • You can simply add the required method createGreeting next to the ... main(...) method in the Greeting class e.g.

    public class Greeting {
        // a `static` method which can be called without an instance of `Greeting`
        public static void main(String[] args) {
            // current code
        }
        
        // an instance method which needs an instance to be called => `new Greeting()`
        public String createGreeting(String value) {
            // do whats ever necessary to fullfill the assert in the test
        }
    }
    

    If you create a new Greeting class you can omit the main method. Alternatively you can replace the main method in the existing Greeting class and add the createGreeting method. For unit tests the main method is not necessary.