我正在为Spring Boot Web服务开发一个测试用例,并且正在获得类似java.lang.NoSuchMethodError的错误:org.mockito.internal.util.MockUtil.getMockSettings(Ljava / lang / Object;)Lorg / mockito / mock / MockCreationSettings。
下面是Web服务代码,测试用例以及在测试用例中将FileUtils,AppContext(只是POJO)用作模拟bean的主要代码。
Web服务代码
@RestController
@RequestMapping("/resource")
public class ListResource {
/* Logger **/
private static final Logger _LOG = LoggerFactory.getLogger(ListResource.class);
@Autowired
private AppContext appContext;
@Autowired
private FileUtils fileUtils;
@Autowired
private ApplicationContext ctx;
private static final String DATE_FORMAT_FOR_ETAG = "yyyy-MM-dd HH:mm:ss'Z'";
private static final DateFormat eTagDateFormat = new SimpleDateFormat(DATE_FORMAT_FOR_ETAG, Locale.US);
private static final String ACCEPT_RANGES_BYTES = "bytes";
@RequestMapping(path = "/getlist", method = RequestMethod.HEAD)
public ResponseEntity<?> fetchGetListHead() throws IOException {
return fetchList(true);
}
@RequestMapping(path = "/getlist", method = RequestMethod.GET)
public ResponseEntity<?> fetchGetListGet() throws IOException {
return fetchList(false);
}
private ResponseEntity<?> fetchList(final boolean justHead) throws IOException {
file = fileUtils.lastZipFileWithPrefix( new File(appContext.getAppProperties().getZipDestDir()));
try {
byte[] readBytes = Files.readAllBytes(file.toPath());
ByteArrayResource resource = new ByteArrayResource(readBytes);
ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
.header(HttpHeaders.ACCEPT_RANGES, ACCEPT_RANGES_BYTES)
.lastModified(new Date().getTime())
.eTag(getETag(file))
.cacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS).cachePublic().mustRevalidate())
.contentLength(file.length())
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE));
return justHead ? responseBuilder.build() : responseBuilder.body(resource);
} catch (Exception e) {
_LOG.error("Error in gettingResource:{}", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
private String getETag(File file) {
String tag = "";
if (file != null) {
tag = eTagDateFormat.format(new Date(file.getAbsoluteFile().lastModified()));
}
return tag;
}
}
SpringBootTest代码
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ListResource.class)
@TestPropertySource(properties = { "zip.dest.dir=src/test/resources/zip/"})
@WebAppConfiguration
public class ListResourceTest {
@Autowired
private WebApplicationContext webApplicationContext;
@MockBean
private AppContext appContext;
@MockBean
private FileUtils fileUtils;
private MockMvc mockMvc;
@Value("${zip.dest.dir}")
private String zipDestDir;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
AppProperties appProperties = new AppProperties();
appProperties.setZipDestDir(zipDestDir);
when(appContext.getAppProperties()).thenReturn(appProperties);
this.mockMvc = webAppContextSetup(webApplicationContext).build();
}
@Test
public void testGetList() throws Exception {
this.mockMvc.perform(get("/resource/getlist")
.contentType(MediaType.APPLICATION_OCTET_STREAM_VALUE).content(zipDestDir))
.andExpect(status().isOk());
}
}
下面是主要课程
@SpringBootApplication
@PropertySources({
@PropertySource(value = "classpath:gateway.properties"),
})
public class GatewayApplicationContext extends SpringBootServletInitializer {
private static final Logger LOG = LoggerFactory.getLogger(GatewayApplicationContext.class);
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(GatewayApplicationContext.class, args);
}
@Bean
public static PropertySourcesPlaceholderConfigurer getPP() {
return new PropertySourcesPlaceholderConfigurer();
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(GatewayApplicationContext.class);
}
}
依赖树如下
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Rest :: Gateway 1.0.21-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ rest-gateway ---
[INFO] com.example:rest-gateway:war:1.0.21-SNAPSHOT
[INFO] +- com.example:rest-common:jar:1.0.12-SNAPSHOT:compile
[INFO] | +- com.oracle:ojdbc6:jar:11.2.0:compile
[INFO] | +- de.codecentric:spring-boot-admin-starter-client:jar:1.4.6:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-actuator:jar:1.5.2.RELEASE:compile
[INFO] | | | \- org.springframework.boot:spring-boot-actuator:jar:1.5.2.RELEASE:compile
[INFO] | | \- com.googlecode.json-simple:json-simple:jar:1.1.1:compile
[INFO] | +- org.springframework.integration:spring-integration-jmx:jar:4.3.6.RELEASE:compile
[INFO] | | \- org.springframework.integration:spring-integration-core:jar:4.3.8.RELEASE:compile
[INFO] | | +- org.springframework:spring-messaging:jar:4.3.7.RELEASE:compile
[INFO] | | \- org.springframework.retry:spring-retry:jar:1.2.0.RELEASE:compile
[INFO] | +- org.jolokia:jolokia-core:jar:1.3.5:compile
[INFO] | +- com.pms.mobiguider:domain:jar:0.2.7:compile
[INFO] | +- org.apache.camel:camel-core:jar:2.18.1:compile
[INFO] | | +- org.slf4j:slf4j-api:jar:1.7.24:compile
[INFO] | | +- com.sun.xml.bind:jaxb-core:jar:2.2.11:compile
[INFO] | | \- com.sun.xml.bind:jaxb-impl:jar:2.2.11:compile
[INFO] | +- org.apache.camel:camel-jackson:jar:2.18.1:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.8.7:compile
[INFO] | +- org.hibernate:hibernate-core:jar:5.0.11.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] | | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] | | +- org.javassist:javassist:jar:3.21.0-GA:compile
[INFO] | | +- antlr:antlr:jar:2.7.7:compile
[INFO] | | +- org.apache.geronimo.specs:geronimo-jta_1.1_spec:jar:1.1.1:compile
[INFO] | | +- org.jboss:jandex:jar:2.0.0.Final:compile
[INFO] | | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | | | \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] | | \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile
[INFO] | +- org.hibernate:hibernate-entitymanager:jar:5.0.11.Final:compile
[INFO] | +- org.springframework:spring-context:jar:4.3.7.RELEASE:compile
[INFO] | | +- org.springframework:spring-aop:jar:4.3.7.RELEASE:compile
[INFO] | | +- org.springframework:spring-beans:jar:4.3.7.RELEASE:compile
[INFO] | | \- org.springframework:spring-expression:jar:4.3.7.RELEASE:compile
[INFO] | +- org.springframework:spring-tx:jar:4.3.7.RELEASE:compile
[INFO] | +- org.aspectj:aspectjtools:jar:1.6.12:compile
[INFO] | +- cglib:cglib:jar:2.2:compile
[INFO] | | \- asm:asm:jar:3.1:compile
[INFO] | +- net.javacrumbs:json-xml:jar:1.0:compile
[INFO] | | \- org.codehaus.jackson:jackson-core-asl:jar:1.8.1:compile
[INFO] | +- commons-codec:commons-codec:jar:1.8:compile
[INFO] | +- com.atomikos:transactions-jdbc:jar:3.9.3:compile
[INFO] | | \- com.atomikos:transactions-jta:jar:3.9.3:compile
[INFO] | | \- com.atomikos:transactions:jar:3.9.3:compile
[INFO] | | \- com.atomikos:transactions-api:jar:3.9.3:compile
[INFO] | | \- com.atomikos:atomikos-util:jar:3.9.3:compile
[INFO] | \- org.springframework:spring-orm:jar:4.3.7.RELEASE:compile
[INFO] | \- org.springframework:spring-jdbc:jar:4.3.7.RELEASE:compile
[INFO] +- com.google.guava:guava:jar:19.0:compile
[INFO] +- commons-io:commons-io:jar:2.4:compile
[INFO] +- commons-lang:commons-lang:jar:2.6:compile
[INFO] +- org.hsqldb:hsqldb:jar:2.3.2:compile
[INFO] +- junit:junit:jar:4.12:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.powermock:powermock-api-mockito:jar:1.6.1:test
[INFO] | \- org.powermock:powermock-api-support:jar:1.6.1:test
[INFO] | +- org.powermock:powermock-core:jar:1.6.1:test
[INFO] | \- org.powermock:powermock-reflect:jar:1.6.1:test
[INFO] | \- org.objenesis:objenesis:jar:2.1:test
[INFO] +- com.sun.grizzly:grizzly-http:jar:1.9.46:test
[INFO] | +- com.sun.grizzly:grizzly-framework:jar:1.9.46:test
[INFO] | +- com.sun.grizzly:grizzly-rcm:jar:1.9.46:test
[INFO] | +- com.sun.grizzly:grizzly-portunif:jar:1.9.46:test
[INFO] | \- com.sun.grizzly:grizzly-utils:jar:1.9.46:test
[INFO] | \- com.sun.grizzly:grizzly-lzma:jar:1.9.46:test
[INFO] +- org.powermock:powermock-module-junit4:jar:1.6.1:test
[INFO] | \- org.powermock:powermock-module-junit4-common:jar:1.6.1:test
[INFO] +- org.mockito:mockito-all:jar:1.9.5:test
[INFO] +- com.jcabi:jcabi-http:jar:1.10.4:test
[INFO] | +- com.jcabi:jcabi-immutable:jar:1.3:test
[INFO] | +- com.jcabi:jcabi-aspects:jar:0.20.1:test
[INFO] | | \- org.aspectj:aspectjrt:jar:1.6.12:test
[INFO] | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | +- com.jcabi:jcabi-log:jar:0.15.1:test
[INFO] | \- com.jcabi:jcabi-manifests:jar:1.1:test
[INFO] +- org.springframework:spring-test:jar:4.3.4.RELEASE:test
[INFO] | \- org.springframework:spring-core:jar:4.3.7.RELEASE:compile
[INFO] | \- commons-logging:commons-logging:jar:1.2:compile
[INFO] +- org.springframework.boot:spring-boot:jar:1.5.2.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.2.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.2.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.24:compile
[INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.24:compile
[INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.24:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] | +- org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.3.3:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile
[INFO] | +- org.springframework:spring-web:jar:4.3.7.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:4.3.7.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-jetty:jar:1.5.2.RELEASE:provided
[INFO] | +- org.eclipse.jetty:jetty-servlets:jar:9.4.2.v20170220:provided
[INFO] | | +- org.eclipse.jetty:jetty-continuation:jar:9.4.2.v20170220:provided
[INFO] | | +- org.eclipse.jetty:jetty-http:jar:9.4.2.v20170220:provided
[INFO] | | +- org.eclipse.jetty:jetty-util:jar:9.4.2.v20170220:provided
[INFO] | | \- org.eclipse.jetty:jetty-io:jar:9.4.2.v20170220:provided
[INFO] | +- org.eclipse.jetty:jetty-webapp:jar:9.4.2.v20170220:provided
[INFO] | | +- org.eclipse.jetty:jetty-xml:jar:9.4.2.v20170220:provided
[INFO] | | \- org.eclipse.jetty:jetty-servlet:jar:9.4.2.v20170220:provided
[INFO] | | \- org.eclipse.jetty:jetty-security:jar:9.4.2.v20170220:provided
[INFO] | | \- org.eclipse.jetty:jetty-server:jar:9.4.2.v20170220:provided
[INFO] | +- org.eclipse.jetty.websocket:websocket-server:jar:9.4.2.v20170220:provided
[INFO] | | +- org.eclipse.jetty.websocket:websocket-common:jar:9.4.2.v20170220:provided
[INFO] | | | \- org.eclipse.jetty.websocket:websocket-api:jar:9.4.2.v20170220:provided
[INFO] | | +- org.eclipse.jetty.websocket:websocket-client:jar:9.4.2.v20170220:provided
[INFO] | | | \- org.eclipse.jetty:jetty-client:jar:9.4.2.v20170220:provided
[INFO] | | \- org.eclipse.jetty.websocket:websocket-servlet:jar:9.4.2.v20170220:provided
[INFO] | | \- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] | +- org.eclipse.jetty.websocket:javax-websocket-server-impl:jar:9.4.2.v20170220:provided
[INFO] | | +- org.eclipse.jetty:jetty-annotations:jar:9.4.2.v20170220:provided
[INFO] | | | +- org.eclipse.jetty:jetty-plus:jar:9.4.2.v20170220:provided
[INFO] | | | +- javax.annotation:javax.annotation-api:jar:1.2:provided
[INFO] | | | +- org.ow2.asm:asm:jar:5.1:provided
[INFO] | | | \- org.ow2.asm:asm-commons:jar:5.1:provided
[INFO] | | | \- org.ow2.asm:asm-tree:jar:5.1:provided
[INFO] | | +- org.eclipse.jetty.websocket:javax-websocket-client-impl:jar:9.4.2.v20170220:provided
[INFO] | | \- javax.websocket:javax.websocket-api:jar:1.0:provided
[INFO] | \- org.mortbay.jasper:apache-el:jar:8.0.33:provided
[INFO] \- org.springframework.boot:spring-boot-test:jar:1.5.2.RELEASE:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.242 s
[INFO] Finished at: 2018-09-26T14:54:30+05:30
[INFO] Final Memory: 30M/346M
[INFO] ---------------------------------------------