【发布时间】:2018-07-06 10:01:21
【问题描述】:
我创建了一个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) 已用时间:0 秒
我知道 JPA 以阻塞方式工作,但我想避免在这个项目中使用 NoSQL DB。我错过了什么 ?非常感谢您的帮助!
【问题讨论】:
标签: spring-boot spring-data-jpa spring-test spring-webflux