我试图在Eclipse中对我的Spring Boot项目进行单元测试。我遇到的问题是,我的@Autowire s被忽略了。
@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();
}
}
我不确定它为什么要运行这个,而我所做的只是测试。
你在这里遗漏了两件事:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShortageClaimAutoAccept.class)
问题内容: 正确,我的junit测试看起来像是一个漫长的故事: 我创建了4位用户 我删除了1位用户 我尝试使用已删除的用户登录,并确保失败 我使用剩余的3个用户之一登录并确认我可以登录 我从一个用户向另一个用户发送一条消息,并验证该消息是否出现在发件人的发件箱和收件人的收件箱中。 我删除邮件 … … 优点 :测试非常有效(非常善于检测错误)并且非常稳定,因为它们仅使用API,如果我重构代码,那
大家好,我需要为我的方法编写单元测试。我有点麻烦,因为我是JUnit的新手。我需要为这个方法写一个测试。这是我的方法 我试过了,但我被挡了,这是我的结果 附言:测试通过了,但我不明白我的测试是不是真的
这是我的pom文件,我已经升级到springboot2.5.12,我需要更新或添加任何其他依赖项吗? 我的Junit测试用例抛出此错误 原因:java。lang.ClassNotFoundException:Reactor。util。上下文java的ContextView。基本/jdk。内部的装载机。内置ClassLoader。java上的loadClass(BuiltinClassLoader.
我应该在中具体放入什么? 我找不到这方面的指导。
我试图测试一个控制器的方法,并得到适当的异常。它仅对ConstraintViolationExcture(javax.validation)失败,而对其他测试(如MisSingServletRequest estParameterExcture或METHOArgumentTypeMismatchExcture)则按预期工作。 下面是我的代码的一部分,在控制器(@验证)中,在我添加这些注释的方法中。
我正在使用Angular2 final(2.0.2)和angular cli。我正在尝试将其设置为使用PhantomJS运行单元测试。使用Chrome和karma Chrome launcher运行规范-所有测试都通过。在Phantomjs预构建2.1中运行相同的功能。13和karma phantomjs launcher 1.0。2次测试失败。 我添加了phantomjs启动器到插件数组中kar