gpt4 book ai didi

java - @after @before 只用了一次,几次@test

转载 作者:行者123 更新时间:2023-11-29 05:38:40 28 4
gpt4 key购买 nike

我有一个发送电子邮件的测试:一个发送一封简单的电子邮件,第二个发送一封带有附件的电子邮件,第三个发送带有标签 html 的电子邮件:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"file:src/main/webapp/WEB-INF/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring-mail.xml",
"classpath:test-applicationContext.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class MailMailTest extends AbstractTransactionalJUnit4SpringContextTests {

@Autowired
MailMail mailMail;

@Value("${mail.mail}")
private String fromEmail;

@Autowired
private MessageSource messages;

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

@Test
public void testSendHTMLMail() {
logger.info("Ini testSendHTMLMail");

mailMail.sendHTMLMail(
fromEmail,
"blah@gmail.com",
"Prueba envio correo con html",
"Esto es una prueba <br>"
+ this.messages.getMessage(
"email.requestPasswordRecovery.body",null, "Default",null));

logger.info("End testSendHTMLMail");
}

@Before
public void setUp() {
logger.info("** SetUp **");
try {
String fileName = "c:/archivo_prueba.txt";
// File f = new File(fileName);
FileWriter fw;
// FileWriter fichero = new FileWriter("c:/prueba.txt",true);
fw = new FileWriter(fileName, true);

PrintWriter pw = new PrintWriter(fw);
pw.println("esto es una prueba");
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();

}

}

@Test
public void testSendMail() {
logger.info("Inicio prueba testSendMail");

mailMail.sendMail(fromEmail, "blah@gmail.com",
"Prueba envio correo con attachment", "Esto es una prueba",
"c:/archivo_prueba.txt");

logger.info("Fin prueba testSendMail");
}

@After
public void tearDown() {
logger.info("** tearDown **");
File f = new File("c:/archivo_prueba.txt");
if (f.delete())
logger.info("El fichero ha sido borrado satisfactoriamente");
else
logger.error("El fichero no puede ser borrado");
}

@Test
public void testSendSimpleMail() {
logger.info("Inicio prueba testSendSimpleMail");

mailMail.sendSimpleMail(fromEmail, "balh@gmail.com",
"Prueba envio correo simple", "Esto es una prueba");

logger.info("Fin prueba testSendSimpleMail");
}
}

在发送带附件的电子邮件的情况下,我需要创建一个文件,这就是为什么我需要:@before 用于创建文件和@after 用于删除它。当我运行我的测试时,@after 和@before 被调用了 3 次。是否可以只为方法 testSendMail() 调用@before 和@after?

最佳答案

最好使用 JUnit Rule 来完成这项任务,特别是 TemporaryFolder规则:

@Rule
public TemporaryFolder temp = new TemporaryFolder();

@Test
public void testSendMail() {
File attachment = temp.newFile("archivo_prueba.txt");

... // Write test data to the file

mailMail.sendMail(fromEmail, "blah@gmail.com",
"Prueba envio correo con attachment", "Esto es una prueba",
attachment.getAbsolutePath());
}

关于java - @after @before 只用了一次,几次@test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18540146/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com