- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我按照 this blog post 使用 @Category
注释设置了一个带有单独单元测试和集成测试的 Maven 构建。
在大多数情况下,这是可行的:标记为 @Category(IntegrationTest.class)
的测试在 integration-test
阶段执行,所有未标记的测试在 阶段执行>测试
阶段。
但是,看起来集成测试的上下文仍在(部分?)单元测试运行时创建,即使测试本身没有运行:
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ bamboo ---
[INFO] Surefire report directory: [removed]
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[INFO] parallel='none', perCoreThreadCount=true, threadCount=0, useUnlimitedThreads=false, threadCountSuites=0, threadCountClasses=0, threadCountMethods=0, parallelOptimized=true
-------------------------------------------------------
T E S T S
-------------------------------------------------------
09:56:05.458 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.example.MyIntegrationTest]
09:56:05.463 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
09:56:05.468 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
09:56:05.481 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.example.MyIntegrationTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
09:56:05.494 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.example.MyIntegrationTest], using SpringBootContextLoader
09:56:05.497 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.MyIntegrationTest]: class path resource [com/example/MyIntegrationTest-context.xml] does not exist
09:56:05.497 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.MyIntegrationTest]: class path resource [com/example/MyIntegrationTestContext.groovy] does not exist
09:56:05.497 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.example.MyIntegrationTest]: no resource found for suffixes {-context.xml, Context.groovy}.
09:56:05.530 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.example.MyIntegrationTest]
09:56:05.563 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.example.MyIntegrationTest]: using defaults.
09:56:05.567 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
09:56:05.580 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
09:56:05.586 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@62fdb4a6, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@11e21d0e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1dd02175, org.springframework.test.context.transaction.TransactionalTestExecutionListener@31206beb, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@3e77a1ed, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@3ffcd140, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@23bb8443, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@1176dcec, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@120d6fe6, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@4ba2ca36, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@3444d69d]
09:56:05.591 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.MyIntegrationTest]
09:56:05.591 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.MyIntegrationTest]
Running com.example.MyUnitTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.323 sec - in com.example.MyUnitTest
这是 MyIntegrationTest
的注释方式:
@Category(IntegrationTest.class)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApp.class)
public class MyIntegrationTest {
@Autowired
private ActualObject actualObject;
}
如何防止这种行为?
最佳答案
这会创建一个 TestContext,但不会对其进行初始化,因此速度应该很快。
对于每个测试类,都会创建一个 Spring TestContextManager,如 SpringJUnit4ClassRunner.java(与 SpringRunner.java 相同)的构造函数中所定义。
我认为除了实际排除测试文件本身之外,您无法避免创建 TestRunner。
您可以通过在测试/资源文件夹中添加 logback-test.xml
来最好地抑制这些日志。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework" level="ERROR"/>
</configuration>
关于java - 带有 SpringRunner.class 的 JUnit @Category 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49316077/
我正在尝试按如下方式创建 Spring 集成测试: @RunWith(SpringRunner::class) @ActiveProfiles(profiles = ["Test"]) @Contex
我有一个 spring-boot 应用程序,我正在尝试为其创建单元测试用例。下面是我尝试运行的代码,我没有任何配置文件(仅使用注释)所以加载所有配置的主类是 ElastSearchBootApplic
我找不到哪个 jar 会为我提供 SpringRunner.class 的依赖项 我正在尝试将我的集成测试升级到 spring-boot 1.4.0 最佳答案 SpringRunner.java位于s
我们正在与我的同事讨论这种方法。他们说仅在集成或功能级别使用 SpringRunner。 问题是在以下级别使用它的利弊是什么? 例如我有简单的 bean : public class RewardDu
在单元测试中,@Runwith(SpringRunner.class) & @SpringBootTest有什么区别? 你能给我解释一下每一个的用例吗? 最佳答案 @RunWith(SpringRun
当我创建 JUnit 测试时: @RunWith(SpringRunner.class) @SpringBootTest @DirtiesContext(classMode = DirtiesCont
每当我看到一篇与 Spring 测试相关的博文时,我都会看到这些类中的任何一个,但不明白真正的区别: @RunWith(SpringRunner.class)@RunWith(Sprin
您好,我正在尝试使用 SpringRunner.class 运行我的单元测试。我正在测试类中为 jdbcTemaplte 创建一个新实例。我正在使用 H2 DB 进行单元测试,并且我能够使用 jdbc
我正在使用@PostConstruct在运行测试之前进行一些初始设置,但似乎@PostConstruct方法在每个测试上运行,而不是仅在测试类初始化后一次。我还看到在 @PostConstruct 之
我尝试在 Intellij IDEA 中导入现有项目。所有依赖项均已解决,项目构建成功。当我尝试执行项目时,我收到以下错误消息: Could not initialize class org.spri
我们的 Spring Boot REST API 目前有一个相当大的单元测试存储库。单元测试将常见的可重用测试代码重构为 TestUtil类是 @Component注释。 看来SpringRunner
这是我在 stackoverflow.com 上提出的第一个问题。 最近我一直在寻找一种方法来更改 Spring Boot (2.x) 命名其通过 @Component (etc) 注释创建的 bea
我尝试用静态方法为类编写单元测试。 当我使用 @RunWith(MockitoJUnitRunner.class) 时,我的测试运行成功,但是当我将其更改为 @RunWith(SpringRunner
我正在尝试测试 JavaMail api 并使用 SpringRunner 和 PowerMockRunner,但它失败了。 @RunWith(PowerMockRunner.class) @Powe
我正在寻找一种方法来最小化 SpringBootTest 的启动时间目前最多需要 15 秒才能启动并执行测试。我已经使用了模拟 webEnvironment和 standaloneSetup()具体的
我收到以下编译器错误: Type mismatch: cannot convert from Class to Class 从此代码: import org.junit.runner.RunWith;
这是一个用于 java spring 数据存储库层的单元测试类。 我有一个spring数据存储库层,其中使用@Autowired注解注入(inject)TestEntityManager类型对象(属于
我用mockito尝试了junit,并为编码练习编写了一些测试用例。 这是我编写的测试用例: @RunWith(SpringRunner.class) public class Transaction
我们有一个 Spring 5 应用程序,使用 JUnit 4 作为测试工具(使用 SpringRunner)。我们遇到一个问题,即未标记带有 @Test 注释的私有(private)帮助器方法正在作为
我使用 @RunWith(MockitoJUnitRunner.class) 与mockito 进行 junit 测试。但现在我正在使用 spring boot 应用程序并尝试使用 @RunWith(
我是一名优秀的程序员,十分优秀!