
在 intellij idea 的 mybatis 测试中无法调用接口方法
在使用 idea 进行 mybatis 测试时,调用接口方法时出现以下报错:
non-static method 'list()' cannot be referenced from a static context
原因分析
该报错提示表明,你在静态上下文中引用了一个非静态方法。这是因为测试方法是静态的,而被调用方法不是。
public static void testlist() {
usermapper usermapper = new usermapper();
list<user> users = usermapper.list();
}在此示例中,testlist() 方法声明为静态的,但 list() 方法不是。这意味着 list() 方法不能在静态上下文中被调用。
解决方案
要解决此问题,有两种方法:
public void testlist() {
usermapper usermapper = new usermapper();
list<user> users = usermapper.list();
}首先,在测试类中声明一个成员变量:
private usermapper usermapper;
然后,在 setup() 方法中对该变量进行初始化:
@before
public void setup() {
usermapper = new usermapper();
}最后,在测试方法中使用该成员变量:
public static void testList() {
List<User> users = userMapper.list();
}以上就是IntelliJ IDEA中MyBatis测试:为什么静态方法无法调用非静态接口方法?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号