我正在开发一个SpringBoot(2.0.0.BUILD-SNAPSHOT)应用程序。
当我使用以下注释为我的实体运行测试时,我成功地运行了测试:
@RunWith(SpringRunner.class)
@DataJpaTest但是,当我使用以下注释对我的服务层运行测试时,我得到了一个突然出现的异常:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.NoSuchMethodError: io.micrometer.core.instrument.Tags.zip([Ljava/lang/String;)Lio/micrometer/core/instrument/Tags;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:378)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:138)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
... 24 more
Caused by: java.lang.NoSuchMethodError: io.micrometer.core.instrument.Tags.zip([Ljava/lang/String;)Lio/micrometer/core/instrument/Tags;
at org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsConfiguration.bindDataSourceToRegistry(DataSourcePoolMetricsConfiguration.java:71)
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
at org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsConfiguration.bindDataSourcesToRegistry(DataSourcePoolMetricsConfiguration.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:702)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
... 40 more这是我的build.gradle:
import org.springframework.boot.gradle.tasks.run.BootRun
import java.text.SimpleDateFormat
buildscript {
ext {
springBootVersion = "2.0.0.BUILD-SNAPSHOT"
}
repositories {
mavenLocal()
jcenter()
mavenCentral()
google()
maven {
url 'https://repo.spring.io/snapshot'
}
maven {
url 'https://repo.spring.io/milestone'
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'org.asciidoctor.convert' version '1.5.3'
id 'idea'
id 'maven'
id 'jacoco'
id 'java'
id 'java-library'
id 'application'
}
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'
group = 'helloworld'
version = '0.0.1-SNAPSHOT'
compileTestJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
options.compilerArgs += '-parameters'
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'org.springframework') {
details.useVersion '5.0.2.RELEASE'
}
}
}
repositories {
maven {
url 'https://repo.spring.io/snapshot'
}
maven {
url 'https://repo.spring.io/milestone'
}
mavenLocal()
jcenter()
maven {
url myMavenRepoReadUrl
}
}
ext {
postgresqlVersion = "42.1.4"
disruptorVersion = "3.3.6"
springfoxSwagger2Version = "2.7.0"
guavaVersion = "23.5-jre"
commonsTextVersion = "1.1"
commonsLang3Version = "3.7"
woodstoxCoreAslVersion = "4.4.1"
springRestDocsVersion = "2.0.0.RELEASE"
snippetsDir = file("$buildDir/generated-snippets")
Date buildTimeAndDate = new Date()
buildDate = new SimpleDateFormat('yyyy-MM-dd').format(buildTimeAndDate)
buildTime = new SimpleDateFormat('HH:mm:ss.SSSZ').format(buildTimeAndDate)
}
dependencies {
asciidoctor "org.springframework.restdocs:spring-restdocs-asciidoctor:${springRestDocsVersion}"
compile("org.springframework.boot:spring-boot-starter") {
exclude group: "org.springframework.boot", module: "spring-boot-starter-logging"
}
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-data-rest")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-hateoas")
compile("org.springframework.boot:spring-boot-starter-cache")
compile("org.springframework.boot:spring-boot-configuration-processor")
compile("org.springframework.data:spring-data-rest-hal-browser")
compile("org.springframework.data:spring-data-elasticsearch")
compile("io.springfox:springfox-swagger2")
compile("io.springfox:springfox-swagger-ui")
compile("org.springframework.boot:spring-boot-starter-log4j2") {
exclude group: "org.apache.logging.log4j", module: "log4j-slf4j-impl"
}
compile("com.lmax:disruptor:${disruptorVersion}")
compile("io.springfox:springfox-swagger2:${springfoxSwagger2Version}")
compile("io.springfox:springfox-swagger-ui:${springfoxSwagger2Version}")
compile("com.google.guava:guava:${guavaVersion}")
compile("org.apache.commons:commons-text:${commonsTextVersion}")
compile("org.apache.commons:commons-lang3:${commonsLang3Version}")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
compile("org.codehaus.woodstox:woodstox-core-asl:${woodstoxCoreAslVersion}")
compile("org.projectlombok:lombok")
runtime("com.h2database:h2")
runtime("org.postgresql:postgresql:${postgresqlVersion}")
runtime("org.flywaydb:flyway-core")
testCompile("com.jayway.jsonpath:json-path")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.springframework.restdocs:spring-restdocs-mockmvc:${springRestDocsVersion}")
}
defaultTasks 'clean', 'build'
jacoco {
toolVersion = "0.7.9"
reportsDir = file("$buildDir/reports/jacoco")
}
jacocoTestReport {
group = "Reporting"
reports {
xml.enabled true
csv.enabled false
html.destination file("$buildDir/reports/jacocoHtml")
}
}
test {
outputs.dir snippetsDir
finalizedBy jacocoTestReport
}
asciidoctor {
dependsOn 'test'
inputs.dir snippetsDir
sourceDir = file('src/main/asciidoc')
sources {
include 'api-guide.adoc'
}
outputDir = file("$buildDir/docs")
}
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
jar {
version = '0.0.1-SNAPSHOT'
dependsOn 'asciidoctor'
from("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version,
'Build-Date': buildDate,
'Build-Time': buildTime)
}
}
publishing {
repositories {
maven {
url myMavenRepoWriteUrl
}
}
publications {
maven(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}
mainClassName = 'HelloWorldApplication'
task bootRunDev(type: BootRun, dependsOn: 'build') {
group = 'Application'
doFirst() {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
systemProperty 'spring.profiles.active', 'dev'
}
}
task bootRunTest(type: BootRun, dependsOn: 'build') {
group = 'Application'
doFirst() {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
systemProperty 'spring.profiles.active', 'test'
}
}这是我对服务层的测试:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase
public class SecurityPrivilegeServiceImplTest {
private static SecurityPrivilege testSecurityPrivilege =
new SecurityPrivilegeBuilder()
.name(PrivilegeName.TEST_PRIVILEGE_1)
.description("Test User Privilege")
.build();
private final UUID uuidTestSecurityPrivilege = UUID.randomUUID();
@Autowired
private SecurityPrivilegeService securityPrivilegeService;
@MockBean
private SecurityPrivilegeRepository securityPrivilegeRepository;
private SecurityPrivilege adminPrivilege;
private SecurityPrivilege superUserPrivilege;
private SecurityPrivilege userPrivilege;
@Before
public void setUp() {
// given
adminPrivilege =
new SecurityPrivilegeBuilder()
.name(PrivilegeName.ADMIN)
.description("Admin Privilege")
.build();
superUserPrivilege =
new SecurityPrivilegeBuilder()
.name(PrivilegeName.SUPERUSER)
.description("Super User Privilege")
.build();
userPrivilege =
new SecurityPrivilegeBuilder()
.name(PrivilegeName.USER)
.description("User Privilege")
.build();
List<SecurityPrivilege> allSecurityPrivileges = Arrays.asList(adminPrivilege, superUserPrivilege, userPrivilege);
given(securityPrivilegeRepository.findById(11L))
.willReturn(Optional.ofNullable(adminPrivilege));
given(securityPrivilegeRepository.findById(-99L))
.willReturn(null);
given(securityPrivilegeRepository.findByUuid(uuidTestSecurityPrivilege))
.willReturn(Optional.ofNullable(testSecurityPrivilege));
given(securityPrivilegeRepository.findByName(adminPrivilege.getName()))
.willReturn(Optional.ofNullable(adminPrivilege));
given(securityPrivilegeRepository.findAll())
.willReturn(allSecurityPrivileges);
given(securityPrivilegeRepository.saveAndFlush(testSecurityPrivilege))
.willReturn(testSecurityPrivilege);
}
/************************************************************************************************************
* Test the Finders
************************************************************************************************************/
@Test
public void whenGoodUuid_thenRetrieveSecurityPrivilege() {
Optional<SecurityPrivilege> found = securityPrivilegeService.findByUuid(uuidTestSecurityPrivilege);
verifyFindByUuidIsCalledOnce(uuidTestSecurityPrivilege);
assert found.isPresent();
assertThat(uuidTestSecurityPrivilege.equals(found.get().getUuid()));
}
@Test
public void whenBadUuid_thenRetrieveSecurityPrivilege() {
UUID randomUuid = UUID.randomUUID();
Optional<SecurityPrivilege> found = securityPrivilegeService.findByUuid(randomUuid);
verifyFindByUuidIsCalledOnce(randomUuid);
assertThat(found == null);
}
@Test(expected = IllegalArgumentException.class)
public void whenNullPrivilegeName_thenIllegalArgumentExceptionIsRaised() {
securityPrivilegeService.findByName(null);
}
@Test
public void whenGoodSecurityPrivilegeName_thenRetrieveSecurityPrivilege() {
Optional<SecurityPrivilege> found = securityPrivilegeService.findByName(adminPrivilege.getName().name());
verifyGetSecurityPrivilegeBySecurityPrivilegeNameIsCalledOnce(adminPrivilege.getName().name());
assertThat(found.get().getName().name()).isEqualTo(adminPrivilege.getName().name());
}
@Test
public void given3SecurityPrivilege_whenGetAll_thenReturn3Records() {
List<SecurityPrivilege> allSecurityPrivileges = securityPrivilegeService.findAll();
verifyFindAllSecurityPrivilegesIsCalledOnce();
assertThat(allSecurityPrivileges).hasSize(3)
.extracting(SecurityPrivilege::getName)
.contains(
PrivilegeName.ADMIN,
PrivilegeName.SUPERUSER,
PrivilegeName.USER);
}
/************************************************************************************************************
* Test the Create
************************************************************************************************************/
@Test(expected = IllegalArgumentException.class)
public void whenNullUser_thenIllegalArgumentExceptionIsRaised() {
securityPrivilegeService.save(null);
}
@Test
public void whenValidUser_thenUserShouldBeSaved() {
SecurityPrivilege savedSecurityPrivilege = securityPrivilegeService.save(testSecurityPrivilege);
verifySaveAndFlushIsCalledOnce();
assertThat(StringUtils.equals(testSecurityPrivilege.getName().name(), savedSecurityPrivilege.getName().name()));
assertThat(StringUtils.equals(testSecurityPrivilege.getDescription(), savedSecurityPrivilege.getDescription()));
}
/************************************************************************************************************
* Test the Delete
************************************************************************************************************/
@Test
public void whenValidPrivilegeName_thenSecurityPrivilegeShouldBeDeleted() {
boolean deletedSuccessfully = securityPrivilegeService.deleteByName(adminPrivilege.getName().name());
verifyDeletePrivilegeNameIsCalledOnce(adminPrivilege.getName().name());
assertThat(deletedSuccessfully);
}
/************************************************************************************************************
* Helpers for Verification
************************************************************************************************************/
private void verifyFindByUuidIsCalledOnce(UUID uuid) {
Mockito.verify(securityPrivilegeRepository, VerificationModeFactory.times(1))
.findByUuid(uuid);
Mockito.reset(securityPrivilegeRepository);
}
private void verifyGetSecurityPrivilegeBySecurityPrivilegeNameIsCalledOnce(String privilegeName) {
Mockito.verify(securityPrivilegeRepository, VerificationModeFactory.times(1))
.findByName(PrivilegeName.valueOf(privilegeName));
Mockito.reset(securityPrivilegeRepository);
}
private void verifyFindAllSecurityPrivilegesIsCalledOnce() {
Mockito.verify(securityPrivilegeRepository, VerificationModeFactory.times(1)).findAll();
Mockito.reset(securityPrivilegeRepository);
}
private void verifySaveAndFlushIsCalledOnce() {
Mockito.verify(securityPrivilegeRepository, VerificationModeFactory.times(1))
.saveAndFlush(any(SecurityPrivilege.class));
Mockito.reset(securityPrivilegeRepository);
}
private void verifyDeletePrivilegeNameIsCalledOnce(String privilegeName) {
Mockito.verify(securityPrivilegeRepository, VerificationModeFactory.times(1))
.findByName(PrivilegeName.valueOf(privilegeName));
Mockito.verify(securityPrivilegeRepository, VerificationModeFactory.times(1))
.delete(any(SecurityPrivilege.class));
Mockito.reset(securityPrivilegeRepository);
}
@TestConfiguration
static class SecuritySecurityPrivilegeServiceImplTestContextConfiguration {
@Bean
public SecurityPrivilegeService securityPrivilegeService() {
return new SecurityPrivilegeServiceImpl();
}
}
}我的application.yml
app:
contextPath: /api
hostName: localhost
name: HelloWorld
port: 9876
logging:
level:
org:
hibernate:
SQL: debug
springframework: info
net:
helloworld:
security:
web: debug
server:
port: 9876
servlet:
context-path: /api
spring:
application:
admin:
enabled: false
jmx-name: org.springframework.boot:type=Admin,name=HelloWorld
name: HelloWorld
datasource:
initialization-mode: always
h2:
console:
enabled: true
http:
converters:
preferred-json-mapper: jackson
jackson:
deserialization:
fail-on-unknown-properties: true
serialization:
indent-output: true
jpa:
open-in-view: true
generate-ddl: false
hibernate:
ddl-auto: validate
properties:
hibernate:
jdbc:
time_zone: GMT+8 发布于 2018-01-28 03:19:38
也许你的依赖性被打破了。尝试从本地删除依赖项并再次导入(使用--refresh-dependencies标志构建项目)
https://stackoverflow.com/questions/48479172
复制相似问题