I am trying to unit test my Spring Boot project in Eclipse. The problem I am having is that my @Autowire s are being ignored.
@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());
}
}
The output.
[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()
I think it might have something to do with not having @RunWith(SpringRunner.class) at the top, but if I include this, it tries to run my main application, which throws exceptions because instance variables aren't populated from the environment:
@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();
}
}
I'm not sure why it tries to run this, when all I am doing is testing.
You are missing two things here:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShortageClaimAutoAccept.class)