Test for exceptions with ExpectedException

Former-commit-id: 47c6c559196b4c10a5deb3698805ff7276f0aa83
This commit is contained in:
Hans Joachim Desserud
2014-10-12 18:27:03 +02:00
parent 9c7cc2acbf
commit 6481938626

View File

@@ -20,12 +20,15 @@ package org.owasp.dependencycheck.utils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.owasp.dependencycheck.utils.Checksum; import org.owasp.dependencycheck.utils.Checksum;
import org.owasp.dependencycheck.utils.Checksum; import org.owasp.dependencycheck.utils.Checksum;
@@ -51,6 +54,9 @@ public class ChecksumTest {
public void tearDown() throws Exception { public void tearDown() throws Exception {
} }
@Rule
public ExpectedException expectedException = ExpectedException.none();
/** /**
* Test of getChecksum method, of class Checksum. * Test of getChecksum method, of class Checksum.
* *
@@ -83,13 +89,9 @@ public class ChecksumTest {
public void testGetChecksum_FileNotFound() throws Exception { public void testGetChecksum_FileNotFound() throws Exception {
String algorithm = "MD5"; String algorithm = "MD5";
File file = new File("not a valid file"); File file = new File("not a valid file");
boolean exceptionThrown = false;
try { expectedException.expect(IOException.class);
byte[] result = Checksum.getChecksum(algorithm, file); Checksum.getChecksum(algorithm, file);
} catch (IOException ex) {
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);
} }
/** /**
@@ -102,13 +104,9 @@ public class ChecksumTest {
public void testGetChecksum_NoSuchAlgorithm() throws Exception { public void testGetChecksum_NoSuchAlgorithm() throws Exception {
String algorithm = "some unknown algorithm"; String algorithm = "some unknown algorithm";
File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath()); File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath());
boolean exceptionThrown = false;
try { expectedException.expect(NoSuchAlgorithmException.class);
byte[] result = Checksum.getChecksum(algorithm, file); Checksum.getChecksum(algorithm, file);
} catch (NoSuchAlgorithmException ex) {
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);
} }
/** /**