From 96e6981d39cda7a70ae895cd28aca8e9ae7d7fba Mon Sep 17 00:00:00 2001 From: Sumanth Varma Dommaraju Date: Tue, 5 Jan 2016 20:02:07 -0500 Subject: [PATCH 1/5] reflection util --- .../org/facil/practice/ReflectionUtils.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/test/java/org/facil/practice/ReflectionUtils.java diff --git a/src/test/java/org/facil/practice/ReflectionUtils.java b/src/test/java/org/facil/practice/ReflectionUtils.java new file mode 100644 index 0000000..6647f62 --- /dev/null +++ b/src/test/java/org/facil/practice/ReflectionUtils.java @@ -0,0 +1,23 @@ +package org.facil.practice; + +import java.lang.reflect.Field; + +/** + * Created by sumanthdommaraju on 1/1/16. + */ +public class ReflectionUtils { + + public static void setNonAccesibleField(Class clazz, T obj, String fieldName, U value) throws IllegalAccessException, NoSuchFieldException { + if(clazz == null){ + throw new NoSuchFieldException(); + } + try{ + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + field.set(obj, value); + return; + }catch (NoSuchFieldException nsfe){ + setNonAccesibleField(clazz.getSuperclass(), obj, fieldName, value); + } + } +} From 624ec5cad9771d43c80cfe9d0b924501f3593e53 Mon Sep 17 00:00:00 2001 From: Sumanth Varma Dommaraju Date: Tue, 5 Jan 2016 20:06:14 -0500 Subject: [PATCH 2/5] Wrote these tests assuming that the implementation of methods under test fulfills the requirement. --- .../AbstractLambdaFileHandlerTest.java | 143 +++++++++++++++++- .../practice/AbstractLambdaHandlerTest.java | 117 +++++++++++++- .../practice/DefaultFileHandlerTest.java | 97 +++++++++++- .../practice/LambdaFunctionPublishTest.java | 130 +++++++++++++++- 4 files changed, 472 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java b/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java index 16104d4..e92111d 100644 --- a/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java +++ b/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java @@ -1,6 +1,19 @@ package org.facil.practice; +import com.amazonaws.services.lambda.AWSLambda; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + +import static org.facil.practice.ReflectionUtils.setNonAccesibleField; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; /** * User: blangel @@ -9,9 +22,135 @@ */ public class AbstractLambdaFileHandlerTest { + private AbstractLambdaFileHandler abstractLambdaFileHandler; + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + private String fileName = "test.txt"; + private File file; + private AWSLambda awsLambda; + private String functionName = "getSomething"; + + public AbstractLambdaFileHandlerTest() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { + abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); + awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + + setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "fileName", fileName); + setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "awsLambda", awsLambda); + setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "functionName", functionName); + + } + @Test - public void createFileIfNotExists() { - // TODO + public void createFileIfNotExists() throws IOException, NoSuchFieldException, IllegalAccessException { + file = temporaryFolder.newFile(fileName); + if(file.exists() && !file.delete()){ + fail(); + } + setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); } + @Test + public void createFileIfNotExistsVerifyMethodCalls() throws IOException, NoSuchFieldException, IllegalAccessException { + final File helperFile = temporaryFolder.newFile(fileName); + file = mock(File.class, new AnswerForFile(helperFile)); + if(helperFile.exists() && !helperFile.delete()){ + fail(); + } + setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); + verify(file,times(1)).exists(); + verify(file,times(1)).createNewFile(); + } + + @Test + public void dontCreateFileIfFileExists() throws IOException, NoSuchFieldException, IllegalAccessException { + final File helperFile = temporaryFolder.newFile(fileName); + file = mock(File.class, new AnswerForFile(helperFile)); + setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); + verify(file,times(1)).exists(); + verify(file,never()).createNewFile(); + } + + + @Test + public void createFileIfNotExistsCatchAndReturnFalse() throws NoSuchFieldException, IllegalAccessException { + Answer answerIOExceptionForFileOperations = new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("exists")){ + return Boolean.FALSE; + } + if(true) { + throw new IOException(); + } + return null; + } + }; + file = mock(File.class, answerIOExceptionForFileOperations); + setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + assertFalse(abstractLambdaFileHandler.createFileIfNotExists()); + } + + @Test(expected = IOException.class) + public void createFileIfNotExistsException() throws NoSuchFieldException, IllegalAccessException { + Answer answerIOExceptionForAllFileOperations = new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(true) { + throw new IOException(); + } + return null; + } + }; + file = mock(File.class, answerIOExceptionForAllFileOperations); + setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + abstractLambdaFileHandler.createFileIfNotExists(); + } + + @Test + public void dontCreateFileIfFileOperationsReturnFalse() throws NoSuchFieldException, IllegalAccessException { + Answer answerIOExceptionForFileOperations = new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("createNewFile")){ + return Boolean.FALSE; + } + if(invocation.getMethod().getName().equals("exists")){ + return Boolean.FALSE; + } + if(true) { + throw new IOException(); + } + return null; + } + }; + file = mock(File.class, answerIOExceptionForFileOperations); + setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + assertFalse(abstractLambdaFileHandler.createFileIfNotExists()); + } + + + private class AnswerForFile implements Answer { + + private File helperFile; + public AnswerForFile(File helperFile){ + this.helperFile = helperFile; + } + + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("createNewFile")){ + return helperFile.createNewFile(); + } + if(invocation.getMethod().getName().equals("exists")){ + return helperFile.exists(); + } + return null; + } + } + + } diff --git a/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java b/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java index 24dcb65..c21c690 100644 --- a/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java +++ b/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java @@ -1,7 +1,21 @@ package org.facil.practice; +import com.amazonaws.AmazonClientException; +import com.amazonaws.services.lambda.AWSLambda; +import com.amazonaws.services.lambda.model.GetAliasRequest; +import com.amazonaws.services.lambda.model.GetAliasResult; +import com.amazonaws.services.lambda.model.GetFunctionRequest; +import com.amazonaws.services.lambda.model.GetFunctionResult; import org.junit.Test; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +import static org.facil.practice.ReflectionUtils.setNonAccesibleField; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + /** * User: blangel * Date: 12/31/15 @@ -9,24 +23,115 @@ */ public class AbstractLambdaHandlerTest { + private AbstractLambdaHandler abstractLambdaHandler; + private AWSLambda awsLambda; + private String functionName; + private GetFunctionRequest getFunctionRequest; + private GetFunctionResult getFunctionResult; + private String aliasType; + private String functionVersion; + private GetAliasRequest getAliasRequest; + private GetAliasResult getAliasResult; + + public AbstractLambdaHandlerTest() throws NoSuchFieldException, IllegalAccessException { + abstractLambdaHandler = mock(AbstractLambdaHandler.class, CALLS_REAL_METHODS); + awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + functionName = "getSomething"; + setNonAccesibleField(abstractLambdaHandler.getClass(),abstractLambdaHandler,"awsLambda", awsLambda); + setNonAccesibleField(abstractLambdaHandler.getClass(),abstractLambdaHandler,"functionName", functionName); + getFunctionRequest = mock(GetFunctionRequest.class, RETURNS_DEEP_STUBS); + getFunctionResult = mock(GetFunctionResult.class); + when(getFunctionRequest.withFunctionName(functionName)).thenReturn(getFunctionRequest); + when(awsLambda.getFunction(getFunctionRequest.withFunctionName(functionName))).thenReturn(getFunctionResult); + + aliasType = "someAlias"; + functionVersion = "10.1"; + getAliasRequest = new GetAliasRequest(); + getAliasResult = new GetAliasResult(); + getAliasResult.setFunctionVersion(functionVersion); + when(awsLambda.getAlias(getAliasRequest.withFunctionName(functionName).withName(aliasType))).thenReturn(getAliasResult); + } + @Test public void doesLambdaFunctionExist() { - // TODO + assertTrue(abstractLambdaHandler.doesLambdaFunctionExist()); } + @Test - public void doesLambdaFunctionExistWithArgument() { - // TODO + public void doesLambdaFunctionExistVerifyMethodCalls(){ + abstractLambdaHandler.doesLambdaFunctionExist(); + verify(getFunctionRequest,times(1)).withFunctionName(functionName); + verify(awsLambda).getFunction((GetFunctionRequest) any()); } + + @SuppressWarnings("unchecked") + @Test + public void doesLambdaFunctionExistException(){ + when(awsLambda.getFunction((GetFunctionRequest) any())).thenThrow(AmazonClientException.class); + assertFalse(abstractLambdaHandler.doesLambdaFunctionExist()); + verify(awsLambda).getFunction((GetFunctionRequest) any()); + } + + //Obviously not a good test case(because verifying a print message is error prone). + // Couldn't able to test in any other way because it is an overloaded method(for which implementation has been tested) + // with just a flag to print a message. + @SuppressWarnings("unchecked") + @Test + public void doesLambdaFunctionExistWithArgument() throws IOException { + PrintStream stdOut = System.out; + try(ByteArrayOutputStream content = new ByteArrayOutputStream()) { + System.setOut(new PrintStream(content)); + when(awsLambda.getFunction((GetFunctionRequest) any())).thenThrow(AmazonClientException.class); + abstractLambdaHandler.doesLambdaFunctionExist(true); + assertEquals(String.format("^error^ Lambda function [ %s ] does not exist^r^%n", functionName), content.toString()); + }finally { + System.setOut(stdOut); + } + } + + @Test public void getAliasVersion() { - // TODO + assertEquals(functionVersion, abstractLambdaHandler.getAliasVersion(aliasType)); + } + + @Test + public void getAliasVersionNotSame() { + assertNotSame("9", abstractLambdaHandler.getAliasVersion(aliasType)); + } + + @Test + public void getAliasVersionVerifyMethodCalls(){ + abstractLambdaHandler.getAliasVersion(aliasType); + verify(awsLambda).getAlias((GetAliasRequest) any()); + } + + @SuppressWarnings("unchecked") + @Test + public void getAliasVersionVerifyException(){ + when(awsLambda.getAlias((GetAliasRequest) any())).thenThrow(AmazonClientException.class); + assertNull(abstractLambdaHandler.getAliasVersion(aliasType)); + verify(awsLambda).getAlias((GetAliasRequest) any()); } + + //Obviously not a good test case(because verifying a print message is error prone). + // Looks like there is no other way to test because it is just a overloaded method + // with just a flag to print a message. + @SuppressWarnings("unchecked") @Test - public void getAliasVersionWithTwoArguments() { - // TODO + public void getAliasVersionWithTwoArguments() throws IOException{ + PrintStream stdOut = System.out; + try(ByteArrayOutputStream content = new ByteArrayOutputStream()) { + System.setOut(new PrintStream(content)); + when(awsLambda.getAlias((GetAliasRequest) any())).thenThrow(AmazonClientException.class); + abstractLambdaHandler.getAliasVersion(aliasType, true); + assertEquals(String.format("^error^ Alias [ %s ] does not exist for Lambda function [ %s ]^r^%n", aliasType, functionName), content.toString()); + }finally { + System.setOut(stdOut); + } } } diff --git a/src/test/java/org/facil/practice/DefaultFileHandlerTest.java b/src/test/java/org/facil/practice/DefaultFileHandlerTest.java index 5e68958..afdfc17 100644 --- a/src/test/java/org/facil/practice/DefaultFileHandlerTest.java +++ b/src/test/java/org/facil/practice/DefaultFileHandlerTest.java @@ -1,6 +1,18 @@ package org.facil.practice; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; /** * User: blangel @@ -9,14 +21,91 @@ */ public class DefaultFileHandlerTest { + private DefaultFileHandler defaultFileHandler; + private Answer answerIOExceptionForFileOperations; + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + public DefaultFileHandlerTest(){ + this.defaultFileHandler = new DefaultFileHandler(); + answerIOExceptionForFileOperations = new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("getName")){ + return "test.txt"; + } + if(true) { + throw new IOException(); + } + return null; + } + }; + } + + @Test + public void SaveFileNullArgs(){ + assertFalse(defaultFileHandler.saveFile(null, null)); + } + + @Test + public void SaveFileNullArgForFile(){ + assertFalse(defaultFileHandler.saveFile(null, "Hello")); + } + + @Test + public void SaveFileNullArgForVersion(){ + assertFalse(defaultFileHandler.saveFile(new File("test.txt"), null)); + } + + @Test + public void saveFile() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + assertTrue(defaultFileHandler.saveFile(file, "Hello")); + } + + @Test + public void saveFileExists() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + defaultFileHandler.saveFile(file, "Hello"); + assertTrue(file.exists()); + } + + @Test + public void SaveFileIOException(){ + File file = mock(File.class, answerIOExceptionForFileOperations); + assertFalse(defaultFileHandler.saveFile(file, "Hello")); + verify(file).getName(); + } + + @Test + public void readFileNullArgForFile(){ + assertNull(defaultFileHandler.readFile(null)); + } + + @Test + public void readFile() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); + bufferedWriter.write("Hello"); + bufferedWriter.close(); + assertEquals("Hello", defaultFileHandler.readFile(file)); + } + + @Test - public void saveFile() { - // TODO + public void readFileNotSame() throws IOException { + File file = temporaryFolder.newFile("test.txt"); + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); + bufferedWriter.write("Bye"); + bufferedWriter.close(); + assertNotSame("Hello", defaultFileHandler.readFile(file)); } @Test - public void readFile() { - // TODO + public void readFileIOException(){ + File file = mock(File.class, answerIOExceptionForFileOperations); + assertNull(defaultFileHandler.readFile(file)); + verify(file).getName(); } } diff --git a/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java b/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java index 618b2c7..07aa9d0 100644 --- a/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java +++ b/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java @@ -1,6 +1,24 @@ package org.facil.practice; +import com.amazonaws.AmazonClientException; +import com.amazonaws.services.lambda.AWSLambda; +import com.amazonaws.services.lambda.model.GetFunctionRequest; +import com.amazonaws.services.lambda.model.PublishVersionRequest; +import com.amazonaws.services.lambda.model.PublishVersionResult; +import org.junit.After; import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.io.*; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import static junit.framework.Assert.assertNull; +import static org.facil.practice.ReflectionUtils.setNonAccesibleField; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; /** * User: blangel @@ -9,14 +27,120 @@ */ public class LambdaFunctionPublishTest { + private AWSLambda awsLambda; + private String versionDescription = "This version adds new functionality"; + private FileHandler fileHandler; + private String functionName = "getSomething"; + private String fileName = "test.txt"; + private String version = "Version 9.08"; + private File file; + private PublishVersionRequest publishVersionRequest = new PublishVersionRequest();//mock(PublishVersionRequest.class, CALLS_REAL_METHODS); + private PublishVersionResult publishVersionResult = new PublishVersionResult();//mock(PublishVersionResult.class, CALLS_REAL_METHODS); + private LambdaFunctionPublish lambdaFunctionPublish; + + public LambdaFunctionPublishTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + publishVersionResult.setVersion(version); + fileHandler = new DefaultFileHandler(); + file = new File(fileName); + awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + + Constructor constructor = LambdaFunctionPublish.class.getDeclaredConstructor(String.class, String.class, AWSLambda.class, + String.class, File.class, FileHandler.class); + constructor.setAccessible(true); + lambdaFunctionPublish = (LambdaFunctionPublish) constructor.newInstance(functionName, versionDescription, awsLambda, fileName, file, fileHandler); + when(awsLambda.publishVersion(publishVersionRequest.withFunctionName(functionName).withDescription(versionDescription))).thenReturn(publishVersionResult); + } + + + @After + public void deleteFile(){ + if(file.exists()){ + file.delete(); + } + } + @Test - public void publishVersion() { - // TODO + public void publishVersion() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method method = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); + method.setAccessible(true); + assertNotNull(method.invoke(lambdaFunctionPublish, null)); + verify(awsLambda).publishVersion((PublishVersionRequest) any()); + } + + @SuppressWarnings("unchecked") + @Test + public void publishVersionException() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + when(awsLambda.publishVersion((PublishVersionRequest) any())).thenThrow(AmazonClientException.class); + Method method = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); + method.setAccessible(true); + assertNull(method.invoke(lambdaFunctionPublish, null)); + verify(awsLambda).publishVersion((PublishVersionRequest) any()); + } + + @Test + public void verifyPublishVersionResultObject() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method method = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); + method.setAccessible(true); + PublishVersionResult publishVersionResultLocal = (PublishVersionResult) method.invoke(lambdaFunctionPublish, null); + assertEquals(version, publishVersionResultLocal.getVersion()); } @Test public void publishNewVersion() { - // TODO + assertTrue(lambdaFunctionPublish.publishNewVersion()); + } + + @Test + public void publishNewVersionReadSavedFile() throws IOException { + lambdaFunctionPublish.publishNewVersion(); + assertTrue(file.exists()); + try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){ + assertEquals(version, bufferedReader.readLine()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void publishNewVersionException() { + when(awsLambda.publishVersion((PublishVersionRequest) any())).thenThrow(AmazonClientException.class); + assertFalse(lambdaFunctionPublish.publishNewVersion()); + } + + @Test + public void publishNewVersionVerifyMethodCalls(){ + lambdaFunctionPublish.publishNewVersion(); + verify(awsLambda).publishVersion((PublishVersionRequest) any()); + } + + @Test + public void publishNewVersionWhenCreateFileNotsExistsFalse() throws NoSuchFieldException, IllegalAccessException { + file = mock(File.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("createNewFile")){ + return Boolean.FALSE; + } + if(invocation.getMethod().getName().equals("exists")){ + return Boolean.FALSE; + } + return null; + } + }); + setNonAccesibleField(lambdaFunctionPublish.getClass(), lambdaFunctionPublish, "file", file); + assertFalse(lambdaFunctionPublish.publishNewVersion()); + } + + @SuppressWarnings("unchecked") + @Test + public void publishNewVersionWhenDoesLambdaFunctionExistsFalse(){ + when(awsLambda.getFunction((GetFunctionRequest) any())).thenThrow(AmazonClientException.class); + assertFalse(lambdaFunctionPublish.publishNewVersion()); + } + + @Test + public void publishNewVersionPublishVersionResultNull(){ + when(awsLambda.publishVersion(publishVersionRequest.withFunctionName(functionName).withDescription(versionDescription))).thenReturn(null); + assertFalse(lambdaFunctionPublish.publishNewVersion()); } } From 0cd6996a551649a0e64b9ca0e202fa184f2a099c Mon Sep 17 00:00:00 2001 From: Sumanth Varma Dommaraju Date: Sun, 10 Jan 2016 10:47:34 -0500 Subject: [PATCH 3/5] Refactoring --- .../AbstractLambdaFileHandlerTest.java | 155 +++++------ .../practice/AbstractLambdaHandlerTest.java | 114 ++++---- .../practice/DefaultFileHandlerTest.java | 113 +++++--- .../practice/LambdaFunctionPublishTest.java | 256 +++++++++++++----- .../org/facil/practice/ReflectionUtils.java | 28 +- 5 files changed, 392 insertions(+), 274 deletions(-) diff --git a/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java b/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java index e92111d..261f5f9 100644 --- a/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java +++ b/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java @@ -1,18 +1,15 @@ package org.facil.practice; -import com.amazonaws.services.lambda.AWSLambda; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.TemporaryFolder; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import static org.facil.practice.ReflectionUtils.setNonAccesibleField; -import static org.junit.Assert.*; +import static org.facil.practice.ReflectionUtils.setNonAccessibleField; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.*; /** @@ -22,135 +19,111 @@ */ public class AbstractLambdaFileHandlerTest { - private AbstractLambdaFileHandler abstractLambdaFileHandler; - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - private String fileName = "test.txt"; - private File file; - private AWSLambda awsLambda; - private String functionName = "getSomething"; - - public AbstractLambdaFileHandlerTest() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { - abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); - awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); - - setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "fileName", fileName); - setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "awsLambda", awsLambda); - setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "functionName", functionName); - - } - @Test public void createFileIfNotExists() throws IOException, NoSuchFieldException, IllegalAccessException { - file = temporaryFolder.newFile(fileName); - if(file.exists() && !file.delete()){ - fail(); - } - setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); - assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); - } - - @Test - public void createFileIfNotExistsVerifyMethodCalls() throws IOException, NoSuchFieldException, IllegalAccessException { - final File helperFile = temporaryFolder.newFile(fileName); - file = mock(File.class, new AnswerForFile(helperFile)); - if(helperFile.exists() && !helperFile.delete()){ - fail(); - } - setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + File file = mock(File.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("exists")){ + return Boolean.FALSE; + } + if(invocation.getMethod().getName().equals("createNewFile")){ + return Boolean.TRUE; + } + return invocation.callRealMethod(); + } + }); + AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaFileHandler, "file", file); assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); verify(file,times(1)).exists(); verify(file,times(1)).createNewFile(); } + @Test - public void dontCreateFileIfFileExists() throws IOException, NoSuchFieldException, IllegalAccessException { - final File helperFile = temporaryFolder.newFile(fileName); - file = mock(File.class, new AnswerForFile(helperFile)); - setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + public void donotCreateNewFileIfExists() throws IOException, NoSuchFieldException, IllegalAccessException { + File file = mock(File.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("exists")){ + return Boolean.TRUE; + } + if(invocation.getMethod().getName().equals("createNewFile")){ + return Boolean.FALSE; + } + return invocation.callRealMethod(); + } + }); + AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaFileHandler, "file", file); assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); verify(file,times(1)).exists(); verify(file,never()).createNewFile(); } - @Test - public void createFileIfNotExistsCatchAndReturnFalse() throws NoSuchFieldException, IllegalAccessException { - Answer answerIOExceptionForFileOperations = new Answer() { + public void donotCreateFileWhenFileOperationsReturnFalse() throws NoSuchFieldException, IllegalAccessException, IOException { + String fileName = "test.txt"; + File file = mock(File.class, new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { if(invocation.getMethod().getName().equals("exists")){ return Boolean.FALSE; } - if(true) { - throw new IOException(); + if(invocation.getMethod().getName().equals("createNewFile")){ + return Boolean.FALSE; } - return null; + return invocation.callRealMethod(); } - }; - file = mock(File.class, answerIOExceptionForFileOperations); - setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + }); + AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaFileHandler, "file", file); + setNonAccessibleField(abstractLambdaFileHandler, "fileName", fileName); assertFalse(abstractLambdaFileHandler.createFileIfNotExists()); + verify(file,times(1)).exists(); + verify(file,times(1)).createNewFile(); } @Test(expected = IOException.class) - public void createFileIfNotExistsException() throws NoSuchFieldException, IllegalAccessException { - Answer answerIOExceptionForAllFileOperations = new Answer() { + public void createFileIfNotExistsWhenExceptionForFileOperations() throws NoSuchFieldException, IllegalAccessException, IOException { + File file = mock(File.class, new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { if(true) { throw new IOException(); } - return null; + return invocation.callRealMethod(); } - }; - file = mock(File.class, answerIOExceptionForAllFileOperations); - setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + }); + AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaFileHandler, "file", file); abstractLambdaFileHandler.createFileIfNotExists(); + verify(file,times(1)).exists(); + verify(file,never()).createNewFile(); } @Test - public void dontCreateFileIfFileOperationsReturnFalse() throws NoSuchFieldException, IllegalAccessException { - Answer answerIOExceptionForFileOperations = new Answer() { + public void createFileIfNotExistsFalseWhenIOException() throws NoSuchFieldException, IllegalAccessException, IOException { + String fileName = "test.txt"; + File file = mock(File.class, new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - if(invocation.getMethod().getName().equals("createNewFile")){ - return Boolean.FALSE; - } if(invocation.getMethod().getName().equals("exists")){ return Boolean.FALSE; } - if(true) { + if(invocation.getMethod().getName().equals("createNewFile")){ throw new IOException(); } - return null; + return invocation.callRealMethod(); } - }; - file = mock(File.class, answerIOExceptionForFileOperations); - setNonAccesibleField(abstractLambdaFileHandler.getClass(),abstractLambdaFileHandler, "file", file); + }); + AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaFileHandler, "file", file); + setNonAccessibleField(abstractLambdaFileHandler, "fileName", fileName); assertFalse(abstractLambdaFileHandler.createFileIfNotExists()); + verify(file,times(1)).exists(); + verify(file,times(1)).createNewFile(); } - - private class AnswerForFile implements Answer { - - private File helperFile; - public AnswerForFile(File helperFile){ - this.helperFile = helperFile; - } - - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - if(invocation.getMethod().getName().equals("createNewFile")){ - return helperFile.createNewFile(); - } - if(invocation.getMethod().getName().equals("exists")){ - return helperFile.exists(); - } - return null; - } - } - - } diff --git a/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java b/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java index c21c690..734e894 100644 --- a/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java +++ b/src/test/java/org/facil/practice/AbstractLambdaHandlerTest.java @@ -5,14 +5,15 @@ import com.amazonaws.services.lambda.model.GetAliasRequest; import com.amazonaws.services.lambda.model.GetAliasResult; import com.amazonaws.services.lambda.model.GetFunctionRequest; -import com.amazonaws.services.lambda.model.GetFunctionResult; import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; -import static org.facil.practice.ReflectionUtils.setNonAccesibleField; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.facil.practice.ReflectionUtils.setNonAccessibleField; import static org.junit.Assert.*; import static org.mockito.Mockito.*; @@ -23,67 +24,42 @@ */ public class AbstractLambdaHandlerTest { - private AbstractLambdaHandler abstractLambdaHandler; - private AWSLambda awsLambda; - private String functionName; - private GetFunctionRequest getFunctionRequest; - private GetFunctionResult getFunctionResult; - private String aliasType; - private String functionVersion; - private GetAliasRequest getAliasRequest; - private GetAliasResult getAliasResult; - - public AbstractLambdaHandlerTest() throws NoSuchFieldException, IllegalAccessException { - abstractLambdaHandler = mock(AbstractLambdaHandler.class, CALLS_REAL_METHODS); - awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); - functionName = "getSomething"; - setNonAccesibleField(abstractLambdaHandler.getClass(),abstractLambdaHandler,"awsLambda", awsLambda); - setNonAccesibleField(abstractLambdaHandler.getClass(),abstractLambdaHandler,"functionName", functionName); - getFunctionRequest = mock(GetFunctionRequest.class, RETURNS_DEEP_STUBS); - getFunctionResult = mock(GetFunctionResult.class); - when(getFunctionRequest.withFunctionName(functionName)).thenReturn(getFunctionRequest); - when(awsLambda.getFunction(getFunctionRequest.withFunctionName(functionName))).thenReturn(getFunctionResult); - - aliasType = "someAlias"; - functionVersion = "10.1"; - getAliasRequest = new GetAliasRequest(); - getAliasResult = new GetAliasResult(); - getAliasResult.setFunctionVersion(functionVersion); - when(awsLambda.getAlias(getAliasRequest.withFunctionName(functionName).withName(aliasType))).thenReturn(getAliasResult); - } - @Test - public void doesLambdaFunctionExist() { + public void doesLambdaFunctionExist() throws NoSuchFieldException, IllegalAccessException { + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + AbstractLambdaHandler abstractLambdaHandler = mock(AbstractLambdaHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaHandler,"awsLambda", awsLambda); assertTrue(abstractLambdaHandler.doesLambdaFunctionExist()); - } - - - @Test - public void doesLambdaFunctionExistVerifyMethodCalls(){ - abstractLambdaHandler.doesLambdaFunctionExist(); - verify(getFunctionRequest,times(1)).withFunctionName(functionName); verify(awsLambda).getFunction((GetFunctionRequest) any()); } - @SuppressWarnings("unchecked") @Test - public void doesLambdaFunctionExistException(){ + public void doesLambdaFunctionExistWhenException() throws NoSuchFieldException, IllegalAccessException { + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + AbstractLambdaHandler abstractLambdaHandler = mock(AbstractLambdaHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaHandler,"awsLambda", awsLambda); + setNonAccessibleField(abstractLambdaHandler,"functionName", "getSomething"); when(awsLambda.getFunction((GetFunctionRequest) any())).thenThrow(AmazonClientException.class); assertFalse(abstractLambdaHandler.doesLambdaFunctionExist()); verify(awsLambda).getFunction((GetFunctionRequest) any()); } //Obviously not a good test case(because verifying a print message is error prone). - // Couldn't able to test in any other way because it is an overloaded method(for which implementation has been tested) + // Couldn't able to test in any other way because it is an overloaded method(the method that has implementation already been tested) // with just a flag to print a message. @SuppressWarnings("unchecked") @Test - public void doesLambdaFunctionExistWithArgument() throws IOException { + public void doesLambdaFunctionExistWithArgument() throws IOException, NoSuchFieldException, IllegalAccessException { PrintStream stdOut = System.out; try(ByteArrayOutputStream content = new ByteArrayOutputStream()) { System.setOut(new PrintStream(content)); + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + String functionName = "getSomething"; when(awsLambda.getFunction((GetFunctionRequest) any())).thenThrow(AmazonClientException.class); + AbstractLambdaHandler abstractLambdaHandler = mock(AbstractLambdaHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaHandler,"awsLambda", awsLambda); + setNonAccessibleField(abstractLambdaHandler,"functionName", functionName); abstractLambdaHandler.doesLambdaFunctionExist(true); assertEquals(String.format("^error^ Lambda function [ %s ] does not exist^r^%n", functionName), content.toString()); }finally { @@ -93,27 +69,50 @@ public void doesLambdaFunctionExistWithArgument() throws IOException { @Test - public void getAliasVersion() { + public void getAliasVersion() throws NoSuchFieldException, IllegalAccessException { + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + GetAliasResult getAliasResult = mock(GetAliasResult.class); + String functionVersion = "10.9"; + String aliasType = "someAliasType"; + when(getAliasResult.getFunctionVersion()).thenReturn(functionVersion); + when(awsLambda.getAlias((GetAliasRequest) any())).thenReturn(getAliasResult); + AbstractLambdaHandler abstractLambdaHandler = mock(AbstractLambdaHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaHandler,"awsLambda", awsLambda); assertEquals(functionVersion, abstractLambdaHandler.getAliasVersion(aliasType)); + verify(awsLambda, times(1)).getAlias((GetAliasRequest) any()); + verify(getAliasResult, times(1)).getFunctionVersion(); } @Test - public void getAliasVersionNotSame() { - assertNotSame("9", abstractLambdaHandler.getAliasVersion(aliasType)); + public void getAliasVersionNotSame() throws NoSuchFieldException, IllegalAccessException { + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + GetAliasResult getAliasResult = mock(GetAliasResult.class); + String functionVersion = "10.9"; + String aliasType = "someAliasType"; + when(getAliasResult.getFunctionVersion()).thenReturn(functionVersion); + when(awsLambda.getAlias((GetAliasRequest) any())).thenReturn(getAliasResult); + AbstractLambdaHandler abstractLambdaHandler = mock(AbstractLambdaHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaHandler,"awsLambda", awsLambda); + assertNotSame("10.8", abstractLambdaHandler.getAliasVersion(aliasType)); + verify(awsLambda, times(1)).getAlias((GetAliasRequest) any()); + verify(getAliasResult, times(1)).getFunctionVersion(); } - @Test - public void getAliasVersionVerifyMethodCalls(){ - abstractLambdaHandler.getAliasVersion(aliasType); - verify(awsLambda).getAlias((GetAliasRequest) any()); - } @SuppressWarnings("unchecked") @Test - public void getAliasVersionVerifyException(){ + public void getAliasVersionWhenException() throws NoSuchFieldException, IllegalAccessException { + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); when(awsLambda.getAlias((GetAliasRequest) any())).thenThrow(AmazonClientException.class); + GetAliasResult getAliasResult = mock(GetAliasResult.class); + String aliasType = "someAliasType"; + String functionName = "getSomething"; + AbstractLambdaHandler abstractLambdaHandler = mock(AbstractLambdaHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaHandler,"awsLambda", awsLambda); + setNonAccessibleField(abstractLambdaHandler,"functionName", functionName); assertNull(abstractLambdaHandler.getAliasVersion(aliasType)); verify(awsLambda).getAlias((GetAliasRequest) any()); + verify(getAliasResult, never()).getFunctionVersion(); } @@ -122,13 +121,20 @@ public void getAliasVersionVerifyException(){ // with just a flag to print a message. @SuppressWarnings("unchecked") @Test - public void getAliasVersionWithTwoArguments() throws IOException{ + public void getAliasVersionWithTwoArguments() throws IOException, NoSuchFieldException, IllegalAccessException { PrintStream stdOut = System.out; try(ByteArrayOutputStream content = new ByteArrayOutputStream()) { System.setOut(new PrintStream(content)); + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + String aliasType = "someAliasType"; + String functionName = "getSomething"; + AbstractLambdaHandler abstractLambdaHandler = mock(AbstractLambdaHandler.class, CALLS_REAL_METHODS); + setNonAccessibleField(abstractLambdaHandler,"awsLambda", awsLambda); + setNonAccessibleField(abstractLambdaHandler,"functionName", functionName); when(awsLambda.getAlias((GetAliasRequest) any())).thenThrow(AmazonClientException.class); abstractLambdaHandler.getAliasVersion(aliasType, true); - assertEquals(String.format("^error^ Alias [ %s ] does not exist for Lambda function [ %s ]^r^%n", aliasType, functionName), content.toString()); + assertEquals(String.format("^error^ Alias [ %s ] does not exist for Lambda function [ %s ]^r^%n", aliasType, + functionName), content.toString()); }finally { System.setOut(stdOut); } diff --git a/src/test/java/org/facil/practice/DefaultFileHandlerTest.java b/src/test/java/org/facil/practice/DefaultFileHandlerTest.java index afdfc17..4a71008 100644 --- a/src/test/java/org/facil/practice/DefaultFileHandlerTest.java +++ b/src/test/java/org/facil/practice/DefaultFileHandlerTest.java @@ -1,8 +1,6 @@ package org.facil.practice; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.TemporaryFolder; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -21,89 +19,116 @@ */ public class DefaultFileHandlerTest { - private DefaultFileHandler defaultFileHandler; - private Answer answerIOExceptionForFileOperations; - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - public DefaultFileHandlerTest(){ - this.defaultFileHandler = new DefaultFileHandler(); - answerIOExceptionForFileOperations = new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - if(invocation.getMethod().getName().equals("getName")){ - return "test.txt"; - } - if(true) { - throw new IOException(); - } - return null; - } - }; - } - @Test public void SaveFileNullArgs(){ + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); assertFalse(defaultFileHandler.saveFile(null, null)); } @Test public void SaveFileNullArgForFile(){ - assertFalse(defaultFileHandler.saveFile(null, "Hello")); + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); + assertFalse(defaultFileHandler.saveFile(null, "version 9.9")); } @Test public void SaveFileNullArgForVersion(){ - assertFalse(defaultFileHandler.saveFile(new File("test.txt"), null)); + File file = mock(File.class); + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); + assertFalse(defaultFileHandler.saveFile(file, null)); } @Test - public void saveFile() throws IOException { - File file = temporaryFolder.newFile("test.txt"); - assertTrue(defaultFileHandler.saveFile(file, "Hello")); + public void saveFile() throws NoSuchFieldException, IllegalAccessException { + File file = spy(new File("test")); + String version = "9.9"; + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); + assertTrue(defaultFileHandler.saveFile(file,version)); + verify(file).getPath(); + if(file.exists()){ + file.delete(); + } } @Test - public void saveFileExists() throws IOException { - File file = temporaryFolder.newFile("test.txt"); - defaultFileHandler.saveFile(file, "Hello"); + public void saveFileVerifyFileExists() throws Exception { + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); + File file = spy(new File("test")); + defaultFileHandler.saveFile(file, "version 9.9"); assertTrue(file.exists()); + verify(file).getPath(); + if(file.exists()){ + file.delete(); + } } @Test public void SaveFileIOException(){ - File file = mock(File.class, answerIOExceptionForFileOperations); - assertFalse(defaultFileHandler.saveFile(file, "Hello")); + File file = mock(File.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("getName")){ + return "test"; + }else{ + throw new IOException(); + } + } + }); + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); + assertFalse(defaultFileHandler.saveFile(file, "version 9.9")); verify(file).getName(); } @Test public void readFileNullArgForFile(){ + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); assertNull(defaultFileHandler.readFile(null)); } @Test public void readFile() throws IOException { - File file = temporaryFolder.newFile("test.txt"); - BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); - bufferedWriter.write("Hello"); - bufferedWriter.close(); - assertEquals("Hello", defaultFileHandler.readFile(file)); + String version = "version 9.9"; + File file = spy(new File("test.txt")); + try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) { + bufferedWriter.write(version); + } + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); + assertEquals(version, defaultFileHandler.readFile(file)); + if(file.exists()){ + file.delete(); + } + verify(file, times(2)).getPath(); } @Test public void readFileNotSame() throws IOException { - File file = temporaryFolder.newFile("test.txt"); - BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); - bufferedWriter.write("Bye"); - bufferedWriter.close(); - assertNotSame("Hello", defaultFileHandler.readFile(file)); + String version = "version 9.9"; + File file = spy(new File("test.txt")); + try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) { + bufferedWriter.write(version); + } + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); + assertNotSame("version 9.8", defaultFileHandler.readFile(file)); + if(file.exists()){ + file.delete(); + } + verify(file, times(2)).getPath(); } @Test public void readFileIOException(){ - File file = mock(File.class, answerIOExceptionForFileOperations); + File file = mock(File.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("getName")){ + return "test"; + }else{ + throw new IOException(); + } + } + }); + DefaultFileHandler defaultFileHandler = new DefaultFileHandler(); assertNull(defaultFileHandler.readFile(file)); verify(file).getName(); } diff --git a/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java b/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java index 07aa9d0..aae0a59 100644 --- a/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java +++ b/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java @@ -5,19 +5,18 @@ import com.amazonaws.services.lambda.model.GetFunctionRequest; import com.amazonaws.services.lambda.model.PublishVersionRequest; import com.amazonaws.services.lambda.model.PublishVersionResult; -import org.junit.After; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import java.io.*; +import java.io.File; +import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import static junit.framework.Assert.assertNull; -import static org.facil.practice.ReflectionUtils.setNonAccesibleField; -import static org.junit.Assert.*; +import static junit.framework.Assert.*; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; /** @@ -27,94 +26,170 @@ */ public class LambdaFunctionPublishTest { - private AWSLambda awsLambda; - private String versionDescription = "This version adds new functionality"; - private FileHandler fileHandler; - private String functionName = "getSomething"; - private String fileName = "test.txt"; - private String version = "Version 9.08"; - private File file; - private PublishVersionRequest publishVersionRequest = new PublishVersionRequest();//mock(PublishVersionRequest.class, CALLS_REAL_METHODS); - private PublishVersionResult publishVersionResult = new PublishVersionResult();//mock(PublishVersionResult.class, CALLS_REAL_METHODS); - private LambdaFunctionPublish lambdaFunctionPublish; - - public LambdaFunctionPublishTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - publishVersionResult.setVersion(version); - fileHandler = new DefaultFileHandler(); - file = new File(fileName); - awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); - - Constructor constructor = LambdaFunctionPublish.class.getDeclaredConstructor(String.class, String.class, AWSLambda.class, - String.class, File.class, FileHandler.class); - constructor.setAccessible(true); - lambdaFunctionPublish = (LambdaFunctionPublish) constructor.newInstance(functionName, versionDescription, awsLambda, fileName, file, fileHandler); - when(awsLambda.publishVersion(publishVersionRequest.withFunctionName(functionName).withDescription(versionDescription))).thenReturn(publishVersionResult); + @Test + public void publishVersion() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, + InstantiationException { + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + String functionName = "getSomething"; + String fileName = "test.txt"; + String versionDescription = "This version adds new functionality"; + FileHandler fileHandler = mock(DefaultFileHandler.class); + File file = mock(File.class); + LambdaFunctionPublish lambdaFunctionPublish = getLambdaFunctionPublish(awsLambda, functionName, + fileName, versionDescription, fileHandler, file); + Method publishVersion = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); + publishVersion.setAccessible(true); + assertNotNull(publishVersion.invoke(lambdaFunctionPublish, null)); + verify(awsLambda).publishVersion((PublishVersionRequest) any()); } - - @After - public void deleteFile(){ - if(file.exists()){ - file.delete(); - } + @Test + public void publishVersionCheckPublishVersionResultObject() throws NoSuchMethodException, InvocationTargetException, + IllegalAccessException, InstantiationException { + AWSLambda awsLambda = mock(AWSLambda.class); + String functionName = "getSomething"; + String fileName = "test.txt"; + String versionDescription = "This version adds new functionality"; + FileHandler fileHandler = mock(DefaultFileHandler.class); + File file = mock(File.class); + PublishVersionResult publishVersionResult = mock(PublishVersionResult.class); + when(awsLambda.publishVersion((PublishVersionRequest) any())).thenReturn(publishVersionResult); + LambdaFunctionPublish lambdaFunctionPublish = getLambdaFunctionPublish(awsLambda, functionName, fileName, + versionDescription, fileHandler, file); + Method publishVersion = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); + publishVersion.setAccessible(true); + assertEquals(publishVersionResult, publishVersion.invoke(lambdaFunctionPublish, null)); + verify(awsLambda).publishVersion((PublishVersionRequest) any()); } @Test - public void publishVersion() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Method method = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); - method.setAccessible(true); - assertNotNull(method.invoke(lambdaFunctionPublish, null)); + public void PublishVersionCheckVersion() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, + InstantiationException { + AWSLambda awsLambda = mock(AWSLambda.class); + String functionName = "getSomething"; + String fileName = "test.txt"; + String versionDescription = "This version adds new functionality"; + String version = "Version 9.08"; + FileHandler fileHandler = mock(DefaultFileHandler.class); + File file = mock(File.class); + PublishVersionResult publishVersionResult = mock(PublishVersionResult.class); + when(publishVersionResult.getVersion()).thenReturn(version); + when(awsLambda.publishVersion((PublishVersionRequest) any())).thenReturn(publishVersionResult); + LambdaFunctionPublish lambdaFunctionPublish = getLambdaFunctionPublish(awsLambda, functionName, fileName, + versionDescription, fileHandler, file); + Method publishVersion = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); + publishVersion.setAccessible(true); + PublishVersionResult publishVersionResultLocal = (PublishVersionResult) publishVersion.invoke(lambdaFunctionPublish, null); + assertEquals(version, publishVersionResultLocal.getVersion()); verify(awsLambda).publishVersion((PublishVersionRequest) any()); } @SuppressWarnings("unchecked") @Test - public void publishVersionException() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + public void publishVersionWhenExceptionReturnNull() throws NoSuchMethodException, InvocationTargetException, + IllegalAccessException, InstantiationException { + AWSLambda awsLambda = mock(AWSLambda.class, RETURNS_DEEP_STUBS); + String functionName = "getSomething"; + String fileName = "test.txt"; + String versionDescription = "This version adds new functionality"; + FileHandler fileHandler = mock(DefaultFileHandler.class); + File file = mock(File.class); + LambdaFunctionPublish lambdaFunctionPublish = getLambdaFunctionPublish(awsLambda, functionName, fileName, + versionDescription, fileHandler, file); when(awsLambda.publishVersion((PublishVersionRequest) any())).thenThrow(AmazonClientException.class); - Method method = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); - method.setAccessible(true); - assertNull(method.invoke(lambdaFunctionPublish, null)); + Method publishVersion = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); + publishVersion.setAccessible(true); + assertNull(publishVersion.invoke(lambdaFunctionPublish, null)); verify(awsLambda).publishVersion((PublishVersionRequest) any()); } - @Test - public void verifyPublishVersionResultObject() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Method method = lambdaFunctionPublish.getClass().getDeclaredMethod("publishVersion",null); - method.setAccessible(true); - PublishVersionResult publishVersionResultLocal = (PublishVersionResult) method.invoke(lambdaFunctionPublish, null); - assertEquals(version, publishVersionResultLocal.getVersion()); - } - @Test - public void publishNewVersion() { - assertTrue(lambdaFunctionPublish.publishNewVersion()); - } @Test - public void publishNewVersionReadSavedFile() throws IOException { - lambdaFunctionPublish.publishNewVersion(); - assertTrue(file.exists()); - try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){ - assertEquals(version, bufferedReader.readLine()); - } + public void publishNewVersion() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, + InstantiationException { + AWSLambda awsLambda = mock(AWSLambda.class);// RETURNS_DEEP_STUBS can also be used instead of mocking PublishVersionResult + //then returning it when awsLambda.publishVersion is called. + String functionName = "getSomething"; + String fileName = "test.txt"; + String version = "9.9"; + String versionDescription = "This version adds new functionality"; + FileHandler fileHandler = mock(DefaultFileHandler.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("saveFile")){ + return Boolean.TRUE; + } + return invocation.callRealMethod(); } + }); + File file = mock(File.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("exists")){ + return Boolean.TRUE; + } + return invocation.callRealMethod(); + } + }); + + LambdaFunctionPublish lambdaFunctionPublish = getLambdaFunctionPublish(awsLambda, functionName, fileName, + versionDescription, fileHandler, file); + PublishVersionResult publishVersionResult = mock(PublishVersionResult.class); + when(publishVersionResult.getVersion()).thenReturn(version); + when(awsLambda.publishVersion((PublishVersionRequest) any())).thenReturn(publishVersionResult); + assertTrue(lambdaFunctionPublish.publishNewVersion()); + verify(file).exists(); + verify(fileHandler).saveFile(file, version); + verify(awsLambda).publishVersion((PublishVersionRequest) any()); } @SuppressWarnings("unchecked") @Test - public void publishNewVersionException() { - when(awsLambda.publishVersion((PublishVersionRequest) any())).thenThrow(AmazonClientException.class); + public void publishNewVersionFalseWhenPublishResultIsNull() + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + AWSLambda awsLambda = mock(AWSLambda.class); + String functionName = "getSomething"; + String fileName = "test.txt"; + String versionDescription = "This version adds new functionality"; + FileHandler fileHandler = mock(DefaultFileHandler.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("saveFile")){ + return Boolean.TRUE; + } + return invocation.callRealMethod(); + } + }); + File file = mock(File.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("exists")){ + return Boolean.TRUE; + } + return invocation.callRealMethod(); + } + }); + when(awsLambda.publishVersion((PublishVersionRequest) any())).thenReturn(null); + LambdaFunctionPublish lambdaFunctionPublish = getLambdaFunctionPublish(awsLambda, functionName, fileName, + versionDescription, fileHandler, file); assertFalse(lambdaFunctionPublish.publishNewVersion()); - } - - @Test - public void publishNewVersionVerifyMethodCalls(){ - lambdaFunctionPublish.publishNewVersion(); + verify(file).exists(); + // fileHandler.saveFile should not be invoked because implementation uses short circuit && + //so return statement should return false immediately when it sees result != null + verify(fileHandler, never()).saveFile((File) any() , (String) any()); verify(awsLambda).publishVersion((PublishVersionRequest) any()); } + @Test - public void publishNewVersionWhenCreateFileNotsExistsFalse() throws NoSuchFieldException, IllegalAccessException { - file = mock(File.class, new Answer() { + public void publishNewVersionFalseWhenCreateFileNotExistsFalse() throws NoSuchFieldException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException, InstantiationException, IOException { + AWSLambda awsLambda = mock(AWSLambda.class); + String functionName = "getSomething"; + String fileName = "test.txt"; + String versionDescription = "This version adds new functionality"; + FileHandler fileHandler = mock(DefaultFileHandler.class); + File file = mock(File.class, new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { if(invocation.getMethod().getName().equals("createNewFile")){ @@ -123,24 +198,55 @@ public Object answer(InvocationOnMock invocation) throws Throwable { if(invocation.getMethod().getName().equals("exists")){ return Boolean.FALSE; } - return null; + return invocation.callRealMethod(); } }); - setNonAccesibleField(lambdaFunctionPublish.getClass(), lambdaFunctionPublish, "file", file); + PublishVersionResult publishVersionResult = mock(PublishVersionResult.class); + when(awsLambda.publishVersion((PublishVersionRequest) any())).thenReturn(publishVersionResult); + + LambdaFunctionPublish lambdaFunctionPublish = getLambdaFunctionPublish(awsLambda, functionName, fileName, versionDescription, fileHandler, file); assertFalse(lambdaFunctionPublish.publishNewVersion()); + verify(file).exists(); + verify(file).createNewFile(); + verify(fileHandler, never()).saveFile((File) any(), (String) any()); + verify(awsLambda, never()).publishVersion((PublishVersionRequest) any()); } @SuppressWarnings("unchecked") @Test - public void publishNewVersionWhenDoesLambdaFunctionExistsFalse(){ + public void publishNewVersionFalseWhenDoesLambdaFunctionExistsFalse() + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException { + AWSLambda awsLambda = mock(AWSLambda.class); + String functionName = "getSomething"; + String fileName = "test.txt"; + String versionDescription = "This version adds new functionality"; + FileHandler fileHandler = mock(DefaultFileHandler.class); + File file = mock(File.class, new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + if(invocation.getMethod().getName().equals("exists")){ + return Boolean.TRUE; + } + return invocation.callRealMethod(); + } + }); + LambdaFunctionPublish lambdaFunctionPublish = spy(new LambdaFunctionPublish(functionName, versionDescription, + awsLambda, fileName, file, fileHandler)); when(awsLambda.getFunction((GetFunctionRequest) any())).thenThrow(AmazonClientException.class); assertFalse(lambdaFunctionPublish.publishNewVersion()); + verify(file).exists(); + verify(file, never()).createNewFile(); + verify(awsLambda).getFunction((GetFunctionRequest) any()); + verify(fileHandler, never()).saveFile((File) any(), (String) any()); } - @Test - public void publishNewVersionPublishVersionResultNull(){ - when(awsLambda.publishVersion(publishVersionRequest.withFunctionName(functionName).withDescription(versionDescription))).thenReturn(null); - assertFalse(lambdaFunctionPublish.publishNewVersion()); - } + private LambdaFunctionPublish getLambdaFunctionPublish(AWSLambda awsLambda, String functionName, String fileName, + String versionDescription, FileHandler fileHandler, File file) + throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { + Constructor constructor = LambdaFunctionPublish.class.getDeclaredConstructor(String.class, String.class, AWSLambda.class, + String.class, File.class, FileHandler.class); + constructor.setAccessible(true); + return (LambdaFunctionPublish) constructor.newInstance(functionName, versionDescription, awsLambda, fileName, file, fileHandler); + } } diff --git a/src/test/java/org/facil/practice/ReflectionUtils.java b/src/test/java/org/facil/practice/ReflectionUtils.java index 6647f62..99c01e9 100644 --- a/src/test/java/org/facil/practice/ReflectionUtils.java +++ b/src/test/java/org/facil/practice/ReflectionUtils.java @@ -7,17 +7,25 @@ */ public class ReflectionUtils { - public static void setNonAccesibleField(Class clazz, T obj, String fieldName, U value) throws IllegalAccessException, NoSuchFieldException { - if(clazz == null){ - throw new NoSuchFieldException(); + public static void setNonAccessibleField(Object object, String fieldName, Object value) throws IllegalAccessException, + NoSuchFieldException { + Field field = getField(object.getClass(), fieldName); + field.setAccessible(true); + field.set(object, value); + } + + private static Field getField(Class clazz, String fieldName) throws NoSuchFieldException { + Field field = null; + for(Class cls = clazz; cls != null; cls = cls.getSuperclass()){ + try{ + field = cls.getDeclaredField(fieldName); + }catch (NoSuchFieldException nsme){ + //If not ignored cannot walk through class hierarchy + } } - try{ - Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - field.set(obj, value); - return; - }catch (NoSuchFieldException nsfe){ - setNonAccesibleField(clazz.getSuperclass(), obj, fieldName, value); + if(field == null){ + throw new NoSuchFieldException(); } + return field; } } From cf8c7dd8c7d450b4b8b236c91b7de2428e3d7c91 Mon Sep 17 00:00:00 2001 From: Sumanth Varma Dommaraju Date: Sat, 16 Jan 2016 16:41:33 -0500 Subject: [PATCH 4/5] Refactoring --- .../AbstractLambdaFileHandlerTest.java | 73 ++++--------------- 1 file changed, 14 insertions(+), 59 deletions(-) diff --git a/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java b/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java index 261f5f9..0ab03e8 100644 --- a/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java +++ b/src/test/java/org/facil/practice/AbstractLambdaFileHandlerTest.java @@ -1,8 +1,6 @@ package org.facil.practice; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.io.File; import java.io.IOException; @@ -21,18 +19,9 @@ public class AbstractLambdaFileHandlerTest { @Test public void createFileIfNotExists() throws IOException, NoSuchFieldException, IllegalAccessException { - File file = mock(File.class, new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - if(invocation.getMethod().getName().equals("exists")){ - return Boolean.FALSE; - } - if(invocation.getMethod().getName().equals("createNewFile")){ - return Boolean.TRUE; - } - return invocation.callRealMethod(); - } - }); + File file = mock(File.class); + when(file.exists()).thenReturn(Boolean.FALSE); + when(file.createNewFile()).thenReturn(Boolean.TRUE); AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); setNonAccessibleField(abstractLambdaFileHandler, "file", file); assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); @@ -43,18 +32,9 @@ public Object answer(InvocationOnMock invocation) throws Throwable { @Test public void donotCreateNewFileIfExists() throws IOException, NoSuchFieldException, IllegalAccessException { - File file = mock(File.class, new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - if(invocation.getMethod().getName().equals("exists")){ - return Boolean.TRUE; - } - if(invocation.getMethod().getName().equals("createNewFile")){ - return Boolean.FALSE; - } - return invocation.callRealMethod(); - } - }); + File file = mock(File.class); + when(file.exists()).thenReturn(Boolean.TRUE); + when(file.createNewFile()).thenReturn(Boolean.FALSE); AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); setNonAccessibleField(abstractLambdaFileHandler, "file", file); assertTrue(abstractLambdaFileHandler.createFileIfNotExists()); @@ -65,18 +45,9 @@ public Object answer(InvocationOnMock invocation) throws Throwable { @Test public void donotCreateFileWhenFileOperationsReturnFalse() throws NoSuchFieldException, IllegalAccessException, IOException { String fileName = "test.txt"; - File file = mock(File.class, new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - if(invocation.getMethod().getName().equals("exists")){ - return Boolean.FALSE; - } - if(invocation.getMethod().getName().equals("createNewFile")){ - return Boolean.FALSE; - } - return invocation.callRealMethod(); - } - }); + File file = mock(File.class); + when(file.exists()).thenReturn(Boolean.FALSE); + when(file.createNewFile()).thenReturn(Boolean.FALSE); AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); setNonAccessibleField(abstractLambdaFileHandler, "file", file); setNonAccessibleField(abstractLambdaFileHandler, "fileName", fileName); @@ -87,15 +58,8 @@ public Object answer(InvocationOnMock invocation) throws Throwable { @Test(expected = IOException.class) public void createFileIfNotExistsWhenExceptionForFileOperations() throws NoSuchFieldException, IllegalAccessException, IOException { - File file = mock(File.class, new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - if(true) { - throw new IOException(); - } - return invocation.callRealMethod(); - } - }); + File file = mock(File.class); + when(file.exists()).thenThrow(IOException.class); AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); setNonAccessibleField(abstractLambdaFileHandler, "file", file); abstractLambdaFileHandler.createFileIfNotExists(); @@ -106,18 +70,9 @@ public Object answer(InvocationOnMock invocation) throws Throwable { @Test public void createFileIfNotExistsFalseWhenIOException() throws NoSuchFieldException, IllegalAccessException, IOException { String fileName = "test.txt"; - File file = mock(File.class, new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - if(invocation.getMethod().getName().equals("exists")){ - return Boolean.FALSE; - } - if(invocation.getMethod().getName().equals("createNewFile")){ - throw new IOException(); - } - return invocation.callRealMethod(); - } - }); + File file = mock(File.class); + when(file.exists()).thenReturn(Boolean.FALSE); + when(file.createNewFile()).thenThrow(IOException.class); AbstractLambdaFileHandler abstractLambdaFileHandler = mock(AbstractLambdaFileHandler.class, CALLS_REAL_METHODS); setNonAccessibleField(abstractLambdaFileHandler, "file", file); setNonAccessibleField(abstractLambdaFileHandler, "fileName", fileName); From 7ce9ef72f7b1cd975bf4ac6899fc3987324225f9 Mon Sep 17 00:00:00 2001 From: Sumanth Varma Dommaraju Date: Sat, 16 Jan 2016 16:45:37 -0500 Subject: [PATCH 5/5] Refactoring --- src/test/java/org/facil/practice/DefaultFileHandlerTest.java | 1 + src/test/java/org/facil/practice/LambdaFunctionPublishTest.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/test/java/org/facil/practice/DefaultFileHandlerTest.java b/src/test/java/org/facil/practice/DefaultFileHandlerTest.java index 4a71008..537aa50 100644 --- a/src/test/java/org/facil/practice/DefaultFileHandlerTest.java +++ b/src/test/java/org/facil/practice/DefaultFileHandlerTest.java @@ -64,6 +64,7 @@ public void saveFileVerifyFileExists() throws Exception { @Test public void SaveFileIOException(){ + //using answer seems more appropriate here than using when File file = mock(File.class, new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { diff --git a/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java b/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java index aae0a59..cc73689 100644 --- a/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java +++ b/src/test/java/org/facil/practice/LambdaFunctionPublishTest.java @@ -114,6 +114,7 @@ public void publishNewVersion() throws NoSuchMethodException, IllegalAccessExcep String fileName = "test.txt"; String version = "9.9"; String versionDescription = "This version adds new functionality"; + //using answer seems more appropriate here than using when FileHandler fileHandler = mock(DefaultFileHandler.class, new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable {