org.mockito.exceptions.misusing.NotAMockException - java

I have tried below lines
#Test
public void getXYZ_Success() throws Exception {
Response result=abc.XYZ(exampleHeader);
Response response=new Response();
response.setMessage(null);
response.setStatusCode("01");
response.setStatus("Failure");
List<ExampleFilterLkp> exampleFilterList=new ArrayList<exampleFilterLkp>();
exampleFilterLkp exampleFilterLkp=new exampleFilterLkp();
examplFilterLkp.setexampleKey("1");
exampleList.add(exampleFilterLkp);
doReturn(response).when(result);
}
I'm getting below error, how to solve this issue, please help me
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
at com.firstdata.mpl.manager.exampleTest.getexampleFilter_NullFailure(FuelManagerTest.java:184)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:670)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Exception message says that argument passed to when() is not a mock.
In this case, it's a result
doReturn(response).when(result);
Exception message even says what a correct invocation should look like:
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
It isn't clear from the question what is being tested here. If abc is a mock object and you want to return the response on XYZ call then something like this should work:
doReturn(response).when(abc).XYZ(exampleHeader);
Otherwise, you need to explain what exactly are you trying to achieve.

result object is not a mock object. when needs a mock object as an arg --> when(mock)

Related

BeforeStories exception

I would like to report stacktrace of my exception to the console, so if I have:
#BeforeStories
public void init() {
throw new RuntimeException("omg");
}
I would like to have:
java.lang.RuntimeException: omg
at com.mycompany.MyClass.init(MyClass.java:69)
Is that possible?
I'm no sure I understand what you're trying to achieve since throwing the exception should already log a failure to console.
but if you wanna just print out the exception message and tracestack to console you can do the line below although it's pretty terrible it should give you what you want:
#BeforeStories
public void runBeforeAllStories() {
new RuntimeException("omg").printStackTrace();
}
the application will continue running but will output the text below in console:
java.lang.RuntimeException: omg at
jbehave.sample.steps.LifecycleSteps.runBeforeAllStories(LifecycleSteps.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.jbehave.core.steps.StepCreator$MethodInvoker.invoke(StepCreator.java:805)
at
org.jbehave.core.steps.StepCreator$BeforeOrAfterStep.perform(StepCreator.java:491)
at
org.jbehave.core.embedder.PerformableTree$FineSoFar.run(PerformableTree.java:340)
at
org.jbehave.core.embedder.PerformableTree$PerformableSteps.perform(PerformableTree.java:1072)
at
org.jbehave.core.embedder.PerformableTree.performBeforeOrAfterStories(PerformableTree.java:427)
at
org.jbehave.core.embedder.StoryManager.performStories(StoryManager.java:117)
at
org.jbehave.core.embedder.StoryManager.runStories(StoryManager.java:107)
at
org.jbehave.core.embedder.StoryManager.runStoriesAsPaths(StoryManager.java:86)
at
org.jbehave.core.embedder.Embedder.runStoriesAsPaths(Embedder.java:213)
at jbehave.sample.common.StoriesTest.run(StoriesTest.java:161) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at
org.junit.runners.ParentRunner.run(ParentRunner.java:309) at
org.junit.runner.JUnitCore.run(JUnitCore.java:160) at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
first of all I appreciate that you want to show your exception in a funny way. You can express your exception like just adding throws Exception to the #BeforeStories method as below.
#BeforeStories
public void init() throws Exception{
throw new RuntimeException("omg");
}

Guava map storing class instance as key and int value

I am trying to use Guava's collections to create a map that will store a class instance as the key and an Integer number as the value. So something like this:
Key - Value
FooOne - 1
FooTwo - 2
FooThree - 3
FooFour - 4
I have this in the code right now:
private ClassToInstanceMap<Object> classRanking = MutableClassToInstanceMap.create();
classRanking.put(FooOne.class, 0);
classRanking.put(FooTwo.class, 1);
classRanking.put(FooThree.class, 2);
classRanking.put(FooFour.class, 3);
But that throws an error when run:
java.lang.ClassCastException: Cannot cast java.lang.Integer to com.jer.abc.test.FooOne
at java.lang.Class.cast(Unknown Source)
at com.google.common.collect.MutableClassToInstanceMap.cast(MutableClassToInstanceMap.java:82)
at com.google.common.collect.MutableClassToInstanceMap.access$000(MutableClassToInstanceMap.java:36)
at com.google.common.collect.MutableClassToInstanceMap$1.checkKeyValue(MutableClassToInstanceMap.java:67)
at com.google.common.collect.MutableClassToInstanceMap$1.checkKeyValue(MutableClassToInstanceMap.java:64)
at com.google.common.collect.MapConstraints$ConstrainedMap.put(MapConstraints.java:313)
at com.fmr.gps.web.support.PortfolioInsightsSupport.populateRankingMap(PortfolioInsightsSupport.java:33)
at com.fmr.gps.web.support.PortfolioInsightsSupport.<init>(PortfolioInsightsSupport.java:29)
at com.fmr.gps.web.PortfolioInsightsSupportTest.setUp(PortfolioInsightsSupportTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
So I was thinking about using a plain old java map but this thread tells me to do otherwise. So any ideas on how to fix this?
ClassToInstanceMap is for when you want to do stuff like classRanking.put(FooOne.class, new FooOne()), i.e. the values will be instances of the key. But 0 is not an instance of FooOne, it's just an Integer. You just want a normal Map<Class<?>, Integer>, so use a HashMap or something.

Cannot Catch arithmetic exception in junit

the method process is really throwing the exception however when I test this in junit is not working eventhough I placed next to the #Test annotation the expected exception to be thrown the test fails why is this happening?
java.lang.ArithmeticException: / by zero
at com.javageek.junit.Calculator.process(Calculator.java:88)
at com.javageek.junit.CalculatorTest.CalculatorTestDivisionByZero(CalculatorTest.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
#Test(expected=ArithmeticException.class)
public void CalculatorTestDivisionByZero(){
Calculator calculator = new Calculator();
calculator.process("DivisionByZero.txt");
}
You need to specify the Runner for your test class as:
#RunWith(JUnit4.class)
Plus your test function probably should not start with a capital letter (CalculatorTestDivisionByZero).
The following works for me (i.e. the test passes):
#Test(expected=ArithmeticException.class)
public void CalculatorTestDivisionByZero(){
int i = 23 / 0;
System.out.println("i=" + i);
}
I don't need lowercase methods or #RunWith.
Maybe your calculator is starting a different thread or process? Then the Exception wouldn't be thrown in the test Thread.
You could also check whether you are importing the correct ArithmeticException class, there may be other ones provided by your application.

Conflicting setter definitions for property "thenComparing":

I'm getting the following exception when attempting to deserialize an object that uses a builder. I'm not sure what it's referring to by the thenComparing property. I will say that the builder is using the #JsonPOJOBuilder(withPrefix = "") annotation.
com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "thenComparing": java.util.Comparator#thenComparing(1 params) vs java.util.Comparator#thenComparing(1 params)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:266)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:241)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:394)
at com.fasterxml.jackson.databind.ObjectReader._findRootDeserializer(ObjectReader.java:1379)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1264)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:896)
at com.theexceptioncatcher.ProjectStocks.SharedLibrary.JSONDailyResultTest.deserialization(JSONDailyResultTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "thenComparing": java.util.Comparator#thenComparing(1 params) vs java.util.Comparator#thenComparing(1 params)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:303)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.filterBeanProps(BeanDeserializerFactory.java:629)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:527)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBuilderBasedDeserializer(BeanDeserializerFactory.java:322)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBuilderBasedDeserializer(BeanDeserializerFactory.java:180)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:341)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:261)
The issue with this is that the filter for the setters allowed for everything in. It turned out there were multiple definitions for "thenComparing" which caused this issue.

Whats wrong with this junit test case?

I am trying to setup tests using jUnit in my application. This is my first experience doing it.
Here is my test case:
public class TestClass
{
StudentDAO s= new StudentDAO();
#Test
public void testStudentsInQFG()
{
Assert.assertEquals(s.getStudentCountFromDB("1"),10);
}
}
Here is the Student DAO method:
public int getStudentCountFromDB(String CourseID)
{
String sqlCount="SELECT Count(*) FROM Student WHERE CourseID=?";
return this.getMySQLJDBCTemplate().queryForObject(sqlCount,new Object[] {CourseID}, Integer.class);
}
The method returns the right results when I call it from my Main method but the fails when I use the assertEquals. Here is my stack trace:
java.lang.NullPointerException
at com.omnitracs.fra.dao.StudentDAO.testStudentsInQFG(StudentDAO.java:107)
at com.omnitracs.fra.junit.TestClass.testStudents(TestClass.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
It looks like when StudentDao is calling your method getMySQLJDBCTemplate(), this method returns null (or directly throws an exception because it tries to access to access a variable which is not yet initialized).
You probably have to call have a method like setMySQLJDBCTemplate(jdbcTemplate) in your DAO, with a correctly initialized jdbcTemplate or a Mock (you can have a look at the library Mockito for that). You need to call it first in your test, to set the mysql template, before trying to access the DB. You may need to initialize as well other fields of your DAO.

Categories