Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6198285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:58:11+00:00 2026-05-24T03:58:11+00:00

I am using junit with hamcrest in my unit tests and I came across

  • 0

I am using junit with hamcrest in my unit tests and I came across a generics problem:


assertThat(collection, empty());

I am aware of type inference not being available this way and that one of the solutions is to give a type hint, but how should I type hint when using static imports?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:58:12+00:00Added an answer on May 24, 2026 at 3:58 am

    While type inference is not as powerful as we would like, in this case, it’s really the API that’s at fault. It unnecessarily restricts itself for no good reason. The is-empty matcher works on any collection, not just on collections of a specific E.

    Suppose the API is designed this way

    public class IsEmptyCollection implements Matcher<Collection<?>>
    {
        public static Matcher<Collection<?>> empty()
        {
            return new IsEmptyCollection();
        }
    }
    

    then assertThat(list, empty()) works as expected.

    You can try to convince the author to change the API. Meanwhile you can have a wrapper

    @SuppressWarnings("unchecked")
    public static Matcher<Collection<?>> my_empty()
    {
        return (Matcher<Collection<?>>)IsEmptyCollection.empty();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing unit tests for Spring Framework 2.5 using JUnit. More specifically, I'm using
I am wondering how I can speedup my unit tests. I am using JUnit
Using Netbeans 6.9.1 my unit tests pass without issue. Using junit (4.8.2, which seems
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
I'm currently re-using JUnit 4 tests from another project against my code. I obtain
We are using JUnit - Selenium for our web tests. We use Maven to
At work we are currently still using JUnit 3 to run our tests. We
We have a lot of integration tests written using JUnit 3 , though we're
I have two tests to check the expected exception throw. I am using Junit
I am using JUnit 4, Maven 2 and latest Eclipse. Problem is simple: I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.