Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 我的junit测试不是@Autowiring。我错过了什么_Spring Boot_Junit4 - Fatal编程技术网

Spring boot 我的junit测试不是@Autowiring。我错过了什么

Spring boot 我的junit测试不是@Autowiring。我错过了什么,spring-boot,junit4,Spring Boot,Junit4,我试图在Eclipse中对我的Spring Boot项目进行单元测试。我遇到的问题是我的@Autowire被忽略了 @SpringBootTest public class ValidateRepositoryTest { private static final String CREATE_TBLVALIDATE_SQL_SCRIPT = "scripts/create/validate.sql"; private static final String D

我试图在Eclipse中对我的Spring Boot项目进行单元测试。我遇到的问题是我的@Autowire被忽略了

@SpringBootTest
public class ValidateRepositoryTest {
    private static final String CREATE_TBLVALIDATE_SQL_SCRIPT = "scripts/create/validate.sql";
    private static final String DROP_TBLVALIDATE_SQL_SCRIPT = "scripts/drop/validate.sql";

    private static final Logger logger = Logger.getLogger(ValidateRepositoryTest.class);

    @Autowired
    private JdbcTemplate jdbc;

    @Before
    public void before() throws SQLException {
        if (jdbc == null) {
            logger.fatal("jdbc == null in ValidateRepositoryTest.before()");
            return;
        }
        ScriptUtils.executeSqlScript(jdbc.getDataSource().getConnection(), new ClassPathResource(CREATE_TBLVALIDATE_SQL_SCRIPT));
    }

    @After
    public void after() throws SQLException {
        if (jdbc == null) {
            logger.fatal("jdbc == null in ValidateRepositoryTest.before()");
            return;
        }
        ScriptUtils.executeSqlScript(jdbc.getDataSource().getConnection(), new ClassPathResource(DROP_TBLVALIDATE_SQL_SCRIPT));
    }

    @Autowired
    ValidateRepository validateRepository;

    @Test
    public void testFindByKeyCode() {
        if (jdbc == null) {
            logger.fatal("validateRepository == null in ValidateRepositoryTest.testFindByKeyCode()");
            return;
        }
        String documentTypeKeyCode = Validate.DOCUMENT_TYPE_CLAIMS_APPROVAL;
        String sendMethodKeyCode = Validate.DOCUMENT_SEND_METHOD_EMAIL;
        Validate validate = validateRepository.findByKeyCode(documentTypeKeyCode);
        assertEquals("Shortage Claims Approval POD", validate.getDescription());

    }
}
输出

[INFO] Running com.kable.newsstand.batch.shortage_claim_auto_accept.entities.validate.test.ValidateRepositoryTest
09:40:51.029 [main] ERROR com.kable.newsstand.batch.shortage_claim_auto_accept.entities.validate.test.ValidateRepositoryTest - jdbc == null in ValidateRepositoryTest.before()
09:40:51.036 [main] ERROR com.kable.newsstand.batch.shortage_claim_auto_accept.entities.validate.test.ValidateRepositoryTest - validateRepository == null in ValidateRepositoryTest.testFindByKeyCode()
09:40:51.036 [main] ERROR com.kable.newsstand.batch.shortage_claim_auto_accept.entities.validate.test.ValidateRepositoryTest - jdbc == null in ValidateRepositoryTest.after()
我认为这可能与顶部没有@RunWith(SpringRunner.class)有关,但如果我包含这一点,它会尝试运行我的主应用程序,这会引发异常,因为实例变量不是从环境中填充的:

@SpringBootApplication
public class ShortageClaimAutoAccept implements CommandLineRunner {
    
    private static final Logger logger = Logger.getLogger(ShortageClaimAutoAccept.class);
    
    private static String cognosUser;
    private static String cognosPassword;
    private static String smtpHost;
    private static String ftpServer;
    private static String ftpUserName;
    private static String ftpPassword;
    private static String ftpPath;
    
    private static Mailer mailer;
    private static FtpRelativePathUsage ftp;

    @Autowired
    JdbcTemplate jdbcTemplate;
    
    @Autowired
    ClaimRepository claimRepository;
    
    @Autowired
    ClaimDetailRepository claimDetailRepository;

    @Autowired
    DocumentControlRepository documentControlRepository;

    @Autowired
    DocumentReportRepository documentReportRepository;

    @Autowired
    ValidateRepository validateRepository;

    private void startClaimAutoAcceptApp() {
        if (smtpHost == null) {
            throw new IllegalStateException("smtp host is null");
        }
        if (cognosUser == null) {
            throw new IllegalStateException("cognos user is null");
        }
        if (cognosPassword == null) {
            throw new IllegalStateException("cognos password is null");
        }
        if (ftpServer == null) {
            throw new IllegalStateException("ftp host is null");
        }
        if (ftpUserName == null) {
            throw new IllegalStateException("ftp user is null");
        }
        if (ftpPassword == null) {
            throw new IllegalStateException("ftp password is null");
        }
        if (ftpPath == null) {
            throw new IllegalStateException("ftp server base path is null");
        }

        acceptClaimDetailsAndCloseClaims();
        emailPODClaims();
    }
    
    public static void main(String[] args) {
        try {
            final Properties props = ApplicationPropertiesProvider.getProperties();
            smtpHost = props.getProperty("SYS_SMTP_HOST");
            cognosUser = props.getProperty("REPORT_RUNNER_USER");
            cognosPassword = props.getProperty("REPORT_RUNNER_PWD");
            ftpServer = props.getProperty("FTP_I_CLAIMSPOD_HOST");
            ftpUserName = props.getProperty("FTP_I_CLAIMSPOD_USRID");
            ftpPassword = props.getProperty("FTP_I_CLAIMSPOD_PWD");
            ftpPath = props.getProperty("FTP_I_CLAIMS_BASE_PATH");
            mailer = new Mailer(smtpHost, "");
            FtpSite ftpSite = new FtpSite(ftpServer, ftpUserName, ftpPassword, ftpPath);
            ftp = new FtpRelativePathUsage(ftpSite);

            SpringApplication.run(ShortageClaimAutoAccept.class, args);
        } catch (IllegalStateException ex) {
            logger.fatal(ex);
        } catch (Exception ex) {
            logger.fatal("Uncaught exception in the process: \n", ex);
        }
    }

    @Override
    public void run(String... arg0) throws Exception {
        startClaimAutoAcceptApp();
    }
}

我不知道为什么它会尝试运行此程序,而我所做的只是测试。

这里缺少两件事:

  • 跑步者
  • 用于加载ApplicationContext的组件类。也可以 可以使用@ContextConfiguration(class=…)指定。如果没有 定义了显式类,测试将查找嵌套配置 类,然后返回到SpringBootConfiguration搜索

  • 您的测试包是什么?您定义的数据源配置类是什么?
    @RunWith(SpringRunner.class)
    
    @SpringBootTest(classes = ShortageClaimAutoAccept.class)