Ste*_*fan 6 java junit maven-module spring-boot
我已经将基于Spring Boot的项目拆分为几个Maven模块.现在只有war-project包含一个入门类(有一个main方法,启动Spring),其他模块都是jar类型.
如果他们不包含启动器,我如何测试jar项目?
示例JUnit测试用例标题:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(StarterClassInDifferentProject.class)
...
Run Code Online (Sandbox Code Playgroud)
我认为每个模块都应该提供上下文测试,因此您可以在早期找到有关线路和配置的问题,而不是依靠完整的应用程序测试来查找它们.
我在同一模块中使用测试应用程序类解决了这个问题.确保这个主类在你的测试目录中.
@SpringBootApplication()
@EnableAutoConfiguration()
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
你的上下文现在应该工作.
@RunWith(SpringRunner.class)
//@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
//@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
//@ContextConfiguration()
@ActiveProfiles(profiles = {Profiles.WEB_REST})
//@TestPropertySource("/config/rest.yml")
@WebMvcTest(EntityController.class)
@DirtiesContext
public class ServicesControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private Controller controller;
@Test
public void testAll() throws Exception {
given(controller.process(null)).willReturn(null);
mvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
Run Code Online (Sandbox Code Playgroud)
我解决了类似的情况。我有一个包含两个模块的项目:
我想以 spring-boot-test 方式测试“lib”项目。
首先,在 pom.xml 中包含范围为“test”的所需依赖项(在我的例子中还有 H2 数据库):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.3.3.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- add also add this here, even if in my project it is already present as a regular dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.3.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.191</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
出于测试目的,在“lib”项目的测试源中,我有一个类作为我的测试配置
package my.pack.utils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@TestConfiguration
@EnableJpaRepositories(basePackages = {"my.pack.engine.storage", "my.pack.storage"})
@EntityScan(basePackages = {"my.pack.storage", "my.pack.entity"})
@EnableAutoConfiguration
public class MyTestConfiguration
{
}
Run Code Online (Sandbox Code Playgroud)
这将设置 H2 数据库以测试应用程序的数据访问功能
最后,只有在我认为有用的测试类中,我才将执行配置为使用测试配置(我并不总是需要这样做,但有时它很方便):
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyTestConfiguration.class)
public class TestAClassThatNeedsSpringRepositories
{
// tests...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7607 次 |
| 最近记录: |