From ee3db428800813821a5c42a2279018d40c906f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Gonz=C3=A1lez?= Date: Tue, 4 Oct 2016 07:53:52 +0200 Subject: [PATCH] Add more examples (#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. --- .../FileNotFoundExceptionExample.java | 20 ------ .../exceptions/FileNotFoundExceptionTest.java | 64 +++++++++++++++++++ 2 files changed, 64 insertions(+), 20 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/core/exceptions/FileNotFoundExceptionExample.java create mode 100644 core-java/src/test/java/org/baeldung/core/exceptions/FileNotFoundExceptionTest.java diff --git a/core-java/src/main/java/com/baeldung/core/exceptions/FileNotFoundExceptionExample.java b/core-java/src/main/java/com/baeldung/core/exceptions/FileNotFoundExceptionExample.java deleted file mode 100644 index 2ac4e49869..0000000000 --- a/core-java/src/main/java/com/baeldung/core/exceptions/FileNotFoundExceptionExample.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.core.exceptions; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; - -public class FileNotFoundExceptionExample { - - public static void main(String[] args) throws IOException { - BufferedReader rd = null; - try { - rd = new BufferedReader(new FileReader(new File("notExisting"))); - rd.readLine(); - } catch (FileNotFoundException ex) { - ex.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/core-java/src/test/java/org/baeldung/core/exceptions/FileNotFoundExceptionTest.java b/core-java/src/test/java/org/baeldung/core/exceptions/FileNotFoundExceptionTest.java new file mode 100644 index 0000000000..9180edfe81 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/core/exceptions/FileNotFoundExceptionTest.java @@ -0,0 +1,64 @@ +package org.baeldung.core.exceptions; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +import org.apache.log4j.Logger; +import org.junit.Test; + +public class FileNotFoundExceptionTest { + + private static final Logger LOG = Logger.getLogger(FileNotFoundExceptionTest.class); + + private String fileName = Double.toString(Math.random()); + + @Test(expected = BusinessException.class) + public void raiseBusinessSpecificException() throws IOException { + try { + readFailingFile(); + } catch (FileNotFoundException ex) { + throw new BusinessException("BusinessException: necessary file was not present.", ex); + } + } + + @Test + public void createFile() throws IOException { + try { + readFailingFile(); + } catch (FileNotFoundException ex) { + try { + new File(fileName).createNewFile(); + readFailingFile(); + System.out.println("File was created and read."); + } catch (IOException ioe) { + throw new RuntimeException("BusinessException: even creation is not possible.", ioe); + } + } + } + + @Test + public void logError() throws IOException { + try { + readFailingFile(); + } catch (FileNotFoundException ex) { + LOG.error("Optional file " + fileName + " was not found.", ex); + System.out.println("File was logged."); + } + } + + protected void readFailingFile() throws IOException { + BufferedReader rd = null; + rd = new BufferedReader(new FileReader(new File(fileName))); + rd.readLine(); + // no need to close file + } + + class BusinessException extends RuntimeException { + public BusinessException(String string, FileNotFoundException ex) { + super(string, ex); + } + } +}