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

时间:2017-12-15 05:43:33

标签: java spring maven unit-testing spring-boot

我试图写一个简单的单元测试。此测试函数将调用我的服务,该服务从数据库中获取信息,将其推送到列表并返回它。我在调试日志中看起来很高和很低,以找到可能导致的结果,但似乎网络上的任何内容都没有帮助我。我对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);
    }
}

POM

<?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>

2 个答案:

答案 0 :(得分:4)

异常的真正原因是

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>

答案 1 :(得分:1)

<强> JDK

  

/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java

<强>的pom.xml

  

1.8

您使用的是java 8还是9?

  

org.springframework.beans.factory.BeanCreationException:错误   在类路径中定义名为'entityManagerFactory'的bean   资源   [组织/ springframework的/引导/自动配置/ ORM / JPA / HibernateJpaAutoConfiguration.class]:   调用init方法失败;嵌套异常是   java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException

java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException

的问题

使用Java 8环境运行应用程序或包含Java 9的JAXB库,JAVA SE 9中不包含JAXB API。请参阅Deprecated, for removal: This API element is subject to removal in a future version.。使用--add-modules( - add-modules java.xml.bind )在类路径中添加模块。 Jave只是弃用了,默认情况下不会在classpath上添加javax.xml.bind模块。

否则通过maven包含依赖性。

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.0</version>
</dependency>