Complex mocking/test set up

Hi
I am evaluating cover for my team.
We own a large codebase - we already have many unit tests. Our system is build in a way that some of the tests require more complex mocking/setups, we use powermock, static mocking etc.
How can I incorporate this? is there a way I can build the test “skeleton” with all the required mocking and tell cover to do the actual test implementation?

Hello @kleinroy ,

Thank you for reaching out with your query.

There are a couple of options I believe could be considered in relation to mocking setups:

  1. Mocking Annotations
  2. CLI Mock Commands - in combination with a Diffblue arguments file
  3. DiffblueBase Class

The annotations and CLI commands can be applied directly to a class and provide certain mocking/static mocking instructions. The base class allows additional flexibility setting up more extensive mocking instructions.

Please note:

  1. Diffblue Cover Annotations library dependency will need to be incorporated into the project
  2. Cover utilises Mockito for mocking capabilities. This may conflict with the mocking setup of the project utilising PowerMock.

Cover will provide a warning if a conflict is identified between Mockito and PowerMock. This can include recommendations on library updates and withdrawing PowerMock in favour of Mockito.

Please let me know if I can provide any additional assistance with your Cover/mocking assessment.

Kind Regards,
Shaun

Thanks,
Is using PowerMock not valid for Cover? we are using PowerMock capabilities extensively.

Hi @kleinroy ,

Presently Cover has limited support for PowerMock and favours use of Mockito. It is still possible for unit tests to be produced, but these may be fewer where code uses the PowerMock library.

I hope that helps provide a little further clarity.

Kind Regards,
Shaun

Thanks
For Diffblue base test class:

  1. Do I need to add Diffblue Cover Annotations library dependency will need to be incorporated into the project
  2. What is the base test class path? src or src/test (with all the package heirarchy)?
    Thanks

Hi @kleinroy

The Diffblue Cover annotations library will not be necessary for utilising the DiffblueBase class. Incorporating the library would be necessary if custom annotation options are utilised.

The DiffblueBase class must include the same name as the class under test, with “DiffblueBase” appended. This should then be located within the equivariant src/test package hierarchy directory, for example if the class under test was: src/main/java/com/example/UserClass.java, the associating DiffblueBase class should be: src/test/java/com/example/UserClassDiffblueBase.java.

Please do reach out if you require any additional information or assistance setting up your base class.

Kind Regards,
Shaun

Thanks @Shaun_Dodd
I tried that and it didn’t work.
Worth mentioning that:

  1. My ***DiffuseBase class is using PowerMockito with @PrepareForTest
  2. My ***DiffuseBase class has some Google Guice stuff in it and the Intelij plugin provide the following warnings:

The following dependencies are incompatible with the current JDK (JDK version: 21.0.6). Consider first fixing this first before tackling any of the other warnings. Please change the following dependencies in your project:
testImplementation(“org.mockito:mockito-core:5.16.0”)

Libraries are on the classpath that have only limited support by Diffblue Cover:
Google Guice 7.0.0
Jakarta XML WS API 4.0.2
PowerMock 2.0.9

Thanks!

Hi @kleinroy ,

Sorry to hear the base class did not work for your instance.

With Mockito being favoured for mocking behaviour it is one of our general dependency libraries. It would be recommended to adjust the version in line with the advisement, though I appreciate the crossover with PowerMock and its version as you mention, which may constrain the Mockito version options.

Additional information on general dependencies is available on our documentation pages [here]

If you would be interested in applying a Mockito direct use case to a sample project for your comparison and assessment, and applying a base class to this, I would be happy to assist you with the setup. This could potentially be via a sample project you are perhaps using for your assessment (I could provide a secure link to collaborate on (nothing proprietary)), or perhaps a separate public project that is close to your use case.

With regards to the limited support libraries, we are progressively expanding Cover’s library support. Incorporation for Google Guice is one such library that is progressing and support is anticipated to be included within the next couple of releases.

Kind Regards,
Shaun

Thanks.
It’s just not that realistic to check everything without PowerMock and the newest Mockito since we will need to change significant parts of our application to make the tests work and we want to test AI UnitTest tools in our real environment.

I don’t have a sample project right now - we are testing this on our current code base, I can try and find a public project or start a fresh one though it might consume some time I am not sure I can spend right now, but I am open for suggestion.

Thanks,
Royi

I can transform most of my tests to use Mockito static mock - the problem is that Cover doesn’t know how to utilise static mock when needed by itsled and since Mockito’s static mock is per test using try-with-resource it cannot be provided by a base class (like I can do with PowerMockito which I can do all the static preparations in a @Before method) - and ideas?

You can emulate try-with-resources on @Before methods like this (beware, this is a bit hacky):

public DiffblueBase {
  private MockedStatic<MockedClass> staticMockHelper;
  @BeforeEach
  void setUpStaticMocks() {
    staticMockHelper = Mockito.mockStatic(MockedClass.class);
    // set up things here
  }
  
  @AfterEach
  void tearDownStaticMocks() {
    staticMockHelper.close();
  }
}

(this may need some slight adjustment).

Yes it works, cool. Problem is that Cover doesn’t know how to utilize mock members from the Base class

Hi @kleinroy,

It sounds as though progress has been made, but perhaps some fine tunning is necessary.

Would it be possible to share an example of the issue encountered and the base class created?

I can provide a email address to share this information if preferred?

Kind Regards,
Shaun