小编Din*_*lae的帖子

使用@Slf4j注释创建时如何模拟Logger?

我有一个带有 @Slf4j 注释的类。

我尝试编写一个测试并模拟记录器,但它不起作用。

@RequiredArgsConstructor
@Slf4j
public abstract class ExampleClass {
    
    protected final PropsClass properties;
    
    protected void logInfo(..) {
        log.info(...);
        clearMappedDiagnosticContext();
    }
}
Run Code Online (Sandbox Code Playgroud)

测试如下所示:

@RunWith(MockitoJUnitRunner.class)
public class ExampleClassTest {
    
    @Mock
    Logger logger;
    
    @Mock
    PropsClass properties;
    
    @InjectMocks
    ExampleClass exampleClass;
    
    @Test
    public void logSomethingtest() {
        ...
        exampleClass.logInfo(...);
        Mockito.verify(logger).info(marker, "foo bar {}", ...);
    }
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

Wanted but not invoked:
logger.info(
    MY_MARKER,
    "..........",
    "....",
    "....",
    0L
);

Actually, there were zero interactions with this mock.
Run Code Online (Sandbox Code Playgroud)

问题是,如何模拟 Logger?

java junit unit-testing slf4j mockito

17
推荐指数
1
解决办法
2万
查看次数

使用 Android Studio Profiler 时,Android 应用程序变慢

我尝试做的是获取有关游戏中英雄的大量数据并将其保存在数据库中,我使用 OkHttp 向服务器发出请求并使用 gson 解析 json,然后将我从 gson 获得的类保存到数据库中通过使用来自 Jetbrains 的名为 Exposed 的 ORM。调用在循环中进行,对于每个英雄将有 500 个关于他们最后一场比赛的结果,有 115 个英雄,我让线程在每个请求前休眠 1 秒。该应用程序开始运行得非常快,我什至无法在控制台中读取结果,但过了一会儿我得到了这个:

I/zygote: Background concurrent copying GC freed 72775(3MB) AllocSpace objects, 5(5MB) LOS objects, 49% free, 5MB/11MB, paused 416us total 1.664s
Run Code Online (Sandbox Code Playgroud)

然后应用程序运行得非常慢,我在数据库中获得 1 个条目/秒,那时我希望获得另一个 15000。在 logcat 中,我也得到了这个

03-29 12:01:00.050 1634-1651/? W/android.os.Debug: failed to get memory consumption info: -1
03-29 12:01:00.059 1634-1651/? E/memtrack: Couldn't load memtrack module
Run Code Online (Sandbox Code Playgroud)

应用程序没有崩溃,它只是变得非常慢,有趣的是,当我重新启动应用程序时,它立即变慢,第一次在 10 分钟后变慢,但后来一直很慢。同样在我重新启动后,它会写出类似的东西

V/StudioProfiler: Loaded classes: 8137
Run Code Online (Sandbox Code Playgroud)

如果应用程序刚刚启动,为什么会加载这么多类,这意味着什么我是初学者,我真的不明白这一切。有任何想法吗?

谢谢你。

android gson kotlin okhttp kotlin-exposed

6
推荐指数
0
解决办法
1698
查看次数

如何获取Android内部Downloads目录的路径?

我需要获取内部下载目录的路径。

我尝试过的事情:

val home = System.getProperty("user.home")
val downloadsDirectory = File("$home/Downloads"

val downloadsDirectory = File("/storage/emulated/0/Download")
Run Code Online (Sandbox Code Playgroud)

System.getProperty("user.home") 仅返回一个空字符串。

我也尝试过

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
Run Code Online (Sandbox Code Playgroud)

它返回此路径 /storage/emulated/0/Download 但 downloadsDirectory.listFiles() 方法返回 null,尽管我在目录中有一个文件。

android

4
推荐指数
1
解决办法
3690
查看次数

使用测试容器时如何解决“Can't get Docker image”?

我正在使用测试容器,直到现在它都工作得很好。我最近更新了 docker 和 IntelliJ,但我不知道这是否是导致以下问题的原因。

这是我使用的依赖项:

testCompile 'org.testcontainers:testcontainers:1.12.3'
testCompile 'org.testcontainers:oracle-xe:1.12.3'
Run Code Online (Sandbox Code Playgroud)

这是堆栈跟踪:

2020-10-12T13:13:06.709 WARN  testcontainers-ryuk [rg.testcontainers.utility.ResourceReaper]  - Can not connect to Ryuk at localhost:32778
java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at org.testcontainers.utility.ResourceReaper.lambda$start$1(ResourceReaper.java:114)
    at java.lang.Thread.run(Thread.java:748)

java.lang.ExceptionInInitializerError
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ...
Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageName=ryangoh/oracle_12c_r1_xe:latest)
    at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1153)
    at org.testcontainers.containers.GenericContainer.setDockerImageName(GenericContainer.java:1141)
    at org.testcontainers.containers.GenericContainer.<init>(GenericContainer.java:246)
    at org.testcontainers.containers.JdbcDatabaseContainer.<init>(JdbcDatabaseContainer.java:36)
    at org.testcontainers.containers.OracleContainer.<init>(OracleContainer.java:40)
    at …
Run Code Online (Sandbox Code Playgroud)

java spring-boot testcontainers

3
推荐指数
1
解决办法
7216
查看次数

Oracle中如何重命名序列?

我需要重命名表的序列。有很多表,而且它们很复杂,不希望删除任何内容。有没有办法给它们重命名?

我试过:

ALTER SEQUENCE ISEQ$$_149698 RENAME TO NEW_SEQUENCE;
RENAME ISEQ$$_149698 to NEW_SEQUENCE;
Run Code Online (Sandbox Code Playgroud)

第一个选项会引发以下错误:

SQL Error [2286] [42000]: ORA-02286: no options specified for ALTER SEQUENCE
Run Code Online (Sandbox Code Playgroud)

第二:

SQL Error [32799] [99999]: ORA-32799: cannot rename a system-generated sequence
Run Code Online (Sandbox Code Playgroud)

sql oracle sequence

3
推荐指数
1
解决办法
9541
查看次数

如何模拟 System 类?

我正在尝试更改 System.currentTimeMillis() 方法的行为以进行测试。我找到了下面的方法,但我不能在代码中使用 aspect 关键字。我真的不明白如何使用这种方法。有什么建议?

public aspect CurrentTimeInMillisMethodCallChanger {
    long around():
        call(public static native long java.lang.System.currentTimeMillis()) 
               && within(user.code.base.pckg.*) {
            return 0; // provide your own implementation returning a long
        }
}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅包含上述代码来源的相关答案

java aspectj aspect

1
推荐指数
1
解决办法
585
查看次数

如何设置 NSAttributedString 文本颜色?

我尝试将一些属性设置为 NSAttributedString。除了前景色外,一切正常。

这就是我尝试设置属性的方式:

func setLabelText(text:String){
    let strokeTextAttributes = [
        NSAttributedString.Key.strokeColor : UIColor.white,
        NSAttributedString.Key.foregroundColor : UIColor.red,
        NSAttributedString.Key.font : UIFont.init(name: "Raleway-ExtraBold", size: 26)!,
        NSAttributedString.Key.strokeWidth : 0.5,
    ]
      as [NSAttributedString.Key : Any]
    label.attributedText = NSMutableAttributedString(string: text, attributes: strokeTextAttributes)
}
Run Code Online (Sandbox Code Playgroud)

正如您在图像中看到的,它没有设置文本颜色:

在此输入图像描述

你知道为什么它会忽略foregroundColor属性吗?

提前致谢。

nsattributedstring ios swift nsattributedstringkey

0
推荐指数
1
解决办法
3274
查看次数