我为控制器类创建了一个JUnit测试。此类下有两个测试1.有效的客户请求2.错误的请求
我对etPeriodicSchedule.createJobSchedule()
进行了存根分析,以便为每个测试返回不同的值。但是我遇到的问题是,第一个测试的存根结果也会返回第二个测试。
注意:这些测试在Spring STS中按预期工作,但在Maven命令行中出现问题。
REST端点:
public @ResponseBody Object createJobSchedule(@RequestBody ETPeriodicJobScheduleDTO jobScheduleDTO) {
try {
Object obj = etPeriodicSchedule.createJobSchedule(jobScheduleDTO);
if(obj instanceof JobScheduleDTO) {
return ResponseEntity.ok().body((JobScheduleDTO)obj);
}
else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(obj);
}
} catch (Exception e) {
log.error("Exception occured while creating job schedule", e);
throw new IllegalArgumentException(EmploymentTaxJobScheduleConstants.CREATE_JOB_SCHDL_RUN_TIME_EXCEPTION_MSG);
}
}
JUnit测试:
@Slf4j
public class JobSchedulerControllerTest extends BaseUnitTest {
@InjectMocks
JobScheduleController controller;
@MockBean
EmploymentTaxPeriodicScheduleService etPeriodicSchedule;
private String etPeriodicJobScheduleJson;
private String etPeriodicJobScheduleBadReqJson;
@Before
@Override
public void setUp() {
super.setUp();
Mockito.clearInvocations(etPeriodicSchedule);
etPeriodicJobScheduleJson = createJsonString("et_periodic_job_schedule.json");
etPeriodicJobScheduleBadReqJson = createJsonString("et_periodic_job_schedule_bad_req.json");
}
private String createJsonString(String filename) {
StringBuilder jsonRequest = new StringBuilder();
try(InputStream is = JobSchedulerControllerTest.class.getClassLoader().getResourceAsStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
String line;
while((line = br.readLine()) != null) {
jsonRequest.append(line);
}
} catch (Exception e) {
log.error("Exception while reading file", e);
}
return jsonRequest.toString();
}
@Test
public void testCreateJobSchedule() throws Exception {
Mockito.when(etPeriodicSchedule.createJobSchedule(any(ETPeriodicJobScheduleDTO.class))).thenReturn(new JobScheduleDTO());
RequestBuilder requestBuilder = post("/jobSchedule").accept(MediaType.APPLICATION_JSON)
.content(etPeriodicJobScheduleJson).contentType(contentType);
MvcResult mvcResult = mvc.perform(requestBuilder).andReturn();
System.out.println("==========================Printing response");
System.out.println(mvcResult.getResponse().getContentAsString());
int status = mvcResult.getResponse().getStatus();
assertEquals(200, status);
}
@Test
public void testCreateJobSchedule_bad_request() throws Exception {
List<ErrorResponseDTO> errors = new ArrayList<>();
errors.add(SchedulerUtils.createErrorResponseDTO(null, "Invalid data"));
Mockito.when(etPeriodicSchedule.createJobSchedule(any(ETPeriodicJobScheduleDTO.class))).thenReturn(errors);
RequestBuilder requestBuilder = post("/jobSchedule").accept(MediaType.APPLICATION_JSON)
.content(etPeriodicJobScheduleBadReqJson).contentType(contentType);
MvcResult mvcResult = mvc.perform(requestBuilder).andReturn();
System.out.println("==========================Printing response");
System.out.println(mvcResult.getResponse().getContentAsString());
int status = mvcResult.getResponse().getStatus();
assertEquals(400, status);
}
}
第二个测试'testCreateJobSchedule_bad_request()'在命令行中失败,因为实际结果是200,而不是400。有人可以帮忙什么地方吗?
编辑:
Please maven dependency tree below
[INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ scn-scheduler ---
[INFO] com.adp.scn.scheduler:scn-scheduler:war:1.0.0-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.1.0.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.1.0.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:2.1.0.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.0.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.0.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.1:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.1:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.23:runtime
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.1.0.RELEASE:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.7:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.7:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.7:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.0.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.12:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.12:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.12:compile
[INFO] | +- org.hibernate.validator:hibernate-validator:jar:6.0.13.Final:compile
[INFO] | | \- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] | +- org.springframework:spring-web:jar:5.1.2.RELEASE:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.1.2.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.1.2.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:5.1.2.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:5.1.2.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:5.1.2.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.1.0.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:2.1.0.RELEASE:compile
[INFO] | | \- org.aspectj:aspectjweaver:jar:1.9.2:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.1.0.RELEASE:compile
[INFO] | | +- com.zaxxer:HikariCP:jar:3.2.0:compile
[INFO] | | \- org.springframework:spring-jdbc:jar:5.1.2.RELEASE:compile
[INFO] | +- javax.transaction:javax.transaction-api:jar:1.3:compile
[INFO] | +- javax.xml.bind:jaxb-api:jar:2.3.1:compile
[INFO] | | \- javax.activation:javax.activation-api:jar:1.2.0:compile
[INFO] | +- org.hibernate:hibernate-core:jar:5.3.7.Final:compile
[INFO] | | +- javax.persistence:javax.persistence-api:jar:2.2:compile
[INFO] | | +- net.bytebuddy:byte-buddy:jar:1.9.3:compile
[INFO] | | +- antlr:antlr:jar:2.7.7:compile
[INFO] | | +- org.jboss:jandex:jar:2.0.5.Final:compile
[INFO] | | +- org.dom4j:dom4j:jar:2.1.1:compile
[INFO] | | \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.4.Final:compile
[INFO] | +- org.springframework.data:spring-data-jpa:jar:2.1.2.RELEASE:compile
[INFO] | | +- org.springframework.data:spring-data-commons:jar:2.1.2.RELEASE:compile
[INFO] | | +- org.springframework:spring-orm:jar:5.1.2.RELEASE:compile
[INFO] | | \- org.springframework:spring-tx:jar:5.1.2.RELEASE:compile
[INFO] | \- org.springframework:spring-aspects:jar:5.1.2.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.1.0.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.1.0.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.1.0.RELEASE:test
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO] | | \- net.minidev:json-smart:jar:2.3:test
[INFO] | | \- net.minidev:accessors-smart:jar:1.2:test
[INFO] | | \- org.ow2.asm:asm:jar:5.0.4:test
[INFO] | +- junit:junit:jar:4.12:test
[INFO] | +- org.assertj:assertj-core:jar:3.11.1:test
[INFO] | +- org.mockito:mockito-core:jar:2.23.0:test
[INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.9.3:test
[INFO] | | \- org.objenesis:objenesis:jar:2.6:test
[INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-core:jar:5.1.2.RELEASE:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.1.2.RELEASE:compile
[INFO] | +- org.springframework:spring-test:jar:5.1.2.RELEASE:test
[INFO] | \- org.xmlunit:xmlunit-core:jar:2.6.2:test
[INFO] +- io.springfox:springfox-swagger-ui:jar:2.4.0:compile
[INFO] | \- io.springfox:springfox-spring-web:jar:2.4.0:compile
[INFO] +- io.springfox:springfox-swagger2:jar:2.4.0:compile
[INFO] | +- io.swagger:swagger-annotations:jar:1.5.6:compile
[INFO] | +- io.swagger:swagger-models:jar:1.5.6:compile
[INFO] | +- io.springfox:springfox-spi:jar:2.4.0:compile
[INFO] | | \- io.springfox:springfox-core:jar:2.4.0:compile
[INFO] | +- io.springfox:springfox-schema:jar:2.4.0:compile
[INFO] | +- io.springfox:springfox-swagger-common:jar:2.4.0:compile
[INFO] | +- com.google.guava:guava:jar:18.0:compile
[INFO] | +- com.fasterxml:classmate:jar:1.4.0:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] | +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
[INFO] | \- org.springframework.plugin:spring-plugin-metadata:jar:1.2.0.RELEASE:compile
[INFO] +- org.projectlombok:lombok:jar:1.18.2:provided
[INFO] +- org.apache.commons:commons-lang3:jar:3.8.1:compile
[INFO] +- org.apache.commons:commons-collections4:jar:4.0:compile
[INFO] +- pl.pragmatists:JUnitParams:jar:1.1.0:test
[INFO] +- com.fasterxml.jackson.module:jackson-module-jsonSchema:jar:2.6.3:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.9.7:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.7:compile
[INFO] | \- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] +- com.oracle.jdbc:ojdbc7:jar:12.1.0.1.0:compile
[INFO] +- com.cyber.ark:javapasswordsdk:jar:5.50.75.0:compile
[INFO] +- org.json:json:jar:20180813:compile
[INFO] \- ma.glasnost.orika:orika-core:jar:1.5.4:compile
[INFO] +- org.javassist:javassist:jar:3.24.0-GA:compile
[INFO] +- com.thoughtworks.paranamer:paranamer:jar:2.8:compile
[INFO] +- com.carrotsearch:java-sizeof:jar:0.0.5:compile
[INFO] \- org.codehaus.janino:janino:jar:3.0.10:compile
[INFO] \- org.codehaus.janino:commons-compiler:jar:3.0.10:compile