I want to mock out a situation where if the service is called with a specific set of inputs, it should return a value, but if it's called with any other inputs throw an exception. So I've got:
doThrow(new ValidationException()).when(mockService).thing(any(), any());
when(mockService.thing(EXPECTED_PARAM_1, EXPECTED_PARAM_2).thenReturn(mockResult);
But when I go to run my test it throws the ValidationException on that second line where I'm creating the mock. It seems as though that second line is being treated as if I was actually calling the service, and since I'm mocking it with params that fit the any() any() it's throwing the exception rather than setting up the additional mock.
Thanks!
It turns out when.thenReturn actually calls the method it's mocking exactly once when the mock is first setup. Since the previous mock setup the default scenario where anytime the method is called it should thrown the exception, the second mock when it does its initial single call triggers the first mock and throws.
The solution to this scenario is to switch the second mock to use doReturn.when instead, as it never actually calls the method.
Related
I am new to unit test. I am trying to create test case of a method which is calling an static method. For this static method I am getting "getting missing behaviour definition for the preceding method call:"
EasyMock can't mock static methods. So that's probably the problem. But having actual failing code will help providing a full answer.
I created a custom component for a proprietary service. If this service is down i get noticed via a call of a callback function. I am throwing a custom exception at this point.
Sending exchanges to the producer/ consumer will yield no errors or exceptions (all seems to fine).
So i need to implement an emergency stop if my custom exception is thrown. I read a bit about exception handling in camel. I think i need a context-scoped onException(MyException.class).??? but what then?
Is this working on exceptions that are called without relation to an exchange? If this is working how to handle it. I want to stop certain routes in this case.
here you can find to stop routes from a route: http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html.
If you do the call of the proprietary service in a route you do have an exchange btw.
kind regards,
soilworker
I created a little workaround: I set a boolean i the callback method is called. On each call of process i check this boolean and if true i throw an exception.
With this the exception is within normal camel exception handling and onException could be used.
How to test a void method i.e. method that doesn't return anything in JMock?
To test a method that doesn't return anything, regardless of the testing or mocking framework you're using, you test the effect of a call to the method.
With JMock that likely means that you create a mock of something the code you're testing should call, set things up so that your mock is used instead of a real object, and set and verify expectations for calls to that mock.
I might be able to get more specific if you can add specifics to your question.
void methods generally make some changes in the value of the fields of the class. If the field of the class is not private then you can access it in your test class after calling the void method in your test method to assert if you are getting the expected value.
I have a method under test. Within its call stack, it calls a DAO which intern uses JDBC to chat with the DB. I am not really interested in knowing what will happen at the JDBC layer; I already have tests for that, and they work wonderfully.
I am trying to mock, using JMock, the DAO layer, so I can focus on the details this method under test. Here is a basic representation of what I have.
#Test
public void myTest()
{
context.checking(new Expectations() {
{
allowing(myDAO).getSet(with(any(Integer.class)));
will(returnValue(new HashSet<String>()));
}
});
// Used only to show the mock is working but not really part of this test.
// These asserts pass.
Set<String> temp = myDAO.getSet(Integer.valueOf(12));
Assert.assertNotNull(temp);
Assert.assertTrue(temp.isEmpty());
MyTestObject underTest = new MyTestObject();
// Deep in this call MyDAO is initialized and getSet() is called.
// The mock is failing to return the Set as desired. getSet() is run as
// normal and throws a NPE since JDBC is not (intentionally) setup. I want
// getSet() to just return an empty set at this layer.
underTest.thisTestMethod();
...
// Other assertions that would be helpful for this test if mocking
// was working.
}
It, from what I have learned creating this test, that I cannot mock indirect objects using JMock. OR I am not seeing a key point. I'm hoping for the second half to be true.
Thoughts and thank you.
From the snippet, I'm guessing that MyTestObject uses reflection, or a static method or field to get hold of the DAO, since it has no constructor parameters. JMock does not do replacement of objects by type (and any moment now, there'll be a bunch of people recommending other frameworks that do).
This is on purpose. A goal of JMock is to highlight object design weaknesses, by requiring clean dependencies and focussed behaviour. I find that burying DAO/JDBC access in the domain objects eventually gets me into trouble. It means that the domain objects have secret dependencies that make them harder to understand and change. I prefer to make those relationships explicit in the code.
So you have to get the mocked object somehow into the target code. If you can't or don't want to do that, then you'll have to use another framework.
P.S. One point of style, you can simplify this test a little:
context.checking(new Expectations() {{
allowing(myDAO).getSet(12); will(returnValue(new HashSet<String>()));
}});
within a test, you should really know what values to expect and feed that into the expectation. That makes it easier to see the flow of values between the objects.
I"m a bit confused because FlexUnith 4's behavior. When I use fail() in try-catch body fail method is just ignored.
[Test]
public function extend():void
{
try {
fail("This should fail");
} catch(er:Error) {}
}
I suppose this one should fail as there is no way around it, but it succeeds and turns green. Whatam I doing wrong? When i put fail() before try-catch block it fails as it is suposed to. BTW using Flash builder 4.
The way assertions are signaled to the framework is through exceptions. fail sends the failure signal using an exception too. That, and the fact that Error is the base class for all exceptions means that no exception will ever reach the framework (your try/catch block catches all excpetions), which means that the test didn't fail.