springboot Junit测试NoClassDefFoundError EntityManagerFactoryBuilderImpl
Springboot Junit test NoClassDefFoundError EntityManagerFactoryBuilderImpl
我创建了一个springboot(2)webflux项目如下:
JPA 实体
@Entity
@Table(name = "users")
public class User implements Serializable
{
...
}
Spring 存储库
public interface UserRepository extends CrudRepository<User, Long>
{
}
服务
@Service
public class UserService
{
@Autowired
private UserRepository userRepo;
...
}
Webflux 处理程序
@Component
public class UserHandler
{
@Autowired
private UserService userService;
public Mono<ServerResponse> getUser(ServerRequest request)
{
...
}
}
路由配置
@Configuration
public class RouteConfiguration
{
@Bean
public static RouterFunction<ServerResponse> userRoutes(UserHandler userHandler)
{
return RouterFunctions.route(RequestPredicates.GET("/user"), userHandler:: getUser);
}
网络应用程序
@SpringBootApplication
public class WebApplication
{
public static void main(String[] args)
{
SpringApplication.run(WebApplication.class);
}
}
POM
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Runtime -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
一切正常 运行 很好,我可以启动我的服务器并使用它。我现在想编写一些测试。这是我所做的:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = WebApplication.class)
public class UserHandlerTest
{
@Autowired
private ApplicationContext context;
@MockBean
private UserService userService;
private WebTestClient testClient;
@Before
public void setUp()
{
testClient = WebTestClient.bindToApplicationContext(context).build();
}
@Test
public void testUser()
{
...
}
}
无论我尝试过什么,我在 "mvn clean install" 过程中遇到休眠依赖项错误:
[ERROR] testUser(...UserHandlerTest) Time elapsed: 0 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl
我知道 JPA 以阻塞方式工作,但我想避免在该项目中使用 NoSQL DB。我错过了什么 ?非常感谢您的帮助!
您可能缺少我们需要为 src/main/resources 下的数据源提供的详细信息。你可以查看 https://github.com/hantsy/spring-reactive-sample/blob/master/boot-data-mongo/src/main/resources/application.yml。这可能对你有帮助。
为了测试我的 Spring Webflux 控制器,我最终使用了 WebFluxTest 注释。它按预期工作:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {RouteConfiguration.class, UserHandler.class})
@WebFluxTest
public class UserHandlerTest
{
@Autowired
private ApplicationContext context;
@MockBean(name="userService")
private UserService userService;
private WebTestClient testClient;
@Before
public void setUp()
{
testClient = WebTestClient.bindToApplicationContext(context).build();
}
...
因为我不使用 RestController 注释而是使用功能端点,所以我不得不使用 ContextConfiguration 并手动实例化 WebTestClient。
我创建了一个springboot(2)webflux项目如下:
JPA 实体
@Entity
@Table(name = "users")
public class User implements Serializable
{
...
}
Spring 存储库
public interface UserRepository extends CrudRepository<User, Long>
{
}
服务
@Service
public class UserService
{
@Autowired
private UserRepository userRepo;
...
}
Webflux 处理程序
@Component
public class UserHandler
{
@Autowired
private UserService userService;
public Mono<ServerResponse> getUser(ServerRequest request)
{
...
}
}
路由配置
@Configuration
public class RouteConfiguration
{
@Bean
public static RouterFunction<ServerResponse> userRoutes(UserHandler userHandler)
{
return RouterFunctions.route(RequestPredicates.GET("/user"), userHandler:: getUser);
}
网络应用程序
@SpringBootApplication
public class WebApplication
{
public static void main(String[] args)
{
SpringApplication.run(WebApplication.class);
}
}
POM
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Runtime -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
一切正常 运行 很好,我可以启动我的服务器并使用它。我现在想编写一些测试。这是我所做的:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = WebApplication.class)
public class UserHandlerTest
{
@Autowired
private ApplicationContext context;
@MockBean
private UserService userService;
private WebTestClient testClient;
@Before
public void setUp()
{
testClient = WebTestClient.bindToApplicationContext(context).build();
}
@Test
public void testUser()
{
...
}
}
无论我尝试过什么,我在 "mvn clean install" 过程中遇到休眠依赖项错误:
[ERROR] testUser(...UserHandlerTest) Time elapsed: 0 s <<< ERROR! java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl
我知道 JPA 以阻塞方式工作,但我想避免在该项目中使用 NoSQL DB。我错过了什么 ?非常感谢您的帮助!
您可能缺少我们需要为 src/main/resources 下的数据源提供的详细信息。你可以查看 https://github.com/hantsy/spring-reactive-sample/blob/master/boot-data-mongo/src/main/resources/application.yml。这可能对你有帮助。
为了测试我的 Spring Webflux 控制器,我最终使用了 WebFluxTest 注释。它按预期工作:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {RouteConfiguration.class, UserHandler.class})
@WebFluxTest
public class UserHandlerTest
{
@Autowired
private ApplicationContext context;
@MockBean(name="userService")
private UserService userService;
private WebTestClient testClient;
@Before
public void setUp()
{
testClient = WebTestClient.bindToApplicationContext(context).build();
}
...
因为我不使用 RestController 注释而是使用功能端点,所以我不得不使用 ContextConfiguration 并手动实例化 WebTestClient。