Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use MockWebServer with Junit 5? class file for org.junit.rules.ExternalResource not found #7987

Closed
jasonlo87 opened this issue Aug 18, 2023 · 4 comments
Labels
bug Bug in existing code

Comments

@jasonlo87
Copy link

jasonlo87 commented Aug 18, 2023

Following all the internet wisdom and guides I'm unable to get MockWebServer to play nice w/ Junit 5.

What is the recipe for getting this to work correctly without and error like this?
final var mockUrl = this.mockWebServer.url("/").toString();
^
class file for org.junit.rules.ExternalResource not found

Gradle dependencies are as follows:
dependencies {
// Platforms
implementation platform(SpringBootPlugin.BOM_COORDINATES)
implementation platform('software.amazon.awssdk:bom:2.20.109')

// Spring Dependencies, Boot version is 2.6.9
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-webflux'
implementation group: 'org.springframework.retry', name:'spring-retry'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-aop'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security'

// Misc 3rd Party Dependencies
implementation group: 'io.jsonwebtoken', name:'jjwt', version:'0.7.0'
implementation group: 'com.sun.mail', name: 'jakarta.mail'
implementation group: 'org.apache.httpcomponents', name:'httpclient'
implementation group: 'org.apache.commons', name: 'commons-lang3'

// Test Dependencies
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testImplementation group: 'org.mockito', name: 'mockito-core'

testImplementation group: 'com.squareup.okhttp3', name: 'mockwebserver', version: '4.11.0'
testImplementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.11.0'
}

test {
useJUnitPlatform()
}

@jasonlo87 jasonlo87 added the bug Bug in existing code label Aug 18, 2023
@yschimke
Copy link
Collaborator

yschimke commented Aug 18, 2023

OkHttp 5 alpha has a junit 5 module.

https://github.com/square/okhttp/tree/master/mockwebserver-junit5

@andreaippo
Copy link

andreaippo commented Aug 22, 2023

This is what fixed it for me:

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>mockwebserver3-junit5</artifactId>
            <version>5.0.0-alpha.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>5.0.0-alpha.11</version>
            <scope>test</scope>
        </dependency>

Be sure you don't have any JUnit 4 dependencies lying around.

@jasonlo87
Copy link
Author

This is what fixed it for me:

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>mockwebserver3-junit5</artifactId>
            <version>5.0.0-alpha.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>5.0.0-alpha.11</version>
            <scope>test</scope>
        </dependency>

Be sure you don't have any JUnit 4 dependencies lying around.

Huh ok this worked; thought I had tried that combo but something must've gotten lost in the sauce.

Using these dependencies my test now runs without error, thanks!

@andreaippo
Copy link

andreaippo commented Aug 22, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code
Projects
None yet
Development

No branches or pull requests

3 participants