GithubHelp home page GithubHelp logo

Comments (9)

linfan avatar linfan commented on May 2, 2024 1

单独示例还没有写,但是我在内部实际项目里找到了有这种用法的地方,确定是可行的。

先再确认一下你是指Service调用Dao的测试,还是Dao调用Mapper的测试。如果Service调用Dao的话肯定可行,因为Dao就是普通类,这个代码我就不贴了。Dao调用Mapper稍微特殊一点,因为Mapper实现类是Mybatis动态生成的。实际这两种情况都适用。我贴一个Dao调用Mapper的例子,只包含相关部分代码(类型名脱敏用Xxx代替):

被测类

public class ReqXxxDAO extends BaseDAO {
    private XxxReqMapper reqMapper;

    public String getNum() {
        ... ... // 省略无关代码
        return reqMapper.getXxxNum(param);
    }
}

测试类

public class ReqXxxDAOTest {

    private ReqXxxDAO dao = new ReqXxxDAO();

    @MockMethod
    Integer getXxxNum(XxxReqMapper r, QueryParam param) {
        return ...;  // 此处有一些基于param的判断逻辑,省略
    }

    @Test
    public void testGetNum() {
        assertEquals(1, dao.getNum());
    }

}

你可以提供一些详细的信息,遇到的错误是什么,相关代码是怎样的。

from testable-mock.

loululin avatar loululin commented on May 2, 2024

是否可留个 mock mybatis plus dao 的demo

from testable-mock.

linfan avatar linfan commented on May 2, 2024

错误信息和被测方法的大致情况能贴一下吗?

from testable-mock.

loululin avatar loululin commented on May 2, 2024

kotlin demo中 没有看到接口方法 mock的例子,,

from testable-mock.

linfan avatar linfan commented on May 2, 2024

我抽空补一个Case做个验证,我理解你的问题是Mock在Service对象里对Dao接口方法的调用报错了对吧?

from testable-mock.

loululin avatar loululin commented on May 2, 2024

对的

from testable-mock.

loululin avatar loululin commented on May 2, 2024

您好,mock mybatis plus 接口的demo有了么,想参考一下

from testable-mock.

loululin avatar loululin commented on May 2, 2024

谢谢您的回复先,dao是接口(kotlin),类似:

/**
 * <p>
 * 钱包交易流水记录表 Mapper 接口
 * </p>
 *
 * @author loulvlin
 * @since 2020-12-23
 */
interface WalletTradeDetailMapper : BaseMapper<WalletTradeDetail>

想问下,可以mock BaseMapper中的等方法么,如 'T selectById(Serializable id);' 这种?

from testable-mock.

linfan avatar linfan commented on May 2, 2024

可以的,唯一需要注意的是如果在DAO对象中定义的Mapper是子类型的(如WalletTradeDetailMapper),即使调用继承自父类型(如BaseMapper)的方法,在测试类定义Mock方法时,首位的参数依然应该使用子类类型。在文档的常见问题中也有提到。

from testable-mock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.