jOOQ 3.5.0 I’m currently trying to write unit tests for a resource that is using jOOQs generated DAO objects. I’ve noticed one of the base classes (DAOImpl) in the DAO hierarchy has many final methods which makes it unfriendly to mock (I’m excluding byte code manipulators like Powermock as a solution). I’m currently using the MockConnection and MockDataProvider pattern to
Tag: mocking
Mockito; verify method was called with list, ignore order of elements in list
I have a class (ClassA) that get the files in a directory. It scans the given directory for files matching a regex. For each matching file, it adds a File Object to a list. Once the directory is processed, it passes the List of Files to another Class (ClassB) for processing I am writing unit tests for ClassA, so am
How do I unit test spring security @PreAuthorize(hasRole)?
What do I need in order to unit test the hasRole part of a PreAuthorize annotation on a controller method? My test should succeed because the logged in user only has one of the two roles, but instead it fails with the following assertion error: java.lang.AssertionError: Status Expected :401 Actual :200 I have the following method in MyController: I created
Mocking static methods with Mockito
I’ve written a factory to produce java.sql.Connection objects: I’d like to validate the parameters passed to DriverManager.getConnection, but I don’t know how to mock a static method. I’m using JUnit 4 and Mockito for my test cases. Is there a good way to mock/verify this specific use-case? Answer Use PowerMockito on top of Mockito. Example code: More information: Why doesn’t
Mocking a Spy method with Mockito
I am writing a unit test for a FizzConfigurator class that looks like: I’d like to write a simple unit test that stubs the doWidget(String,Config) method (so that it doesn’t actually fire and hit the database), but that allows me to verify that calling doBuzz(String) ends up executing doWidget. Mockito seems like the right tool for the job here. This
Mock a constructor with parameter
I have a class as below: The logic in the constructor A(String test) and check() are the things I am trying to mock. I want any calls like: new A($$$any string$$$).check() returns a dummy string “test”. I tried: But it doesn’t seem to be working. new A($$$any string$$$).check() is still going through the constructor logic instead of fetch the mocked
Using Mockito with multiple calls to the same method with the same arguments
Is there a way to have a stubbed method return different objects on subsequent invocations? I’d like to do this to test nondeterminate responses from an ExecutorCompletionService. i.e. to test that irrespective of the return order of the methods, the outcome remains constant. The code I’m looking to test looks something like this. Answer You can do that using the
Verifying partially ordered method invocations in JMockit
I’m trying to write a unit test (using JMockit) that verifies that methods are called according to a partial order. The specific use case is ensuring that certain operations are called inside a transaction, but more generally I want to verify something like this: Method beginTransaction is called. Methods operation1 through to operationN are called in any order. Method endTransaction
Using Mockito’s generic “any()” method
I have an interface with a method that expects an array of Foo: I am mocking this interface using Mockito, and I’d like to assert that doStuff() is called, but I don’t want to validate what argument are passed – “don’t care”. How do I write the following code using any(), the generic method, instead of anyObject()? Answer Since Java
Mocking a URL in Java
We have a URL object in one of our Java classes that we want to mock, but it’s a final class so we cannot. We do not want to go a level above, and mock the InputStream because that will still leave us with untested code (we have draconian test coverage standards). I’ve tried jMockIt’s reflective powers but we work