Java mvn测试不在springboot项目中运行单元测试

Java mvn测试不在springboot项目中运行单元测试,java,spring-boot,maven,Java,Spring Boot,Maven,当我从start.spring.io创建项目时,除了项目包附带的默认应用程序测试之外,我在springboot项目中还有单元测试。当我从命令行运行mvn test时,我看到只运行默认的应用程序测试,而不运行我编写的单元测试。但是,我可以从IntelliJ运行这些测试。我使用的是maven版本3.6.2和maven surefire插件版本2.22.2。有人能告诉我我错过了什么吗?谢谢 这是我的测试课: @RunWith(SpringRunner.class) @SpringBootTest pu

当我从start.spring.io创建项目时,除了项目包附带的默认应用程序测试之外,我在springboot项目中还有单元测试。当我从命令行运行
mvn test
时,我看到只运行默认的应用程序测试,而不运行我编写的单元测试。但是,我可以从IntelliJ运行这些测试。我使用的是maven版本
3.6.2
和maven surefire插件版本
2.22.2
。有人能告诉我我错过了什么吗?谢谢

这是我的测试课:

@RunWith(SpringRunner.class)
@SpringBootTest
public class BranchServiceUnitTest {

    @Autowired
    private BranchService branchService;

    @MockBean
    private BranchRepository branchRepository;

    @Test
    public void testAddNewBranch() {
        Branch testBranch = new Branch();
        testBranch.setBranchName("TestBranch");
        testBranch.setCity("TestCity");
        testBranch.setContactNumber("TestContactNumber");
        testBranch.setEmailId("TestEmailId");
        Mockito.when(branchRepository.save(testBranch)).thenReturn(testBranch);
        Branch addedBranch = branchService.addBranch(testBranch);
        assertThat(addedBranch.getCity()).isEqualTo("TestCity");
    }

    @Test
    public void findBranchById() {
        Branch testBranch = new Branch();
        testBranch.setId(1);
        testBranch.setBranchName("TestBranch");
        testBranch.setCity("TestCity");
        testBranch.setContactNumber("TestContactNumber");
        testBranch.setEmailId("TestEmailId");
        Mockito.when(branchRepository.findById(testBranch.getId())).thenReturn(java.util.Optional.of(testBranch));
        Branch foundBranch = branchService.getBranchById(1);
        assertThat(foundBranch.getId()).isEqualTo(1);
    }

    @Test
    public void testGetAllBranches() {
        Branch testBranch1 = new Branch();
        testBranch1.setId(1);
        testBranch1.setBranchName("TestBranch");
        testBranch1.setCity("TestCity");
        testBranch1.setContactNumber("TestContactNumber");
        testBranch1.setEmailId("TestEmailId");

        Branch testBranch2 = new Branch();
        testBranch2.setId(2);
        testBranch2.setBranchName("TestBranch");
        testBranch2.setCity("TestCity");
        testBranch2.setContactNumber("TestContactNumber");
        testBranch2.setEmailId("TestEmailId");

        List<Branch> branches = Arrays.asList(testBranch1,testBranch2);
        Mockito.when(branchRepository.findAll()).thenReturn(branches);
        assertThat(branches.size()).isEqualTo(2);
        assertThat(branches.get(0).getId()).isEqualTo(1);
    }
}
@RunWith(SpringRunner.class)
@春靴测试
公共类BranchServiceUnitTest{
@自动连线
私人分行服务分行服务;
@蚕豆
私人部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门部门;
@试验
纽伯兰(纽约州){
Branch testBranch=新分支();
testBranch.Name(“testBranch”);
testBranch.setCity(“TestCity”);
testBranch.setContactNumber(“TestContactNumber”);
testBranch.setEmailId(“TestEmailId”);
Mockito.when(branchRepository.save(testBranch)),然后返回(testBranch);
Branch addedBranch=branchService.addBranch(testBranch);
资产(addedBranch.getCity()).isEqualTo(“TestCity”);
}
@试验
public void findBranchById(){
Branch testBranch=新分支();
testBranch.setId(1);
testBranch.Name(“testBranch”);
testBranch.setCity(“TestCity”);
testBranch.setContactNumber(“TestContactNumber”);
testBranch.setEmailId(“TestEmailId”);
when(branchRepository.findById(testBranch.getId()).thenReturn(java.util.Optional.of(testBranch));
Branch foundBranch=branchService.getBranchById(1);
assertThat(foundBranch.getId()).isEqualTo(1);
}
@试验
public void testGetAllBranchs(){
Branch testBranch1=新分支();
testBranch1.setId(1);
testBranch1.名称(“TestBranch”);
testBranch1.setCity(“TestCity”);
testBranch1.setContactNumber(“TestContactNumber”);
testBranch1.setEmailId(“TestEmailId”);
Branch testBranch2=新分支();
testBranch2.setId(2);
testBranch2.名称(“TestBranch”);
testBranch2.setCity(“TestCity”);
testBranch2.setContactNumber(“TestContactNumber”);
testBranch2.setEmailId(“TestEmailId”);
List branchs=Arrays.asList(testBranch1、testBranch2);
Mockito.when(branchRepository.findAll())。然后返回(branchs);
assertThat(branchs.size()).isEqualTo(2);
assertThat(branchs.get(0.getId()).isEqualTo(1);
}
}
以下是我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rkasibha</groupId>
    <artifactId>rentabook</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rentabook</name>
    <description>Rent a book service</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>

</project>


4.0.0
org.springframework.boot
spring启动程序父级
2.2.6.1发布
com.rkasibha
租房
0.0.1-快照
租房
租书服务
1.8
org.springframework.boot
弹簧靴起动器执行器
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧靴开发工具
运行时
真的
mysql
mysql连接器java
运行时
org.modelmapper
模型映射器
2.3.6
org.springframework.boot
弹簧起动试验
测试
org.junit.vintage
朱尼特老式发动机
朱尼特
朱尼特
测试
org.springframework.boot
springbootmaven插件
这看起来是假的

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <includes>
            <include>**/*Test*.java</include>
        </includes>
    </configuration>
</plugin>

org.apache.maven.plugins


测试目录中是否有要排除的特定类?如果没有,我会删除整个插件。Surefire已经在Maven的隐式父POM中声明,具有合理的默认值,将覆盖所有测试。自己声明它是不必要的冗长,并且主动破坏了一些现成的东西。

我认为是JUnit 4和JUnit 5奇怪的混合导致了这个问题:

  • SpringBoot2.2.6(我已经使用
    start.Spring.io
    生成了一个示例应用程序)使用JUnit5
  • 另一方面,您的测试是使用
    @RunWith
    编写的,这意味着它在后台使用JUnit4
  • 依赖关系:
  • 
    朱尼特
    朱尼特
    测试
    
    这似乎也是可疑的-spring boot starter测试已经包含了JUnit 5上所有必需的依赖项,所以您不需要这个依赖项


    现在,在分辨率方面,请查看此示例应用程序附带的默认测试(您在问题中描述的测试)。很有可能它自己使用JUnit 5,因此您最好将测试迁移到JUnit 5并重新运行。

    如果您的测试在单独运行时成功运行,但在运行过程中未被拾取

    mvn test
    
    很可能您在测试中使用的是较旧的Junit包。通常,当Junit版本中出现错误时,就会出现上述奇怪的情况

    如果正在使用Junit5,请确保导入中的包

    import org.junit.jupiter.api
    

    你能试着注释掉
    maven surefire插件
    中的
    includes
    部分吗?按照建议删除includes部分。@MichaelKreutz我从插件部分删除了maven surefire插件部分,但仍然没有运气。有一些路径需要配置吗?我现在删除了插件Michael,但是surefire插件仍然无法获取我的测试类。你是对的,一旦我删除了依赖项
    import org.junit.jupiter.api