Reputation: 3351
I have a test that send emails: one sends a simple email, the second sends an email with attachment and the third sends and email with tags 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,
"[email protected]",
"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, "[email protected]",
"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, "[email protected]",
"Prueba envio correo simple", "Esto es una prueba");
logger.info("Fin prueba testSendSimpleMail");
}
}
In the case of sending an email with attachment I need to create a file, that's why I need: @before for creating a file and @after to delete it. When I run my test @after and @before are called 3 times. Is it possible to call @before and @after just for the method testSendMail()?
Upvotes: 1
Views: 592
Reputation: 242686
It would be better to use JUnit Rule
s for this task, in particular, the TemporaryFolder
rule:
@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, "[email protected]",
"Prueba envio correo con attachment", "Esto es una prueba",
attachment.getAbsolutePath());
}
Upvotes: 2
Reputation: 26
You can use @BeforeClass and @AfterClass to only run the setup and teardown methods a single time (before ALL the tests are run, and after ALL the tests are run).
As far as selectively running the @Before and @After methods based on specific individual unit tests, I don't think that's possible.
Upvotes: 1