spring when(..)stub of service method does not work in Mockito Unit Test(服务方法的存根在Mockito单元测试中不起作用)

sdnqo3pr  于 2023-03-28  发布在  Spring
关注(0)|答案(1)|浏览(107)

为什么下面的服务方法的when存根不起作用?我看到当我运行测试时,调用的是真实的ar11applicationService的processEligibleRecords,而不是我创建的存根。我正在测试一个应用程序,其run将调用Service方法。anyString/anyInt param选项用于创建存根。

@SpringBootTest
@RunWith(MockitoJUnitRunner.class) 
@ExtendWith(MockitoExtension.class)
class Ar11ApplicationTest {

    @Mock
    Ar11ApplicationService ar11ApplicationService;
    
    @Mock
    EmailService emailService;
    
    @InjectMocks   // inject into the object we're testing
    Ar11Application ar11Application;
    
    @BeforeEach
    void setUp() throws Exception {
        
        // This is the stub that should get called - 9 params, next-to-last is Int, all others String
        Mockito.when(ar11ApplicationService.processEligibleRecords(Mockito.anyString(), Mockito.anyString(), 
                                                                   Mockito.anyString(), Mockito.anyString(), 
                                                                   Mockito.anyString(), Mockito.anyString(), 
                                                                   Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()))
              .thenReturn(new Ar11ResultBean());
    }

    @Test
    void main() throws Exception {
        
        org.junit.jupiter.api.Assertions.assertDoesNotThrow(
                  ()-> 
                  {
                      ar11Application.run(new String[] {});
                  }
                );
    }

}

服务实现类的方法(Ar11ApplicationServiceImpl implements Ar11ApplicationService):

@Component
public class Ar11ApplicationServiceImpl implements Ar11ApplicationService {

@Override
@Transactional(readOnly = false, rollbackFor = Exception.class)
public Ar11ResultBean processEligibleRecords(String eligibleFilename, String newFilename, 
                                             String sourceDir, String sftpUserName, 
                                             String sftpPrivateKeyPath, String sftpPrivateKeyPassphrase, 
                                              String sftpHost, Integer sftpPort, String remoteDir) 
 throws Exception {

我正在测试的应用程序类是

@SpringBootApplication
public class Ar11Application implements CommandLineRunner {
    

// General properties
@Value("${AR11_ELIGIBLE_FILENAME}")
private String eligibleFilename;    
@Value("${AR11_NEW_FILENAME_PREFIX}")
private String newFilenamePrefix;

// SFTP properties  
@Value("${AR11_SFTP_USERNAME}")
private String sftpUserName;    
@Value("${AR11_SFTP_KEY_PATH}")
private String sftpPrivateKeyPath;  
@Value("${AR11_SFTP_PASSPHRASE}")
private String sftpPrivateKeyPassphrase;
@Value("${AR11_SFTP_HOST}")
private String sftpHost;    
@Value("${AR11_SFTP_PORT}")
private Integer sftpPort;
@Value("${AR11_SFTP_SOURCE_DIR}")
private String sourceDir;
@Value("${AR11_SFTP_TARGET_DIR}")
private String remoteDir;   

    @Autowired
    Ar11ApplicationService ar11ApplicationService;

@Override
public void run(String... args) throws Exception {
    
try {
    Ar11ResultBean ar11ResultBean = ar11ApplicationService.processEligibleRecords(eligibleFilename, newFilename, sourceDir,  
                                                                                  sftpUserName, sftpPrivateKeyPath, sftpPrivateKeyPassphrase, sftpHost, sftpPort, remoteDir);
dy1byipe

dy1byipe1#

@tgdavies的答案是删除顶部的@SpringBootTest。应该是这样的:

@RunWith(MockitoJUnitRunner.class) // No @SpringBootTest!
@ExtendWith(MockitoExtension.class)
class Ar11ApplicationTest {

    @Mock
    Ar11ApplicationService ar11ApplicationService;
    
    @Mock
    EmailService emailService;
    
    @InjectMocks
    Ar11Application ar11Application;
    
    @BeforeEach
    void setUp() throws Exception {
        
        Mockito.when(ar11ApplicationService.processEligibleRecords(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()))
             .thenReturn(new Ar11ResultBean(500));

我在调试器中验证了,只要添加了@SpringBootTest,它就会立即转到真实的的Service类的代码,而无需在此单元测试中执行setUp

相关问题