简体   繁体   English

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

[英]Can't get any unit tests to run in Spring Boot

I'm trying to write a simple unit test. 我正在尝试编写一个简单的单元测试。 This test function will call my service, which grabs info from the database, pushes it to a list and returns it. 这个测试函数将调用我的服务,该服务从数据库中获取信息,将其推送到列表中并返回。 I've looked high and low in the debug logs to find what could be causing, but it seems nothing on the web is helping me out. 我在调试日志中查看了很多内容,以查找可能的原因,但是似乎网络上没有任何帮助。 I'm not too familiar with Spring Boot, but it seems surprising that all this effort and I am unable to get a simple unit test to pass. 我对Spring Boot不太熟悉,但是所有这些努力似乎令人惊讶,而且我无法通过简单的单元测试。

Error Logs 错误记录

http://text-share.com/view/fb0369c3 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;

Example Test 示例测试

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();
    }

}

Service 服务

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);
    }
}

Application 应用

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

The real cause of the exception is 导致异常的真正原因是

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

So I believe you need to add JAXBE library to your classpath (it was removed from JDK in java9 and I guess you are using java9) 因此,我相信您需要将JAXBE库添加到您的类路径(它已从java9中的JDK中删除,我想您正在使用java9)

Try to add this to your pom file 尝试将其添加到您的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>

JDK JDK

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

pom.xml 的pom.xml

1.8 1.8

Are you using java 8 or 9 ? 您使用的是Java 8还是9?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]中定义的名称为'entityManagerFactory'的bean时出错:调用init方法失败; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException 嵌套的异常是java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException

Issue with java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException , java.lang.NoClassDefFoundError问题:javax / xml / bind / JAXBException

Run the application with Java 8 environement or Include the JAXB library for Java 9, JAXB API are not included in JAVA SE 9. Refer Deprecated, for removal: This API element is subject to removal in a future version. 在Java 8环境下运行该应用程序,或包括Java 9的JAXB库,JAVA SE 9中不包含JAXB API。请参见不推荐使用,以进行删除:此API元素可能会在将来的版本中删除。 . Use --add-modules ( --add-modules java.xml.bind ) to add module in classpath. 使用--add-modules(-- add-modules java.xml.bind )将模块添加到类路径中。 Jave has only deprecated and does not add javax.xml.bind module on classpath by default. Jave仅已弃用,默认情况下不会在类路径上添加javax.xml.bind模块。

Otherwise include as dependency through maven. 否则包括通过maven作为依赖项。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM