Java Junit不使用Spring boot
我正在尝试使用SpringBoot测试我的存储库 我跟着 我的测试班:Java Junit不使用Spring boot,java,spring,junit,spring-boot,Java,Spring,Junit,Spring Boot,我正在尝试使用SpringBoot测试我的存储库 我跟着 我的测试班: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) public class NotificationRepositoryTest { @Inject NotificationRepository notificationRepository; @T
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class NotificationRepositoryTest {
@Inject
NotificationRepository notificationRepository;
@Test
public void notificationRepositoryTest()
{
//some logic
}
}
Application.java:
@ComponentScan({"com.mycompany.notification.processor.service"})
@EnableAutoConfiguration
@Configuration
@ImportResource({
"classpath:integration-context.xml", "classpath:launch-context.xml", "classpath:applicationContext-NotificationProcessorService.xml"
})
@EnableJpaRepositories(basePackages = {"com.mycompany.notification.processor.service.dao"})
//@Import({ ServletConfiguration.class, WebappConfiguration.class })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
build.gradle:
buildscript {
repositories {
maven { url 'http://artifactory/artifactory/libs-release-local' }
mavenCentral()
maven { url 'http://repo.spring.io/milestone/' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.0.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
war {
baseName = 'notification-processor-service'
version = '1.0.0-SNAPSHOT'
}
jar {
baseName = 'notification-processor-service'
version = '1.0.0-SNAPSHOT'
}
ext {
springIntegrationKafkaVersion = '1.0.0.M2'
}
repositories {
maven { url 'http://artifactory/artifactory/libs-release-local' }
maven { url 'http://artifactory/artifactory/resin-hessian' }
mavenCentral()
maven {
url 'https://repository.apache.org/content/groups/public'
}
maven { url 'http://repo.spring.io/milestone/' }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("com.mycompany.core:infra-messaging-framework:0.0.14")
{
exclude module: 'org.springframework:spring-asm'
exclude module: 'spring-webmvc'
exclude module: 'org.springframework:spring-oxm'
exclude module: 'slf4j-log4j12'
}
compile("com.mycompany.services.configuration:ConfigurationManager:2.0.11")
{
exclude module: 'org.springframework:spring-asm'
exclude module: 'spring-webmvc'
exclude module: 'org.springframework:spring-oxm'
exclude module: 'slf4j-log4j12'
}
compile("com.caucho:resin-hessian:4.0.23")
compile("mysql:mysql-connector-java")
compile('org.springframework.boot:spring-boot-starter-web:1.2.0.RELEASE')
compile("org.springframework.boot:spring-boot-starter-batch:1.2.0.RELEASE")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("javax.inject:javax.inject:1")
compile('org.springframework.batch:spring-batch-admin-manager:1.3.0.RELEASE') {
exclude module: 'slf4j-log4j12'
}
testCompile('org.springframework.boot:spring-boot-starter-test:1.2.0.RELEASE')
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
我得到以下错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
Caused by: java.lang.IllegalArgumentException: Cannot load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration or @ContextHierarchy.
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:57)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
... 23 more
你知道我错过了什么吗
谢谢。您真的确定此StackTrace与该
NotificationRepositoryTest
相关吗?看起来您的一些测试类只是标记了@RunWith(SpringJUnit4ClassRunner.class)
,当您从IDEA运行所有测试时,您会看到这样一个异常。我对@SpringApplicationConfiguration
有足够的测试,所以如果只显示正确的springboot测试类这是我仅有的测试用例,那么您的案例对我来说没有意义。除了这个类,我的项目中没有其他@RunWith。请查看项目的IDEA依赖项,并确保没有旧的Spring测试版本。根据这些导入的名称,我不得不猜测一些导入的XML配置没有正确设置上下文。考虑到这是一款Spring Boot应用程序,它们很可能都不应该定义任何类型的上下文。我建议在删除所有这些导入的情况下尝试测试。此外,您不需要@EnableJpaRepositories
,尽管这可能只是您的项目结构的产物,我们看不到。我已将我的项目上载到github。在这里查看我的junit类。也许把这件事搞清楚会容易些。非常感谢。