似乎没有一种可以使@DataJpaTest
正常运行的在线标准方法。
现在是否没有使用@DataJpaTest
,并且所有测试都使用@SpringBootTest
在服务或控制器级别运行?
@Repository
public interface MyBeanRepository extends JpaRepository<MyBean, Long> {
}
@Configuration
@EnableJpaRepositories("com.app.repository.*")
@ComponentScan(basePackages = { "com.app.repository.*" })
public class ConfigurationRepository {
}
@Entity
public class MyBean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Version
@Column(name = "version")
private Integer version;
@NotNull
@Size(min = 2)
private String name;
}
@DataJpaTest
public class MyBeanIntegrationTest {
@Autowired
MyBeanRepository myBeanRepository;
@Test
public void testMarkerMethod() {
}
@Test
public void testCount() {
Assertions.assertNotNull(myBeanRepository , "Data on demand for 'MyBean' failed to initialize correctly");
}
}
使用Eclipse-> Run Junit运行会显示这些日志。
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
at org.springframework.util.Assert.state(Assert.java:73)
使用gradle test运行会显示初始化失败的错误。
FAILURE: Build failed with an exception.
* What went wrong:
Test failed.
Failed tests:
Test com.app.repository.MyBeanIntegrationTest#initializationError (Task: :test)
这是gradle脚本。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin")
}
}
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
id 'eclipse'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtimeOnly 'org.hsqldb:hsqldb'
testImplementation ('org.springframework.boot:spring-boot-starter-test') {
// exlcuding junit 4
exclude group: 'junit', module: 'junit'
}
/**
Test Dependencies Follows
**/
// junit 5 api
testCompile "org.junit.jupiter:junit-jupiter-api:5.2.0"
// For junit5 parameterised test support
testCompile "org.junit.jupiter:junit-jupiter-params:5.2.0"
// junit 5 implementation
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.2.0"
// Only required to run junit5 test from IDE
testRuntime "org.junit.platform:junit-platform-launcher"
}
编辑:
此问题已解决,并提交到同一存储库。 https://github.com/john77eipe/spring-demo-1-test
答案 0 :(得分:-3)
添加Github链接是一个好主意。我在那里看到了以下问题:
1)如果您不能用@SpringBootApplication
注释主类,则可以使用:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.app.repository")
public class MySampleApplication {
}
2)将您的ConfigurationRepository
类的注释更改为:
@EnableJpaRepositories("com.app.repository")
@ComponentScan(basePackages = { "com.app.repository" })
public class ConfigurationRepository {
那应该让我们继续下一点:
3)您的MyBeanIntegrationTest
的注释应为:
@SpringBootTest(classes = MyAppApplication.class)
public class MyBeanIntegrationTest {
4)在application.yml
中,您在最后一行有一个缩进的小问题。转换制表符,以便空格,应该没问题。
5)接下来是MyBeanRepository
界面。您不能在那里使用名为findOne
的方法。事实是,在标记为JpaRepository
或CrudRepository
等的接口中,方法名称需要遵循某些规则。如果您将其标记为包含类型MyBean
的存储库,则您的方法名称应更改为findById
,因为Spring会在bean中查找名为id
的属性。用findOne
命名将导致测试失败,并显示以下内容:
No property findOne found for type MyBean!
修复这些问题后,您的测试将通过我的环境。
我希望这会有所帮助!