spring-cloud / spring-cloud-contract

Support for Consumer Driven Contracts in Spring
https://cloud.spring.io/spring-cloud-contract
Apache License 2.0
720 stars 438 forks source link

Generated JUnit5 test contains unused imports #1292

Open arnosthavelka opened 4 years ago

arnosthavelka commented 4 years ago

I can see unused imports when I open generated test in my IDE:

import org.junit.jupiter.api.extension.ExtendWith;
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.*;

Sample My integration to contracts is like that:

<plugin>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-contract-maven-plugin</artifactId>
    <version>${spring-cloud.version}</version>
    <extensions>true</extensions>
    <configuration>
        <baseClassForTests>com.github.aha.poc.junit.springboot.AbstractContractTest</baseClassForTests> 
        <testFramework>JUNIT5</testFramework> 
    </configuration>
</plugin>

The test base class is defined as:

@SpringBootTest(webEnvironment = MOCK)
public class AbstractContractTest {

    @Autowired
    private WebApplicationContext context;

    @BeforeEach
    void setup() {
        webAppContextSetup(context);
    }
}

The complete example can be found here: https://github.com/arnosthavelka/junit-poc/tree/develop/spring-boot/src/test/resources/contracts/city.

marcingrzejszczak commented 4 years ago

Related branch - https://github.com/spring-cloud/spring-cloud-contract/tree/issues_%231292_unused_imports

it's not that easy actually with the current implementation since fromFile(...) can be passed in any spot. Maybe I'll have to switch to emitting events when some things got called. I'll have to think about it.

arnosthavelka commented 4 years ago

FYI it's nice to have feature :-). The unsued imports bothers me in IDE, but they are only in generated tests -> therefore no such a big deal.