我正在尝试设置一个非常简单的 Jersey 客户端示例。所以我从这样的 pom.xml 开始:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>simple-service-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-service-webapp</name>
<build>
<finalName>simple-service-webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.23</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Run Code Online (Sandbox Code Playgroud)
和这样的代码:
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("http://api.openweathermap.org/data/2.5/weather?q=berlin");
Invocation.Builder request = webTarget.request(MediaType.APPLICATION_JSON);
Response response = request.get();
System.out.println(response.getStatus());
String x = response.readEntity(String.class);
System.out.println(x); …Run Code Online (Sandbox Code Playgroud) 我正在使用Spock测试atm,我想知道这样的事情是否可能.我的方法不起作用,我想知道你们中是否有人有类似的意图,并找到了办法.
我想调用一个方法或一个闭包,它只能为每个相应的where子句调用,以便设置一些东西.我不能只调用所有这些方法,因为它会破坏我的测试.我到目前为止找到的唯一方法是检查当前状态是什么,并在if语句中相应地调用方法:if(state == SomeStateEnum.FIRST_STATE){somePrivateMethodFromSpec()}但我不知道是否无法完成以更好的方式.我希望我的意图很明确(对不起,我不是母语人士)下面是一些示例代码,可能会更好地了解我想要做什么.先感谢您.
def 'is this even possible?'() {
when:
def resultState = service.someServiceMethod(param)
then:
resultState == state
where:
state | param | method
SomeStateEnum.FIRST_STATE | 'param1' | somePrivateMethodFromSpec()
SomeStateEnum.SECOND_STATE | 'param2' | someOtherPrivateMethodFromSpec()
}
private def somePrivateMethodFromSpec() {
someServiceMock.demand.AAA() {}
}
private def someOtherPrivateMethodFromSpec() {
someServiceMock.demand.BBB() {}
}
def 'or maybe something like this?'() {
when:
closure.call()
def resultState = service.someServiceMethod(param)
then:
resultState == state
where:
state | param | closure
SomeStateEnum.FIRST_STATE | 'param1' | {println '1'}
SomeStateEnum.SECOND_STATE …Run Code Online (Sandbox Code Playgroud) 所以我有一个可以为几个不同国家启动的应用程序,例如:mvn clean package -Dcountry=FRANCE resp。mvn clean package -Dcountry=GERMANY
对于不同的国家,我有不同的行为,尤其是在验证东西时。
所以我有这个包含国家相关验证器的类:
@Component
public class SomeValidatingClass {
private final Validator myCountrySpecificValidator;
@Autowired
public SomeValidatingClass(MyCountrySpecificValidator myCountrySpecificValidator) {
this.myCountrySpecificValidator = myCountrySpecificValidator;
}
public void doValidate(Object target, Errors errors) {
myCountrySpecificValidator.validate(target, errors);
}
}
Run Code Online (Sandbox Code Playgroud)
第一个国家相关验证器:
public class MyCountrySpecificValidator1 implements Validator {
@Override
public void validate(Object target, Errors errors) {
if (target == null) {
errors.rejectValue("field", "some error code");
}
}
}
Run Code Online (Sandbox Code Playgroud)
第二个国家相关验证器:让我们假设
public class MyCountrySpecificValidator2 implements Validator {
@Override
public void validate(Object target, Errors errors) …Run Code Online (Sandbox Code Playgroud)