无法在Spring Boot中运行任何单元测试

Ryan S:

我正在尝试编写一个简单的单元测试。这个测试函数将调用我的服务,该服务从数据库中获取信息,将其推送到列表中并返回。我在调试日志中查看了很多内容,以查找可能的原因,但是似乎没有什么可以帮助我。我对Spring Boot不太熟悉,但是所有这些努力似乎使我感到惊讶,而且我无法通过简单的单元测试。

错误日志

http://text-share.com/view/fb0369c3

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed;

示例测试

package com.algoq.algoq;

import com.algoq.algoq.models.Subscriber;
import com.algoq.algoq.respositories.SubscriberRepository;
import com.algoq.algoq.services.AlgorithmService;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;


import java.util.ArrayList;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@TestConfiguration
@SpringBootTest(classes = {
        AlgoQApplication.class,
        })
public class ExampleTest extends AlgoQApplicationTests {

    @Autowired
    private AlgorithmService aService;

    @MockBean
    private SubscriberRepository employeeRepository;

    @Bean
    public AlgorithmService aService() {
        return new AlgorithmService();
    }


    @Test
    public void subscriberListNull() throws Exception {
        ArrayList<Subscriber> subs = aService.getSubscribers();
        assertThat(subs).isEmpty();
    }

}

服务

package com.algoq.algoq.services;

import com.algoq.algoq.models.Subscriber;
import com.algoq.algoq.respositories.SubscriberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Service
public class AlgorithmService {

    @Autowired
    private SubscriberRepository subRep;

    /**
     * Gets a list of subscribers to return to the API
     * @return
     */
    public ArrayList<Subscriber> getSubscribers() {
        ArrayList<Subscriber> subscribers = new ArrayList<>();
        subRep.findAll()
                .forEach(subscribers::add);
        return subscribers;
    }

    /**
     * Adds a new subscriber to the database
     * @param sub
     * @return
     */
    public void addSubscriber(Subscriber sub) {
        subRep.save(sub);
    }

    /**
     * Finds a single user id
     * @param email
     * @return
     */
    public List<Subscriber> getSubscriber(String email) {
        return subRep.findByEmailAddress(email);
    }
}

应用

package com.algoq.algoq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
//@EnableScheduling
public class AlgoQApplication {

    public static void main(String[] args) {
        SpringApplication.run(AlgoQApplication.class, args);
    }
}

聚甲醛

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.algoQ</groupId>
    <artifactId>algo-q</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>algo-q</name>
    <description>An algorithm a day keeps the brain up to date</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.12</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.5.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.python</groupId>
            <artifactId>jython-standalone</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>com.h2database</groupId>-->
            <!--<artifactId>h2</artifactId>-->
            <!--<version>1.4.194</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.pygments</groupId>
            <artifactId>pygments</artifactId>
            <version>1.5</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
Aliaksei Stadnik:

导致异常的真正原因是

java.lang.ClassNotFoundException: javax.xml.bind.JAXBException

因此,我相信您需要将JAXBE库添加到您的类路径(它已从java9中的JDK中删除,我想您正在使用java9)

尝试将其添加到您的pom文件中

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.0</version>
</dependency>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法在Visual Studio 2015中运行任何单元测试

Spring Boot单元测试将无法运行。NullInsteadOfMockException

Spring Boot不运行单元测试

Spring Boot 单元测试未运行

为什么我的Spring应用程序从Spring Boot单元测试中运行

在Spring Boot应用程序中运行JUnit单元测试而不提供数据源

如何在 JUnit 单元测试中从运行中排除 Spring Boot 组件?

运行单元测试时出现Spring Boot错误

在Android Kotlin中,assertNotNull导致单元测试无法运行

Django中单元测试的替代设置无法正常运行

无法在Webstorm中运行Typescript单元测试

无法在单元测试中运行远程功能

无法在Android Studio中运行单元测试

angular-cli-单元测试无法在VSTS中运行?

无法在Visual Studio 2017 Professional中运行单元测试

单元测试中的Spring Boot数据源

单元测试中的Spring Boot @Autowired返回NullPointerException

单元测试中的 Spring Boot @RestController @Autowired null

如何在 Spring Boot 中对 StdDeserializer 进行单元测试?

Spring Boot 2.1.1:java.lang.IllegalStateException:运行单元测试时无法检索@EnableAutoConfiguration基本包错误

无法在Windows下使用Ruby 2.0.0对Redmine运行任何单元测试。那有可能吗?

无法运行单元测试用例

dotnet核心:无法运行单元测试

Xunit单元测试将无法运行

JUnit Eclipse无法运行单元测试

无法运行python单元测试

运行单元测试无法捕获更改

.netcore单元测试无法正常运行

无法在Xcode 11中运行单元测试:运行目标*对于您选择执行的测试无效