简体   繁体   English

如何在要测试的类中模拟Abstract类方法

[英]How to mock Abstract class method in a class to be tested

There is an abstract class 有一个abstract

public abstract class BaseProcessor {
 public BooksTransaction getBooksTransaction() {
        return booksTransaction;
    }
}

There is another final class which is to be tested using Junit 还有另一个final class要使用Junit进行测试

public final class CreateOrganisationProcessor extends BaseProcessor {

public boolean process() throws Exception { //method to be tested
        request = new CreateOrganisationRequest(IntegrationSystems.valueOf(getBooksTransaction().getSource()),
                IntegrationSystems.valueOf(getBooksTransaction().getDestination()), getBooksTransaction()); 
        request.setRequestTypes(getRequestTypes());
return true;
}
}

I tried spying the BaseProcessor class and mocking getBooksTransaction method to return BooksTransaction Object. 我试图刺探BaseProcessor类和嘲讽 getBooksTransaction方法来return BooksTransaction对象。 Code: 码:

@Test
   public void testProcess() throws Exception {
BaseProcessor spy = Mockito.spy(new CreateOrganisationProcessor());
       BooksTransaction booksTransaction = new BooksTransaction();
       booksTransaction.setReferenceID(DEFAULT_REFERENCE_ID);
       Mockito.doReturn(booksTransaction).when(spy).getBooksTransaction();
}

Here, BooksTransaction is an JPA Entity class. 在这里, BooksTransaction是一个JPA Entity类。

However, when I run the test case, the mock does not seem to be working, it does not return a BooksTransaction Object. 但是,当我运行测试用例时,该模拟似乎无法正常工作,它不会返回BooksTransaction对象。 It neither throws an exception , nor any error . 它既不会引发exception ,也不会引发任何error

I would like to know the right way to spy this method so that it returns me an object of BooksTransaction as per my mock . 我想知道spy此方法的正确方法,以便根据我的mock返回给我一个BooksTransaction对象。

I am new to Mockito , any help would be appreciable. 我是Mockito ,任何帮助都是不小的。 Thanks in advance. 提前致谢。

It's funny that you got 5 up-votes for a question that does not even compile to begin with... I have simplified it just a bit, so that I could actually compile it, since I do not know your structure or can't even guess it correctly. 有趣的是,您对一个甚至没有开始编译的问题进行了5次投票...我将其简化了一点,以便我可以实际编译它,因为我不知道您的结构或无法甚至猜对了。

But the very first point you should be aware of is that Mockito can't by default mock final classes; 但是,您应该意识到的第一点是, Mockito 默认情况下无法模拟final类。 you have a comment under your question that shows how to enable that. 您的问题下有一条评论,显示了如何启用它。

@Getter
static abstract class BaseProcessor {
    private BooksTransaction BooksTransaction;
}

@Getter
static class CreateOrganisationProcessor extends BaseProcessor {

    CreateOrganisationRequest request;

    public boolean process() { //method to be tested
        request = new CreateOrganisationRequest(getBooksTransaction());
        return true;
    }

    public CreateOrganisationRequest getRequest() {
        return request;
    }
}


@RequiredArgsConstructor
@Getter
static class BooksTransaction {
    private final String testMe;
}

@Getter
@RequiredArgsConstructor
static class CreateOrganisationRequest {
    private final BooksTransaction booksTransaction;
}

And here is a test that does work: 这里是一个测试, 工作:

@Test
public void test() {
    CreateOrganisationProcessor org = new CreateOrganisationProcessor();
    CreateOrganisationProcessor spy = Mockito.spy(org);
    System.out.println(spy);
    BooksTransaction booksTransaction = new BooksTransaction("go!");

    Mockito.doReturn(booksTransaction).when(spy).getBooksTransaction();

    spy.process();
    BooksTransaction mocked = spy.getRequest().getBooksTransaction();
    Assert.assertEquals(mocked.getTestMe(), "go!");
}

And now think about it, you say in a comment : //method to be tested but you are not even calling it in your test, sounds fishy doesn't it? 现在考虑一下,您在注释中说: //method to be tested但您甚至没有在测试中调用它,听起来可疑吗? Than that method is defined in CreateOrganisationProcessor , but you are assigning your spy to: 虽然在CreateOrganisationProcessor定义了该方法,但是您将间谍分配给:

 BaseProcessor spy = Mockito.spy(new CreateOrganisationProcessor());

So now you can't even call process anymore on that spy reference, since it is not defined in BaseProcessor . 因此,现在您甚至无法在该spy引用上调用process ,因为它没有BaseProcessor定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM