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.
Related
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)
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");
}
Our system can run over both web platform and Windows platform. We created tests for the web platform by Cucumer, Selenium, and JUnit and it works fine but very slow.
We decided to execute the same tests on the Windows platform, and created a program which integrates with it, operated by the JUnit. It works very very faster, but we have a problem.
Most cases when AssertTrue has to fail the test, it actually calls fail(), but the test continues running to the next sentence. Moreover, the output of the fail() is actually printed.
Why does it occur?
EDIT: I don't know what can be clearer than what I wrote, but you want:
assertTrue("retval = " + params[0], params[0].equals("0"));
When params[0] = "1018" the test is supposed to stop with failure. But it continues to the next code instruction.
The output is:
java.lang.AssertionError: retval = 1026
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.assertTrue(Assert.java:41)
at com.eshbel_17_0.WinformBridge.readToJava(WinformBridge.java:225)
at com.eshbel_17_0.WinformExec.runDirectProcedure(WinformExec.java:111)
at com.eshbel_17_0.Definitions.the_user_runs_the_direct_activation(Definitions.java:362)
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 cucumber.runtime.Utils$1.call(Utils.java:34)
at cucumber.runtime.Timeout.timeout(Timeout.java:13)
at cucumber.runtime.Utils.invoke(Utils.java:30)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:35)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:298)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:48)
at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:91)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
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 cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:89)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:40)
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 cucumber.api.junit.Cucumber.run(Cucumber.java:94)
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: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)
Oh, I found why it happens, and it's so foolish mistake!
This code is sorrounded by try-catch block, and gets also Throwable e. it includes also Assertion Error exception, so test not failed.
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.
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.