mockserver
mockserver copied to clipboard
Build jar with maven spring-boot-maven-plugin, report ClassNotFoundException when using ExpectationResponseCallback
Describe the issue Build jar with maven spring-boot-maven-plugin, report ClassNotFoundException when using ExpectationResponseCallback, but when i run same case in IDE ,it's correct
What you are trying to do
MockServer version
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>5.13.2</version>
</dependency>
To Reproduce Steps to reproduce the issue:
- How you are running MockServer (i.e maven plugin, docker, etc)
@Bean
public ClientAndServer mockServer(){
String rootPath = System.getProperty("user.dir");
ConfigurationProperties.initializationJsonPath( rootPath+ "/config/mock.json");
ConfigurationProperties.logLevel("INFO");
ConfigurationProperties.disableSystemOut(false);
ConfigurationProperties.launchUIForLogLevelDebug(true);
ConfigurationProperties.watchInitializationJson(true);
return startClientAndServer(11080);
}
- Code you used to create expectations mock.json
{
"httpRequest": {
"path": "****",
"method": "POST"
},
"httpResponseClassCallback": {
"callbackClass": "com.zchz.mock.MyExpectationResponseCallback"
}
}
ExpectationResponseCallback class
package com.zchz.mock;
import com.alibaba.fastjson.JSONObject;
import com.icbc.api.utils.HttpRequestParamsUtils;
import lombok.extern.slf4j.Slf4j;
import org.mockserver.mock.action.ExpectationResponseCallback;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.model.HttpStatusCode;
import org.mockserver.model.MediaType;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
import static org.mockserver.model.Header.header;
@Slf4j
public class MyExpectationResponseCallback implements ExpectationResponseCallback {
MockProperties properties;
RedisService redisService;
public MyExpectationResponseCallback(){
properties = SpringUtil.getBean(MockProperties.class);
redisService = SpringUtil.getBean(RedisService.class);
}
@Override
public HttpResponse handle(HttpRequest httpRequest) throws Exception {
JSONObject respBody = new JSONObject();
return HttpResponse.response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withHeaders(
header("Content-Length", respBody.toJSONString().getBytes(StandardCharsets.UTF_8).length),
header("Connection", "keep-alive")
)
.withContentType(MediaType.APPLICATION_JSON_UTF_8).withBody(respBody.toJSONString());
}
}
build with
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
<outputDirectory>${package.output}</outputDirectory>
<mainClass>com.zchz.mock.TestApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>`
run with command
java -jar .\mockServer-1.0-SNAPSHOT.jar --spring.config.location=application.properties -Djava.util.logging.config.file=logging.properties
- What error you saw when request the interface,return stateCode 404
Expected behaviour
match expectation and return
MockServer Log log report:
matched expectation:
{
"httpRequest" : {
"method" : "POST",
"path" : "*****"
},
"httpResponseClassCallback" : {
"callbackClass" : "com.zchz.mock.MyExpectationResponseCallback"
},
"id" : "0a6c66db-80c4-30e6-aebb-84f68073cfe8",
"priority" : 0,
"timeToLive" : {
"unlimited" : true
},
"times" : {
"unlimited" : true
}
}
2022-07-20 17:43:49 SEVERE 11080 ClassNotFoundException - while trying to instantiate ExpectationResponseCallback class "com.zchz.mock.MyExpectationResponseCallback " ```