Adam
Adam

Reputation: 161

How to mock filter with repository in constructor?

I created filter which saving abnormal requests and this filter have repository in constructor:

@Component
public class LogRequestsFilter extends OncePerRequestFilter {

    private final LogRepository logRepository;

    @Autowired
    public LogRequestsFilter(LogRepository logRepository) {
        this.logRepository = logRepository;
    }

Im trying creating a test like I did earlier in my controller class, but I cant add filter:

    @SpyBean
    private LogRepository logRepository;


    @Before
    public void setup(){
        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .addFilters(new LogRequestsFilter(this.logRepository), "/*")
                .build();
    }

this gives me error:

Inferred type 'T' for type parameter 'T' is not within its bound; 
should extend 'org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder

Upvotes: 1

Views: 296

Answers (1)

Adam
Adam

Reputation: 161

Thankfully for @M.DEinum and @JB Nizet I created working solution:

    @Autowired
    private LogRepository logRepository;


    @Before
    public void setup(){
        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
LogRequestsFilter(this.logRepository), "/*")
                .build();
    }

Upvotes: 1

Related Questions