【问题标题】:My junit test is not @Autowiring. What am I missing我的junit测试不是@Autowireing。我错过了什么
【发布时间】:2020-10-24 07:52:12
【问题描述】:

我正在尝试在 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();
    }
}

当我所做的只是测试时,我不确定它为什么会尝试运行它。

【问题讨论】:

  • 你的测试包是什么,你定义数据源的配置类是什么?

标签: spring-boot junit4


【解决方案1】:

你在这里遗漏了两件事:

  1. 跑步者
@RunWith(SpringRunner.class)
  1. 用于加载 ApplicationContext 的组件类。也能 使用 @ContextConfiguration(classes=...) 指定。如果不 定义了显式类,测试将查找嵌套配置 类,然后回退到 SpringBootConfiguration 搜索。
@SpringBootTest(classes = ShortageClaimAutoAccept.class)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多