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

测试类中如何注入一个List的Field? #36

Open
lewe518 opened this issue Apr 27, 2020 · 1 comment
Open

测试类中如何注入一个List的Field? #36

lewe518 opened this issue Apr 27, 2020 · 1 comment

Comments

@lewe518
Copy link

lewe518 commented Apr 27, 2020

代码:

@Component
public class CandyHelper{
    @Autowired
    private List<CandyFilter> candyFilterList;
}

测试类

public class CandyHelperTest {
    @Tested
    private CandyHelper testInstance;
    @Injectable
    private List<CandyFilter> candyFilterList;
}

问题

使用Injectable时,会提示java.lang.IllegalArgumentException: java.util.List is not mockable;
不使用Injectable时会提示java.lang.IllegalStateException: Missing @Injectable for field "java.util.List candyFilterList" in CandyHelper

请问这种情况应该如何mock?

@guhanjie
Copy link

guhanjie commented Nov 30, 2021

这种情况可以用@tested进行修饰,并给该List字段进行初始化,然后就可以自动注入了。
代码例如:

public class CandyHelperTest {
    @Tested
    private CandyHelper testInstance;
    @Tested
    private List<CandyFilter> candyFilterList = new ArrayList<>();
}

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

2 participants