有没有办法在调用类的任何其他函数之前始终执行函数?
我有一个类,我需要在调用任何函数之前总是刷新一些字段:
public class Example {
private int data;
public void function1(){
}
public void function2(){
}
//@BeforeOtherFunction
private void refresh(){
// refresh data
}
}
Run Code Online (Sandbox Code Playgroud)
因为它似乎是糟糕的编程,所以我不想refresh在每个其他函数的开头调用。由于其他人也将在此项目上工作,因此存在危险,即有人扩展了调用而没有调用refresh。
JUnit 有一个带有@Before-Annotation的解决方案。有没有办法在其他课程中做到这一点?
顺便说一句:如果你知道一种编程模式,它以另一种方式解决这个问题,而不是每次调用任何函数时都执行一个函数,那也会非常有帮助!
由于迁移到 Spring Boot 3,当我运行 Maven Spring Boot goal 时,我的应用程序不再停留在断点处mvn spring-boot:run。
这是因为 Spring 分叉了线程或进程,并且调试器未附加到此。
在以前的 Spring-Boot 版本中,您可以通过传递来禁用分叉-Dspring-boot.run.fork=false(请参阅如何使用 IntelliJ IDEA 社区版调试 spring-boot 应用程序?)。
不幸的是,这个选项已被删除,您可以在Spring Boot 3.0 迁移指南中阅读:
Spring Boot 2.7 中已弃用的 spring-boot:run 和 spring-boot:start 的 fork 属性已被删除。
有没有可能让断点再次起作用?
当然IntelliJ Ultimate有更好的Spring Boot集成。我正在尝试使其与社区版一起使用。
我还尝试不运行 Maven 目标,而是进行应用程序运行配置。到目前为止,这失败了,因为java.lang.ClassNotFoundException它找不到 Main 类。不确定我是否应该进一步调查该选项。
最后一个想法是使用调试选项启动 Maven 目标,以便可以附加外部调试器。这也不起作用,即使可以,我也只能在启动后附加调试器,这将使调试 spring 上下文的创建变得不可能。
Espresso 标榜的功能是它总是等待 Android 的 UI-Thread 空闲,这样您就不必处理任何时间问题。但我似乎发现了一个例外:-/
设置是在每个片段中ViewPager带有一个EditText。我希望 EspressoEditText在第一个片段中输入文本,滑动到第二个片段并EditText在该片段中执行相同的操作(3 次):
@MediumTest
public void testSwipe() throws InterruptedException {
onView(withIdInActiveFragment(EXTERN_HOURS_INPUT))
.perform(typeText("8.0"));
onView(withIdInActiveFragment(DAY_PAGER))
.perform(swipeLeft());
//Thread.sleep(2000); // <--- uncomment this and the test runs fine
onView(withIdInActiveFragment(EXTERN_HOURS_INPUT))
.perform(typeText("8.0"));
onView(withIdInActiveFragment(DAY_PAGER))
.perform(swipeLeft());
//Thread.sleep(2000);
onView(withIdInActiveFragment(EXTERN_HOURS_INPUT))
.perform(typeText("8.0"));
onView(withIdInActiveFragment(DAY_PAGER))
.perform(swipeLeft());
}
public static Matcher<View> withIdInActiveFragment(int id) {
return Matchers.allOf(withParent(isDisplayed()), withId(id));
}
Run Code Online (Sandbox Code Playgroud)
但是在执行第一次滑动时出现此错误:
android.support.test.espresso.AmbiguousViewMatcherException: '(has parent matching: is displayed on the screen to the user and with id: de.cp.cp_app_android:id/extern_hours_input)' matches multiple views in the …Run Code Online (Sandbox Code Playgroud)