Why the class TestTypeExcludeFilter has not implemented hashCode? I have a problem with that, How can I exclude the TestTypeExcludeFilter from my test cases?

TypeExcludeFilter class org.springframework.boot.test.context.filter.TestTypeExcludeFilter has not implemented hashCode

best wishes!

Comment From: wilkinsona

I'm not sure that I understand the problem you're seeing. Can you please expand a bit on the problem that you're seeing and what it is that isn't working as you expect. A complete yet minimal sample that reproduces the problem would be the ideal way to do that. You can share such a sample with us by pushing it to a separate repository on GitHub or zipping it up and attaching it to this issue.

Comment From: alonelyleaf

The method hashCode in TypeExcludeFilter need be implemented, but org.springframework.boot.test.context.filter.TestTypeExcludeFilter didn't do that. When I use its hashCode method, it throws a error.

      // the method in TypeExcludeFilter
        @Override
    public int hashCode() {
        throw new IllegalStateException("TypeExcludeFilter " + getClass() + " has not implemented hashCode");
    }
// my test code, When I run my test cases, it throw a Exception.
@Configuration
public class MyConfiguration {

    @Resource
    private Map<String, Object> consumerContainer;

    @Bean
    public MyAspect myAspect() {
        return new MyAspect(this.consumerContainer);
    }
}

@Aspect
public class MyAspect implements ApplicationContextAware {

    private Map<String, Object> container;
    private ApplicationContext applicationContext;

    public MyAspect(Map<String, Object> container) {
        this.container = container;
        System.out.println(toString());
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public Map<String, Object> getContainer() {
        return container;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public String toString() {
        return "MyAspect(container=" + this.getContainer() + ", applicationContext=" + this.getApplicationContext() + ")";
    }
}

Comment From: wilkinsona

Thanks for the additional information. Unfortunately, I can't see the connection between what you've shared and TestTypeExcludeFilter. Before we make any changes, I'd like to fully understand the problem. To that end, can you please provide the complete and minimal sample that I requested above.

Comment From: alonelyleaf

I am sorry to provide the code now. you see my test code. Run the test class com.example.demo.DemoApplicationTests, And you can see the exception.

https://github.com/alonelyleaf/demo-TypeExcludeFilter

Comment From: wilkinsona

Thanks for the sample. I've reproduced the failure. Your code is quite unusual, in particular injecting a Map<String, Object> of every bean in the context is something that I'm not sure I've seen before, particularly when also injecting ApplicationContext as well. Due to the minimal nature of the sample (thank you for keeping it nice and small) I can't tell what is the purpose of the map of beans, but I'd encourage you to take a different approach if at all possible.

With all that said, now that I understand the nature of the problem, I think we should implement hashCode() (and equals(Object)) on TestTypeExcludeFilter.