Could not start test generator because Spring Boot Test could not be found in the classpath

I’m struggling to get the community IDEA plugin to generate a test. I thought I was starting with the most basic of projects by starting a new project from Spring Initializr. I’ve left the default settings apart from selecting Gradle instead of Maven for build. I’ve not selected any other dependencies.

I’ve written a simple class and tried to get Diffblue Cover to generate tests for it but get this error.

Could not start test generator because Spring Boot Test could not be found in the classpath.: We could not start Diffblue Cover because Spring Boot Test, which is documented as a requirement for Diffblue Cover, could not be found.

I’m not entirely sure what Diffblue Cover requires. In the Gradle build file it has these dependencies

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

and if I look into IDEA’s External Libraries node I can see Gradle: org.springframework.boot:spring-boot-test:2.4.0

Here’s the class I’m trying to test

package com.example.demo;

public class DummyService {

    public int addNumbers(int x, int y) {
        return x + y;
    }
}

I’m not sure if I’m missing something very obvious.
Many thanks,
Andy

Hi Andy, thanks for trying Diffblue Cover!

When creating a project on Spring initializr, I would recommend to add Spring Web as a dependency.

The problem that you are experiencing is due to the Gradle integration in IntelliJ: it creates separate source and test modules, rather than just source and test directories, as the Maven integration does.

We are working on a fix to recognise this properly in Diffblue Cover, but as a workaround you can either switch to Maven, where the IntelliJ integration works as expected, or follow the following steps:

  1. Create Spring project with Spring Web dependency on initializr.
  2. Import the project in IntelliJ.
  3. Go to File -> Project Structure -> Modules.
  4. Select main from the module tree and select the tab Dependencies
  5. Click on the + icon and select 2 Library.
  6. Add the following libraries:
  • Gradle: org.springframework.boot:spring-boot-test:2.4.0
  • Gradle: org.springframework.boot:spring-boot-starter-test:2.4.0
  • Gradle: org.springframework:spring-test:5.3.1
  • Gradle: net.bytebuddy:byte-buddy:1.10.18
  • Gradle: org.mockito:mockito-core:3.6.0
  1. Change the Scope of these 5 modules to Test.

After recompiling the project I was able to generate the following test for your dummy class:

public class DummyServiceTest {
    @Test
    public void testAddNumbers() {
        assertEquals(5, (new DummyService()).addNumbers(2, 3));
    }
}
1 Like

Thanks @pkesseli
That has worked for me. I did originally have the Spring Web as a dependency but I’d tried stripping things back to the bare minimum to try and get it to work.

I’m assuming this is an issue specific to the community plugin as it is IntelliJ creating separate modules to match its project structure and wouldn’t affect the CLI tool?

Hi Andy, you are welcome, I’m glad it worked.

That’s correct, the CLI is unaffected by this, and we also fixed the issue for the plug-in in our upcoming 2020.12.01 release.