I’ve been trying to understand how to use DiffBlue on my code base, starting with the IDEA plugin. I “upgraded” to a trial license of the “full” version, hoping I’d get better results, but am really having a hard time getting any kind of usable tests out of DB:
I’m trying to create a set of tests for a Spring service. I click on the DiffBlue icon in the IDE margin. DiffBlue spits out a bunch of broken tests with the comment:
// Diffblue Cover was unable to write a Spring test,
// so wrote a non-Spring test instead.
// Reason: R026 Failed to create Spring context.
// Attempt to initialize test context failed with
// java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
// at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
// at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
// at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
// at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
// at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
// at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
// at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
// at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
// at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:543)
// See https://diff.blue/R026 to resolve this issue.
If I change the @SpringBootConfiguration
annotation:
@SpringBootTest
@ExtendWith(SpringExtension.class)
class LocationManagementServiceTest {
on the test to
@ContextConfiguration(classes = {LocationManagementService.class})
@ExtendWith(SpringExtension.class)
class LocationManagementServiceTest {
I get a bunch of failures when running the tests, and have to manually add a:
@MockBean
FooBar foobar;
For each org.springframework.beans.factory.NoSuchBeanDefinitionException
as the ApplicationContext fails to initialize for each test.
When I look at the DB documentation for R026 it says:
The first step to resolving an R026 is to ensure you have an active Spring profile, e.g. “test” or “dev”, if available. The second step to is to check if there are any missing environment variables or system properties. These two steps are the most common resolutions for failures by Diffblue Cover in creating Spring contexts.
But it’s unclear to me what “ensure you have an active Spring profile” means in this context.
Hi Ian,
Thanks for raising this - please can you provide the logfile?
There are many reasons that the spring context cannot be loaded; the logfile will show the test that Diffblue Cover wrote but didn’t work; you can also try to manually run this test to see what Spring context loading error you get.
To find the log file follow this documentation for CLI, and this for IntelliJ
Also, thank you for the feedback on the documentation for R026, I will pass this on.
Kind regards,
Jane @ Diffblue
Sure, if I just hit the icon, this is the test class declaration and the first few lines of the first test created.
@SpringBootTest
@ExtendWith(SpringExtension.class)
class LocationManagementServiceTest {
@Autowired
private LocationManagementService locationManagementService;
/**
* Method under test: {@link LocationManagementService#locationExists(String)}
*/
@Test
void testLocationExists() {
// Diffblue Cover was unable to write a Spring test,
// so wrote a non-Spring test instead.
// Reason: R026 Failed to create Spring context.
// Attempt to initialize test context failed with
// java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test
[…]
user.log (6.5 MB)
Hi Ian,
Thank you for sending that - apologies are you able to please send the support.log
file for us to inspect, as this has more information for me to have a deeper dive into the behaviour.
I have had feedback regarding the meaning behind the documentation - this is regarding declaring the Spring profiles in IntelliJ. If you haven’t already done this, you can add the active profiles in IntelliJ by clicking Run
in the top panel > then Edit Configurations
.
In the pop up, click Modify options
> then tick Add VM options
> then as in the red box below fill in -Dspring.profiles.active={name.of.profile}
Please let me know if you have any questions with this
Kind regards,
Jane @ Diffblue
Hi Jane,
I’m a little confused by the request to add a spring profile to the application. While I do have a test profile (and a dev profile, and a prod profile in production), I am generating tests via the IDEA plugin. So I’m clicking on the little “Erlenmeyer flask” icon that appears next to the target class declaration. It doesn’t appear to make any difference whether I select a profile–in fact, it doesn’t seem like DiffBlue uses that information at all when generating tests.
Hi @ibcoleman
Apologies for the confusion, these are instructions on how to declare existing Spring Profiles in your project to IntelliJ. Alternatively, you can declare the Spring Profile solely to Cover in the Diffblue settings. The option can be seen by clicking Diffblue > Change Settings > Under the Spring section there is Active Profiles - documentation found here.
We also have some more general Spring profile documentation which describes the former option found here.
The reason of mentioning this here is Cover requires that the Spring Profile(s) is/are declared to find the correct Spring context, so could help in this situation. In your message, when you say here you have the profiles, have these profiles been declared?
I hope this helped provide some more context to my previous message, please let me know if you need any further information.
Kind regards,
Jane @ Diffblue