【问题标题】:Springboot Junit test NoClassDefFoundError EntityManagerFactoryBuilderImplSpringboot Junit 测试 NoClassDefFoundError EntityManagerFactoryBuilderImpl
【发布时间】: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


    【解决方案1】:

    您可能缺少我们需要为 src/main/resources 下的数据源提供的详细信息。你可以查看https://github.com/hantsy/spring-reactive-sample/blob/master/boot-data-mongo/src/main/resources/application.yml。这可能会帮助你。

    【讨论】:

    • 亲爱的 Yogendra,我正在使用数据源进行运行时,但在测试期间,由于 MockBean 注释,我想模拟 UserService。这应该够了吧?无论如何,我尝试使用 h2 数据库创建 application-test.profile 并在我的测试用例中使用 ActiveProfiles,但仍然出现相同的错误。
    【解决方案2】:

    为了测试我的 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 2021-08-04
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 2017-10-20
      相关资源
      最近更新 更多