Skip to content
Advertisement

Tag: mocking

jOOQ: Mocking DAO objects

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

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

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

Advertisement