Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for assertEmpty #465

Closed
Barbatruuk opened this issue Jul 25, 2012 · 6 comments
Closed

support for assertEmpty #465

Barbatruuk opened this issue Jul 25, 2012 · 6 comments

Comments

@Barbatruuk
Copy link

Hello,

Sorry if this has been requested/discussed before, I didn't find a reference to a similar request.

I'd like to have an assertEmpty(...) method that tests for instance if a String is empty. I understand that:
assertEquals("", someString);
assertTrue(someString.isEmpty());

have similar effect, but it reads so much easier:
assertEmpty(someString);

any thoughts on that?

Thanks,

@djh82
Copy link
Contributor

djh82 commented Jul 27, 2012

You would normally write:

import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.text.IsEmptyString.*;

public class EmptyStringTest
{
    @Test
    public void test()
    {
        assertThat("", isEmptyString());
    }
}

@djh82
Copy link
Contributor

djh82 commented Jul 27, 2012

If that solves your problem, can you close the issue?

Thanks for your contribution, hopefully it will help someone else too :)

@Barbatruuk
Copy link
Author

Yes and No ;)

I'm using JUnit 4.10 (via maven) which has a dependency to hamcrest-core 1.1, the core version does not have the text package included. There is however, a hamcrest-text 1.1 that does have this package, but not the IsEmptyString class (and it's not in maven). I like to keep version synchronised but it looks like that's not possible in thise case. I can however include hamcrest-all 1.3 and exclude hamcrest-core 1.1 from JUnit (perhaps you could rollout a JUnit version that uses 1.3? :) ).

But wait...

running assertThat("", isEmptyString()); throws a:

    java.lang.NoSuchMethodError: org.hamcrest.core.AnyOf.anyOf([Lorg/hamcrest/Matcher;)Lorg/hamcrest/core/AnyOf;
    at org.hamcrest.text.IsEmptyString.<clinit>(IsEmptyString.java:18)    

because of (copy & paste from decompiled class, excuse the capitalization):

private static final Matcher<String> NULL_OR_EMPTY_INSTANCE = AnyOf.anyOf(new Matcher[] { IsNull.nullValue(), INSTANCE });

which is inside the IsEmptyString class

So, there appears to be an issue in the hamcrest IsEmptyString class, but quite honestly I don't understand what's supposed to happen inside the AnyOf class, perhaps you do?

Guess I have to stick to my old trick.

@djh82
Copy link
Contributor

djh82 commented Jul 31, 2012

You're right, I do apologise. That's what you would need to write if Hamcrest 1.3 was supported ;) See #404.

The AnyOf class allows you to specify an array (vararg) of matchers; the result will be true if 'any of' the matchers return true. The IsEmptyString use of AnyOf is to combine a null matcher with an empty string matcher to create a nullOrEmptyString matcher.

@dsaff
Copy link
Member

dsaff commented Aug 2, 2012

Right, this should be better once we get #404 submitted.

@Barbatruuk
Copy link
Author

Closed hoping the next release will have #404 fixed this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants