开发者

Testing toString Junit

开发者 https://www.devze.com 2023-02-25 20:44 出处:网络
Given a toString method: public String toStr开发者_开发知识库ing() { String accountString; NumberFormat money = NumberFormat.getCurrencyInstance();

Given a toString method:

public String toStr开发者_开发知识库ing()
{
    String accountString;
    NumberFormat money = NumberFormat.getCurrencyInstance();

    accountString = cust.toString();
    accountString += " Current balance is " + money.format (balance);
    return accountString;
}

how i can test it with Junit?


Here's how I'd do it:

public class AccountTest
{
    @Test
    public void testToString()
    {
        Account account = new Account(); // you didn't supply the object, so I guessed
        String expected = ""; // put the expected value here
        Assert.assertEquals(expected, account.toString());
    }
}


A very good library for testing toString on model classes: https://github.com/jparams/to-string-verifier

Sample:

@Test
public void myTest()
{
    ToStringVerifier.forClass(Student.class).verify();
}


I've been wondering about this too and while I think duffymo's answer is fine, it's not the best.

The problems I see are things you can't always know or, as you pointed out in comments, newlines and other space characters that probably don't matter.

So I think the better thing is instead of using assertEquals, I would say use assertTrue in conjunction with contains (checks if a specific String exists in the String) and/or matches (uses regex to see if a String holds a certain pattern).

Let's say you're working with a Name class defined as

public class Name {
    // each of these can only be alphabetic characters
    String first;
    String middle;
    String last;

    // a bunch of methods here

    String toString(){
        String out = "first=" + first + "\n" +
                     "middle=" + middle + "\n" +
                     "last=" + last + "\n" +
                     "full name=" + first + " " middle + " " + last;
    }
}

So now you test this with the following (assume that name is a Name object created in a prior setUp method)

void toStringTest1(){
    String toString = name.toString();
    // this test checks that the String at least is 
    // outputting "first=" followed by the first variable
    // but the first variable may incorrect 1's and 0's
    assertTrue(toString.contains("first=" + first));
}

void toStringTest2(){
    String toString = name.toString();
    // this test checks that the String has "first="
    // followed by only alphabetic characters
    assertTrue(toString.matches("first=[a-zA-Z]*?"));
}

This kind of testing is much more robust than what duffymo has said. If you want toString to contain a hashcode, you wouldn't be able to know beforehand what the expected value might be.

This also allows you to cover a great deal more possibilities. It could be that your one expected value just happens to be a case that it works for.

Also, do keep in mind that you would also want to have tests (in my given case) for setting the first variable. And while one might think that having a setFirst test would be enough, it's better to have more tests across the board because it could be that setFirst works but you have something failing in toString (maybe you split first and added a : without realizing it)

More tests of many kinds is always better!

0

精彩评论

暂无评论...
验证码 换一张
取 消