doCallRealMethod 不适用于在抽象类中自动装配的对象

问题描述 投票:0回答:2

我正在尝试为具有超类的子类编写测试基础。我试过下面的方法,但流程没有进入 enrichEvent 方法。

public abstract class StudentProcessor implements IStudentProcessor<Event> {
  @Autowired private StudentService studentService;

    public method processEvent(Event events){
     studentService.enrichEvent(event); -- void return type
     }
    }

子类测试用例

@RunWith(MockitoJUnitRunner.class)
public class ChildStudentProcessorTest {
 @Mock private StudentService studentService;
 @InjectMocks
  private StudentProcessor childStudentEventProcessor = new ChildStudentProcessor();
 

 @Before
 public void setUp() {
  doCallRealMethod().when(studentService).enrichEvent(Mockito.any());
 }

 @Test
 public void test_vanilaScenario() {
  Event baseModel =
      ParserUtil.getObjectFromFile("events/event1.json", Event.class);
  childStudentEventProcessor.processEvent(baseModel);
  }
java mockito junit4 spring-test
2个回答
1
投票

以下行不正确:

@InjectMocks
private StudentProcessor childStudentEventProcessor = new ChildStudentProcessor();

你正在调用

ChildStudentProcessor
的空构造函数。您需要在
StudentProcessor
ChildStudentProcessor
中创建一个以
StudentService
作为参数的新构造函数。然后打电话:

@InjectMocks
private StudentProcessor studentEventProcessor;

这样你在测试类中模拟的

studentService
将作为参数传递到新的构造函数中,并且
doCallRealMethod
将被正确执行。


0
投票

您可以像下面的示例一样使用 Junit5 测试您的课程:

public abstract class StudentProcessor implements IStudentProcessor<Event> {
    // you don't need use autowired, you can use the construsctor for this
  // if you declare  private final StudentService studentService;
 // and build the constructor you can get instance too
    @Autowired
    private StudentService studentService;

    public void processEvent(Event events){
        studentService.enrichEvent(events);
    }
}

你的测试班

@ExtendWith(MockitoExtension.class)
public class ChildStudentProcessorTest {
    @Mock
    private StudentService studentService;
    @InjectMocks
    private StudentProcessor childStudentEventProcessor;


    @BeforeEach
    public void setUp() {
        //here you define how is the implementation for your test
        childStudentEventProcessor = Mockito.mock(ChildStudentProcessor.class);
        //here you define how the studentService should do execute when recive any Event.class
        Mockito.when(studentService).enrichEvent(Mockito.any(Event.class));
    }

    @Test
    public void test_vanilaScenario() {
        //here you define your instance of event
        Event baseModel = ParserUtil.getObjectFromFile("events/event1.json", Event.class);
        //here you do call for your method for test
        childStudentEventProcessor.processEvent(baseModel);
        //here you can test if the studentService is in use one time
        Mockito.verify(studentService,Mockito.times(1)).enrichEvent(baseModel);
    }
}

或者您可以使用如下代码:

@ExtendWith(MockitoExtension.class)
public class ChildStudentProcessorTest {
    @Mock
    private StudentService studentService;
    @InjectMocks
    private StudentProcessor childStudentEventProcessor;



    @Test
    public void test_vanilaScenario_ChildStudentProcessor() {
        //here you define how is the implementation for your test
        childStudentEventProcessor = Mockito.mock(ChildStudentProcessor.class);
        //here you define how the studentService should do execute when recive any Event.class
        Mockito.when(studentService).enrichEvent(Mockito.any(Event.class));

        //here you define your instance of event
        Event baseModel = ParserUtil.getObjectFromFile("events/event1.json", Event.class);
        //here you do call for your method for test
        childStudentEventProcessor.processEvent(baseModel);
        //here you can test if the studentService is in use one time
        Mockito.verify(studentService,Mockito.times(1)).enrichEvent(baseModel);
    }

    @Test
    public void test_vanilaScenario_OtherChildStudentProcessor() {
        //here you define how is the implementation for your test
        childStudentEventProcessor = Mockito.mock(OtherChildStudentProcessor.class);
        //here you define how the studentService should do execute when recive any Event.class
        Mockito.when(studentService).enrichEvent(Mockito.any(Event.class));

        //here you define your instance of event
        Event baseModel = ParserUtil.getObjectFromFile("events/event1.json", Event.class);
        //here you do call for your method for test
        childStudentEventProcessor.processEvent(baseModel);
        //here you can test if the studentService is in use one time
        Mockito.verify(studentService,Mockito.times(1)).enrichEvent(baseModel);
    }
}

如果你想测试异常,你可以像下面的代码那样做:

@ExtendWith(MockitoExtension.class)
public class ChildStudentProcessorTest {
    @Mock
    private StudentService studentService;
    @InjectMocks
    private StudentProcessor childStudentEventProcessor;



    @Test
    public void test_vanilaScenario_ChildStudentProcessor() {
        //here you define how is the implementation for your test
        childStudentEventProcessor = Mockito.mock(ChildStudentProcessor.class);
        //here you define how the studentService should do execute when recive any Event.class
        Mockito.when(studentService).enrichEvent(Mockito.any(Event.class));

        //here you define your instance of event
        Event baseModel = ParserUtil.getObjectFromFile("events/event1.json", Event.class);
        //here you do call for your method for test
        childStudentEventProcessor.processEvent(baseModel);
        //here you can test if the studentService is in use one time
        Mockito.verify(studentService,Mockito.times(1)).enrichEvent(baseModel);
    }

    @Test
    public void test_vanilaScenario_OtherChildStudentProcessor() {
        //here you define how is the implementation for your test
        childStudentEventProcessor = Mockito.mock(OtherChildStudentProcessor.class);
        //here you define how the studentService should do execute when recive any Event.class
        Mockito.when(studentService).enrichEvent(Mockito.any(Event.class));

        //here you define your instance of event
        Event baseModel = ParserUtil.getObjectFromFile("events/event1.json", Event.class);
        //here you do call for your method for test
        childStudentEventProcessor.processEvent(baseModel);
        //here you can test if the studentService is in use one time
        Mockito.verify(studentService,Mockito.times(1)).enrichEvent(baseModel);
    }


    @Test
    public void test_vanilaScenario_ChildStudentProcessor_NullPointerException() {
        //here you define how is the implementation for your test
        childStudentEventProcessor = Mockito.mock(OtherChildStudentProcessor.class);
        //here you define how the studentService should do execute when recive any Event.class and then do
        //throw NullPointerException
        Mockito.doThrow(new NullPointerException()).when(studentService).enrichEvent(Mockito.any(Event.class));

        //here you define your instance of event
        Event baseModel = ParserUtil.getObjectFromFile("events/event1.json", Event.class);
        //here you do call for your method for NullPointerException test
        Assertions.assertThrows(NullPointerException.class,()->childStudentEventProcessor.processEvent(baseModel));
        //here you can test if the studentService is in use one time
        Mockito.verify(studentService,Mockito.times(1)).enrichEvent(baseModel);
    }

}

使用无效方法进行更多测试无效测试 使用异常方法进行更多测试exceptions test

© www.soinside.com 2019 - 2024. All rights reserved.