我创建了一个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 Handler
@Component
public class UserHandler
{
@Autowired
private UserService userService;
public Mono<ServerResponse> getUser(ServerRequest request)
{
...
}
}
RouteConfiguration
@Configuration
public class RouteConfiguration
{
@Bean
public static RouterFunction<ServerResponse> userRoutes(UserHandler userHandler)
{
return RouterFunctions.route(RequestPredicates.GET("/user"), userHandler:: getUser);
}
Web应用程序
@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()
{
...
}
}
我尝试了什么,在&#34; mvn clean install&#34;中出现了hibernate依赖关系的错误。过程:
[ERROR] testUser(... UserHandlerTest)经过的时间:0秒&lt;&lt;&lt;错误! java.lang.IllegalStateException:无法加载ApplicationContext 引起:org.springframework.beans.factory.BeanCreationException:创建名称为&#39; entityManagerFactory&#39;的bean时出错在类路径资源中定义[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]:调用init方法失败;嵌套异常是java.lang.NoClassDefFoundError:无法初始化类org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl 引起:java.lang.NoClassDefFoundError:无法初始化类org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl
我知道JPA以阻塞的方式工作,但我想避免在这个项目中使用NoSQL DB。我错过了什么 ?非常感谢你的帮助!
答案 0 :(得分:0)
可能您缺少我们需要在src / main / resources下为数据源提供的详细信息。你可以查看https://github.com/hantsy/spring-reactive-sample/blob/master/boot-data-mongo/src/main/resources/application.yml。这可能对你有帮助。
答案 1 :(得分:0)
为了测试我的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。