1

我有一个发送电子邮件的测试:一个发送一封简单的电子邮件,第二个发送带有附件的电子邮件,第三个发送带有标签 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?

4

2 回答 2

2

最好使用 JUnitRule来完成此任务,特别是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());
}
于 2013-08-30T19:43:30.547 回答
1

您可以使用 @BeforeClass 和 @AfterClass 只运行一次 setup 和 teardown 方法(在所有测试运行之前,在所有测试运行之后)。

至于基于特定的单个单元测试有选择地运行 @Before 和 @After 方法,我认为这是不可能的。

于 2013-08-30T19:29:04.817 回答