@after @before仅使用一次,几次@test

时间:2013-08-30 19:25:57

标签: java junit annotations

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

@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?

2 个答案:

答案 0 :(得分:2)

最好将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());
}

答案 1 :(得分:1)

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

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