Java 在@BeforeStep之前初始化测试中的模拟

Java 在@BeforeStep之前初始化测试中的模拟,java,mockito,spring-batch,spring-boot-test,Java,Mockito,Spring Batch,Spring Boot Test,我有一个带有@BeforeStep函数的自定义读取器,用于初始化一些数据。这些数据来自外部数据库 @组件 公共类CustomReader实现ItemReader{ 私人餐饮服务; 私人的某物对某物; @先于 私有void初始化(){ someDTO=restApiService.getData(); } @凌驾 公共部分阅读(){ ... 归还某物 } } 在我的单元测试中,我需要模拟对外部数据库的调用 @RunWith(SpringRunner.class) @SpringBootT

我有一个带有@BeforeStep函数的自定义读取器,用于初始化一些数据。这些数据来自外部数据库

@组件
公共类CustomReader实现ItemReader{
私人餐饮服务;
私人的某物对某物;
@先于
私有void初始化(){
someDTO=restApiService.getData();
}
@凌驾
公共部分阅读(){
...    
归还某物
}
}
在我的单元测试中,我需要模拟对外部数据库的调用

@RunWith(SpringRunner.class)
@SpringBootTest(classes=NedBatchApplication.class)
公共类CustomReaderTest{
@自动连线
客户阅读器客户阅读器;
@嘲弄
重启服务重启服务;
@以前
私有无效设置(){
initMocks(this);
ReflectionTestUtils.setField(customReader,“restApiService”,restApiService);
Mockito.when(restApiService.getData()),然后返回(expectedData);
}
}
我面临的问题是,当我启动测试时,@BeforeStep是在单元测试的@before之前执行的。因此restApiService.getData()返回null而不是expectedData


是否有一种方法可以实现我想要的或我需要用不同的方法来实现它?

您确定
BeforeStep
before
注释之前运行
吗(通过使用日志记录或类似方法?)

您的Mockito调用可能不完全正确。请尝试改用
Mockito.doReturn(expectedData).when(restApiService).getData()


作为一种替代方法,如果
RestApiService
是在自定义读取器中自动连接的,那么您可以在测试中使用自定义读取器声明上的
@InjectMocks
注释,这将导致在测试期间将
RestApiService
的模拟版本注入到类中。

您确定吗因为
BeforeStep
before
注释之前运行(通过使用日志记录或类似方法?)

您的Mockito调用可能不完全正确。请尝试改用
Mockito.doReturn(expectedData).when(restApiService).getData()


作为一种替代方法,如果
RestApiService
是在自定义读取器中自动连接的,则可以在测试中使用自定义读取器声明上的
@InjectMocks
注释,这将导致在测试期间将
RestApiService
的模拟版本注入类中。

通常在使用基于Spring的测试,尝试将依赖项(如
restApiService
(您想要模拟的依赖项)变成Springbean,然后您可以指示Spring在应用程序上下文创建过程中通过
@MockBean
注释创建模拟并注入到应用程序上下文中:

import org.springframework.boot.test.mock.mockito.MockBean;  
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NedBatchApplication.class)
public class CustomReaderTest {

    @MockBean
    private RestApiService restApiService; 
}

通常,在使用基于Spring的测试时,尝试将依赖项(如
restApiService
(您想要模拟的依赖项)设置为SpringBean,然后您可以指示Spring在应用程序上下文创建过程中通过
@MockBean
注释创建模拟并注入应用程序上下文:

import org.springframework.boot.test.mock.mockito.MockBean;  
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NedBatchApplication.class)
public class CustomReaderTest {

    @MockBean
    private RestApiService restApiService; 
}

在与一位同事反思之后,他给了我一个解决方案:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = NedBatchApplication.class)
public class CustomReaderTest {

  CustomReader customReader;

  @Mock
  RestApiService restApiService;

  @Before
  private void setup() {
      MockitoAnnotations.initMocks(this);
      Mockito.when(restApiService.getData().thenReturn(expectedData);

      this.customReader = new CustomReader(restApiService);
  }

 @Test
 public void test() {
   customReader.initialize();
   (...)
 }
}

在与一位同事反思之后,他给了我一个解决方案:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = NedBatchApplication.class)
public class CustomReaderTest {

  CustomReader customReader;

  @Mock
  RestApiService restApiService;

  @Before
  private void setup() {
      MockitoAnnotations.initMocks(this);
      Mockito.when(restApiService.getData().thenReturn(expectedData);

      this.customReader = new CustomReader(restApiService);
  }

 @Test
 public void test() {
   customReader.initialize();
   (...)
 }
}

是的,通过在调试模式下使用断点,我确信
BeforeStep
BeforeStep
注释之前运行了
。因此,使用
@Autowired
注释很可能会导致在运行测试的
BeforeStep
之前对自定义阅读器进行完全初始化。您是否尝试过将代码更改为在
RestAPIService
是自动连线的,允许您在测试中使用
@InjectMocks
@MockBean
注释来模拟它?我将尝试一下,并让您不断更新结果。感谢您的关注。我尝试了另一种方法。我使用了initialize()public,在@Before注释的方法中,我创建了一个新的CustomReader,比如CustomReader=newcustomreader(restApiService)是的,我确信
BeforeStep
是在调试模式下使用断点在
BeforeStep
注释之前运行的。因此,使用
@Autowired
注释很可能会导致在运行测试的
BeforeStep
之前对自定义阅读器进行完全初始化。您是否尝试过更改y我们的代码使
RestAPIService
自动连接,允许您在测试中使用
@InjectMocks
@MockBean
注释对其进行模拟?我将尝试一下,并让您不断更新结果。感谢您的关注。我尝试了不同的方法。我制作了initialize()public,在@Before注释的方法中,我创建了一个新的CustomReader,比如CustomReader=newcustomreader(restApiService)restApiService是用@Service注释的。在@MockBean中更改@Mock不会改变结果。问题是
BeforeStep
before
注释之前执行,witch使
restApiService.getData()返回null,我的读取器在
read()中也返回null
method.restApiService用@Service注释。在@MockBean中更改@Mock不会改变结果。问题是
BeforeStep
before
注释之前执行,witch使
restApiService.getData()返回null,而我的读取器在
read()中也返回null
方法。