嗨,我不明白有什么优点可以让我使用@ContextHierarchy,如下所示:
@ContextHierarchy({
@ContextConfiguration("/test-db-setup-context.xml"),
@ContextConfiguration("FirstTest-context.xml")
})
@RunWith(SpringJUnit4ClassRunner.class)
public class FirstTest {
...
}
@ContextHierarchy({
@ContextConfiguration("/test-db-setup-context.xml"),
@ContextConfiguration("SecondTest-context.xml")
})
@RunWith(SpringJUnit4ClassRunner.class)
public class SecondTest {
...
}
Run Code Online (Sandbox Code Playgroud)
过度使用带有locations参数的单个@ContextConfiguration,如下所示:
@ContextConfiguration(locations = {"classpath:test-db-setup-context.xml", "FirstTest-context.xml", "SecondTest-context.xml" })
Run Code Online (Sandbox Code Playgroud)
在每种情况下,应用程序上下文在不同的junit测试类之间共享.
我正在使用 React Native 为 iOS 和 Android 开发一个应用程序,并且我试图阻止应用程序中显示的特定于设备的缩放。
对于文本/字体大小缩放,将以下代码放在根级 App.js 文件中可以解决 iOS 和 Android 的问题:
if (Text.defaultProps == null) {
Text.defaultProps = {};
}
Text.defaultProps.allowFontScaling = false;
Run Code Online (Sandbox Code Playgroud)
但是,Android 设备具有以下仍在应用的显示尺寸设置:
我已尝试(未成功)将我在以下问题的答案中找到的针对此问题的各种“解决方案”拼凑在一起:
如果设置 -> 显示 -> 显示大小更改为大或小,则禁用应用程序或活动缩放
如何防止系统字体大小更改对 android 应用程序的影响?
我经常发现对BaseActivity扩展Activity类的类的引用。我的理解是,它是内部的类,我会写一个方法(我们称之为adjustDisplayScale)来进行更改Configuration的Context,我从拿到Resources,那之后我会打电话adjustDisplayScale的内部onCreate()方法后super.onCreate()的在MainApplication.java文件中。
到目前为止,在这个目录中我只有两个文件 -MainApplication.java和MainActivity.java.
我已经尝试创建一个新的模块和相关的包文件来adjustDisplayScale按照这些说明来实现,但它不起作用:https :
//facebook.github.io/react-native/docs/text.html
我已经尝试将实现的功能adjustDisplayScale中onCreate()喜欢这一点,它没有工作:
@Override …Run Code Online (Sandbox Code Playgroud) android android-context android-layout context-configuration react-native-android
当 Spring 上下文层次结构关闭时,没有保证 bean 被销毁的顺序,这样说是否正确?例如,子上下文中的 bean 将在父上下文之前被销毁。从一个最小的例子来看,上下文的破坏似乎在上下文之间完全不协调(奇怪的是)。两个上下文都注册了一个关闭钩子,该钩子稍后将在不同的线程中执行。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy({
@ContextConfiguration(classes = {ATest.Root.class}),
@ContextConfiguration(classes = {ATest.Child.class})
})
public class ATest {
@Test
public void contextTest() {
}
public static class Root {
@Bean
Foo foo() {
return new Foo();
}
}
public static class Child {
@Bean
Bar bar() {
return new Bar();
}
}
static class Foo {
Logger logger = LoggerFactory.getLogger(Foo.class);
volatile boolean destroyed;
@PostConstruct
void setup() {
logger.info("foo setup");
}
@PreDestroy
void destroy() {
destroyed = true;
logger.info("foo …Run Code Online (Sandbox Code Playgroud) 我在Spring引导应用程序中将RestTemplateBuilder与@ContextConfiguration一起使用时遇到问题(我尝试添加@ SpringBootTest,@ RunWith(SpringRunner.class)批注,但没有任何运气)。
任何帮助表示赞赏。这是背景:
我已经按照以下方式注释了我的课程:
@ContextConfiguration(classes = {
JsonNodeList.class,
JsonNodeUtils.class,
MyService.class,
RestClient.class,
RestTemplateBuilder.class}, loader = SpringBootContextLoader.class)
public class StepsDefinition {
Run Code Online (Sandbox Code Playgroud)
RestClient类的RestTemplateBuilder自动绑定为:
@Autowired
public RestClient(final RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = configureRestTemplate(restTemplateBuilder);
}
Run Code Online (Sandbox Code Playgroud)
MyService类自动连接RestClient。当我尝试使用加载应用程序@ContextConfiguration有SpringBootContextLoader,我收到以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplateBuilder': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.client.RestTemplateBuilder]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.boot.web.client.RestTemplateBuilder.<init>()
Run Code Online (Sandbox Code Playgroud) integration-testing spring-test resttemplate spring-boot context-configuration
有一个@Value注释的常量,在运行测试时没有被初始化,当构造函数中需要它时,它会抛出NullPointerException。
要测试的示例类:
class TestClass {
@Value("${test.value1}")
private String value1;
private final TestTemplate testTemplate;
public TestClass(TestTemplateBuilder builder) {
testTemplate = builder.someMethod(value1).build();
}
---
}
Run Code Online (Sandbox Code Playgroud)
测试类示例:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestClass.class)
@SpringBootTest
class TestClassTest {
@MockBean
TestTemplateBuilder builder;
@Autowired
TestClass testClass = new TestClass(testTemplate);
@Before
public void setUp() {
ReflectionTestUtils.setField(testClass, "value1", "VALUE");
Mockito.when(builder.build()).thenReturn(new TestTemplate());
}
---
}
Run Code Online (Sandbox Code Playgroud)
尝试了一些方法,但没有任何效果:
application.properties具有所需值的文件。application-test.properties并添加@TestPropertySource(locations="classpath:application-test.properties").@SpringBootTest(properties = { "test.value1=VALUE" })我也尝试过其他一些东西,但我得到的NullPoiterException是someMethod(value1).
版本:
java ×2
junit ×2
spring ×2
spring-boot ×2
spring-test ×2
android ×1
hierarchical ×1
mockito ×1
resttemplate ×1