Layman
Layman

Reputation: 89

How do I test a method in java with objects I created? (Helpful title suggestions are welcome.)

I am going through a class to learn about JAVA...

I was told to add a method (void swapNames(Greeter other)) that swaps the names of this greeter and another. Then to create two objects in the Greeter class and use the swapNames method to swap their names.

I started with ..

public class Greeter {

    public Greeter(String aName){
        name = aName;
    }


    public String sayHello(){
        return "Hello, " + name + "!";
    }

    private static String name;

    public static void swapNames(Greeter other){

        String aux = name;
        name = other.getName();
        other.setName(aux);
    }

    private void setName(String aux) {
        Greeter.name = aux;
    }

    private String getName() {
        return name;
    }

}

But I am unsure how to test my code.

I have a tester class with a main method, and I have created two object instances of Greeter..

Greeter nameGreeter = new Greeter("John Smith");
Greeter nameGreeter = new Greeter("Jane Doe");

I'm not sure where to go with this though.

Upvotes: 0

Views: 3653

Answers (3)

v.coder
v.coder

Reputation: 1932

AssertEquals can be used to check the values of Strings.

The Test can go like this:

Greeter greetsJohn = new Greeter("John Smith");
Greeter greetsJane = new Greeter("Jane Doe");

greetsJohn.swapNames(greetsJane);

assertEquals("Name in greetsJohn is replaced with jane","Jane Doe",greetsJohn.sayHello());

Syntax: assertEquals(message, expectedValue, actualValue);

Upvotes: 0

laune
laune

Reputation: 31290

Don't use a single static variable to hold anything that should be the property of a certain object instance.

class Greeter {
    private String name; // like this!

    public void swap( Greeter other ){ // like this!

Now the test:

Greeter greetsJohn = new Greeter( "John Smith" );
Greeter greetsJane = new Greeter( "Jane Doe" );
System.out.println( greetsJohn.sayHello() );
System.out.println( greetsJane.sayHello() );
greetsJohn.swapNames( greetsJane );
System.out.println( greetsJohn.sayHello() );
System.out.println( greetsJane.sayHello() );

A static String name means that everybody says hello to the same person.

Upvotes: 1

Mena
Mena

Reputation: 48404

If your swapNames method is static (as in question code), it should take two Greeters as arguments, otherwise it won't know this Greeter's name when swapping with the other.

I would make the method instance (remove the static keyword), then:

  • Store other.getName() in a method-scoped variable
  • Set other.setName(this.name)
  • Set this.name as your stored variable

You can then instantiate two Greeters with different names, invoke the swap method on one with the other as argument, and test both through your getter or your sayHello method.

The choice of your test scope will then impact on the framework or methodology you choose: either just print or log, assert, or use some specialized frameworks such as JUnit.

Upvotes: 1

Related Questions