Java Spring boot测试API和存储库的多个类

Java Spring boot测试API和存储库的多个类,java,spring,unit-testing,spring-boot,integration-testing,Java,Spring,Unit Testing,Spring Boot,Integration Testing,我为Spring Boot编写了测试,有两个类正在测试API和存储库。骨骼如下所示 @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) @AutoConfigureMockMvc public class AppointmentAPITest { /* * we need a system to simulate this beha

我为Spring Boot编写了测试,有两个类正在测试API和存储库。骨骼如下所示

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class AppointmentAPITest {


    /*
     * we need a system to simulate this behavior without starting a
     * full HTTP server. MockMvc is the Spring class that does that.
     * */
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private AppointmentAPI api;


    // now the tests are going on

}
存储库测试类

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class AppointmentRepositoryTest {


    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private AppointmentRepository repository;


    // write the test here 

}
我如何使用一个类来运行它们?例如,如果类将是
AppointmentApplicationTests

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

    @Test
    public void contextLoads() {

    }

}

配置该类以便调用API和repo类中的所有测试的最佳方法是什么?

我认为最简单的方法是创建一个
套件
来运行一组测试,如:

JUnit4

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    some.package.AppointmentRepositoryTest,
    some.package.AppointmentApplicationTests
})
public class MySuite {
}
Junit5

@RunWith(JUnitPlatform.class)
@SelectClasses({some.package.AppointmentRepositoryTest,
    some.package.AppointmentAPITest.class})
public class JUnit5TestSuiteExample {
}

然而,这并不一定是最好的方法。还要考虑如何创建Maven PoT的测试配置文件,执行一系列测试或包。p> 我认为最简单的方法是创建一个
套件
来运行一组测试,比如:

JUnit4

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    some.package.AppointmentRepositoryTest,
    some.package.AppointmentApplicationTests
})
public class MySuite {
}
Junit5

@RunWith(JUnitPlatform.class)
@SelectClasses({some.package.AppointmentRepositoryTest,
    some.package.AppointmentAPITest.class})
public class JUnit5TestSuiteExample {
}

然而,这并不一定是最好的方法。还要考虑如何创建Maven PoT的测试配置文件,执行一系列测试或包。p> 现在如何运行测试?你能提供一个例子吗?我从每个文件中分别运行它们。您还需要查看文件中的测试吗?不,我的意思是,您只需要执行“右键单击->以Junit测试方式运行”之类的操作吗?任何构建工具maven/gradle?您使用IDEA/Eclipse什么IDE?现在如何运行测试?你能提供一个例子吗?我从每个文件中分别运行它们。您还需要查看文件中的测试吗?不,我的意思是您只需要执行“右键单击->以Junit测试的形式运行”之类的操作吗或者怎么做?任何构建工具maven/gradle?您使用IDEA/Eclipse的IDE是什么?我需要这样的东西,但在使用代码后出现错误-
org.junit.platform.commons.util.PremissionException:如果没有至少一个测试引擎,则无法创建启动器;考虑将引擎实现jar添加到类路径< /代码> @ AARFE,只是猜测,但也许您在尝试使用JunIT5,而应该使用4?认为弹簧靴有4个。5与4有点不同,在POM中需要更多的DEP。我刚刚检查了
JUnit4
的代码。第二个注释需要是
@Suite.SuiteClasses({AppointmentRepositoryTest.class,appointmentapietest.class})
。我需要这样的注释,但在使用代码-
org.junit.platform.commons.util.PremissionException:如果没有至少一个测试引擎,则无法创建启动器;考虑将引擎实现jar添加到类路径< /代码> @ AARFE,只是猜测,但也许您在尝试使用JunIT5,而应该使用4?认为弹簧靴有4个。5与4有点不同,在POM中需要更多的DEP。我刚刚检查了
JUnit4
的代码。第二个注释需要是
@Suite.SuiteClasses({AppointmentRepositoryTest.class,AppointmentAPITest.class})