Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试Spring引导库模块_Java_Spring_Spring Boot_Unit Testing_Automated Tests - Fatal编程技术网

Java 测试Spring引导库模块

Java 测试Spring引导库模块,java,spring,spring-boot,unit-testing,automated-tests,Java,Spring,Spring Boot,Unit Testing,Automated Tests,我得到了一个多模块项目,其中不是每个模块都是一个应用程序,但很多模块都是libs。这些lib正在做主要的工作,我想在实现它们的地方测试它们。LIB的当前依赖项: implementation 'org.springframework.boot:spring-boot-starter' testImplementation 'org.springframework.boot:spring-boot-starter-test' 在主源代码中有一个带有@Configuration的类和一个bean:

我得到了一个多模块项目,其中不是每个模块都是一个应用程序,但很多模块都是libs。这些lib正在做主要的工作,我想在实现它们的地方测试它们。LIB的当前依赖项:

implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
在主源代码中有一个带有
@Configuration
的类和一个bean:

@Bean public String testString() { return "A Test String"; }
我有两个测试班:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles({"default", "test"}) 
public class Test1 {  

    @Test
    public void conextLoaded() {
    }
}
-

第一次测试有效。第二种情况并非如此。该项目中没有
@SpringBootApplication
,因此在与测试相同的包中,我添加了一个测试配置:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.to.config") 
public class LibTestConfiguration {
}

而且它不起作用。对于
@Service
的类也一样。它们不在上下文中。我怎样才能使它像一个普通的Spring启动应用程序一样运行,而不实际运行,并从我需要的配置文件中加载配置和上下文?默认配置文件和测试配置文件共享它们的大部分属性(目前),我希望它们像启动tomcat一样被加载。

我切换到JUnit 5并使其工作起来。。。因此,如果您想测试数据库内容:

@DataMongoTest
@ExtendWith(SpringExtension.class)
@ActiveProfiles({"default", "test"})
class BasicMongoTest { ... }
  • 用于自动关联所有存储库和mongo模板
  • 使用aplication.yml config初始化
  • 不初始化或配置拦截器

如果类路径中有一个类具有
@SpringBootApplication
(在测试上下文中可以是空的测试主节点),则进行完整的应用程序上下文测试

  • 使用所有配置和bean初始化完整上下文
  • 如果没有必要,就不应该这样做,因为它加载了所有的应用程序上下文,并且有点违背了单元测试只激活所需内容的目的

仅测试特定组件和配置:

@SpringBootTest(classes = {Config1.class, Component1.class})
@EnableConfigurationProperties
@ExtendWith(SpringExtension.class)
@ActiveProfiles({"default", "test"})
public class SpecificComponentsTest { ... }
  • 仅使用Config1和Component1类初始化上下文。Component1和Config1中的所有bean都可以自动连接

理想情况下,您的库组件应该是可单元测试的。这是计划,但它们仍然是具有特定测试配置的服务和组件。另一种方法是自己初始化服务,但我觉得这不太合适。你熟悉测试模拟吗?通常一次只测试一个服务,在测试过程中模拟每个服务的依赖关系。是的,但如果依赖关系是一个数据库,而我有一个复杂的查询要测试,那该怎么办?我也想测试spring数据存储库。它们通常是在运行时由spring boot创建的。我也有一个elasticsearch索引。这些查询往往会很快变得复杂,而且往往会在这里或那里中断。我理解隔离部分,对于大多数服务,您是对的,我可以自己在测试运行时注入依赖项(主要是字符串配置)。但是对于spring提供的那些实现,我不知道如何(仍然更喜欢外部testconf)注意,
@extendedwith(SpringExtension.class)
在使用
@SpringBootTest
时是多余的
@SpringBootTest
@ExtendWith(SpringExtension.class)
@ActiveProfiles({"default", "test"})
public class FullContextTest { ... }
@SpringBootTest(classes = {Config1.class, Component1.class})
@EnableConfigurationProperties
@ExtendWith(SpringExtension.class)
@ActiveProfiles({"default", "test"})
public class SpecificComponentsTest { ... }