In this post under Junit, I will show with example the purpose and how to use @TempDir annotation. Lets say you have written class that zip the input file as shown below FileZip package package20; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FileZip { public void zip(File inputFile) throws Exception…… Continue reading Junit @TempDir annotation
Category: JUnit
Unit testing method exceptions
Sometime we have to test whether a method throws proper and required exceptions or not. One way of testing is placing the method call (to be tested) inside a try catch block, as shown below try { class2.method1(); fail(“Should have thrown exception”); } catch(IllegalArgumentException excep) { } catch(Exception excep) { fail(“Should have thrown IllegalArgumentException exception”);…… Continue reading Unit testing method exceptions
Setting timeout for test methods
Junit offers us the option to set timeout for test methods. If a test methods doesnt complete within specified time limit, an exception is thrown failing the test case. The use case scenario where this feature can be used is for example testing the performance of a method. The below code shows you how to…… Continue reading Setting timeout for test methods
Making JUnit ignore test methods
If we want JUnit to ignore certain test methods we can do that by using the @Ignore annotation applied on the methods that has to be ignored. The below test class tests a functionality of a calculator CalculatorTest package Package1; import static org.junit.Assert.assertEquals; import org.junit.Ignore; import org.junit.Test; @Ignore public class CalculatorTest { @Test public void…… Continue reading Making JUnit ignore test methods