Skip to content

Commit ee3db42

Browse files
varomorfpivovarit
authored andcommitted
Add more examples (eugenp#712)
* Add new module for mocks comparison. * Add sources for testing. * Changes on testCase. * Enter some tests for mockito. * More tests for Mockito. * Even more tests. * Add the rest of the mocking libraries. * Javadoc on test. * Test bare bones for EasyMock. * Fist kind of test and setup. * Add tests using EasyMock with a change on LoginService. * Create LoginControllerTest.java * Test setup * [JMockit] No method called test. * [JMockit] Two methods called test. * [JMockit] One method called test. * [JMockit] Exception mock test * [JMockit] Mocked object to pass around test. * [JMockit] Custom matcher test. * [JMockit] Partial mocking test. * [JMockit] Fix with IDE. * Not stubs. Mocks. MOCKS!!! * Remove unnecesary import. * Use correct encoding. Was having problems with buildings. * Remove failing module. * Create new module mocks and move mock-comparisons there. * Add jmockit module. * Add model class. * Add collaborator class. * Add performer class. * Add performer test. * Fix * Add interface for tests. * Test for any. * Test for with. * Test for null. * Test for times. * Test for arg that. * Test for result and returns. * Test for delegate. * Add verifications to any tests. * Add verifications to with test. * Add verification examples to methods using null. * Add verifications to methods using times. * Formatting. * Compress tests and fix one test. * Adding new article to readme. * [BAEL-178] Add collaborator for advanced article. * [BAEL-178] Add link to readme. * [BAEL-178] Add test for mockUp. * [BAEL-178] Add test for invoke method. * [BAEL-178] Add constructors and tests for mockup for constructors. * [BAEL-178] Add private fields and more test for deencapsulation. * [BAEL-178] Add inner class and test for instantiating inner classes. * [BAEL-178] Multimocks. * [BAEL-178] Add test for expectation reusing. * [BAEL-178] Move test class to tests folders. * Add postgresql dependency. * Add test and config with properties. * [BAEL-114] Add new project for JPA with JNDI. * [BAEL-114] Config without xml. * [BAEL-114] Bring part of Foo, FooServie and FooDao. * [BAEL-114] Show all foos. * [BAEL-114] Readme. * [BAEL-114] Undo changes on main jpa project. * [BAEL-114] Remove unnecesary dependencies. * [BAEL-114] Add tomcat config. * [BAEL-114] Fixes. * Add tests for Optional streams. * Add Java 9 version of the test. * Rename and move to new core-java-9 module. * Move contents from spring-jpa-jndi to spring-jpa and make necessary changes. * Do not use try-catch on configuration. * Add example for FileNotFoundException mini-article. * Add more examples. * Change examples to tests.
1 parent 455c71a commit ee3db42

2 files changed

Lines changed: 64 additions & 20 deletions

File tree

‎core-java/src/main/java/com/baeldung/core/exceptions/FileNotFoundExceptionExample.java‎

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.baeldung.core.exceptions;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
9+
import org.apache.log4j.Logger;
10+
import org.junit.Test;
11+
12+
public class FileNotFoundExceptionTest {
13+
14+
private static final Logger LOG = Logger.getLogger(FileNotFoundExceptionTest.class);
15+
16+
private String fileName = Double.toString(Math.random());
17+
18+
@Test(expected = BusinessException.class)
19+
public void raiseBusinessSpecificException() throws IOException {
20+
try {
21+
readFailingFile();
22+
} catch (FileNotFoundException ex) {
23+
throw new BusinessException("BusinessException: necessary file was not present.", ex);
24+
}
25+
}
26+
27+
@Test
28+
public void createFile() throws IOException {
29+
try {
30+
readFailingFile();
31+
} catch (FileNotFoundException ex) {
32+
try {
33+
new File(fileName).createNewFile();
34+
readFailingFile();
35+
System.out.println("File was created and read.");
36+
} catch (IOException ioe) {
37+
throw new RuntimeException("BusinessException: even creation is not possible.", ioe);
38+
}
39+
}
40+
}
41+
42+
@Test
43+
public void logError() throws IOException {
44+
try {
45+
readFailingFile();
46+
} catch (FileNotFoundException ex) {
47+
LOG.error("Optional file " + fileName + " was not found.", ex);
48+
System.out.println("File was logged.");
49+
}
50+
}
51+
52+
protected void readFailingFile() throws IOException {
53+
BufferedReader rd = null;
54+
rd = new BufferedReader(new FileReader(new File(fileName)));
55+
rd.readLine();
56+
// no need to close file
57+
}
58+
59+
class BusinessException extends RuntimeException {
60+
public BusinessException(String string, FileNotFoundException ex) {
61+
super(string, ex);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)