小编Ash*_*mar的帖子

覆盖Java System.currentTimeMillis以测试对时间敏感的代码

有没有办法,无论是在代码中还是在JVM参数中,都可以覆盖当前时间,如System.currentTimeMillis手动更改主机上的系统时钟所示?

一点背景:

我们有一个系统可以运行许多会计工作,这些工作围绕当前日期(即本月的第一天,一年中的第一天等)展开大部分逻辑.

不幸的是,很多遗留代码都会调用诸如new Date()or之类的函数,这些函数Calendar.getInstance()最终都会调用System.currentTimeMillis.

出于测试目的,我们现在仍然需要手动更新系统时钟来操作代码认为运行测试的时间和日期.

所以我的问题是:

有没有办法覆盖返回的内容System.currentTimeMillis?例如,告诉JVM在从该方法返回之前自动添加或减去一些偏移量?

提前致谢!

java testing jvm systemtime

117
推荐指数
7
解决办法
7万
查看次数

BufferedReader和BufferedInputStream之间的区别

之间有什么区别BufferedReader,BufferedInputStreamScanner在Java?BufferedReader读取文本并BufferedInputStream读取byte.除此之外有什么区别吗?

java io bufferedreader java.util.scanner

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

Spring中的DispatcherServlet和ContextLoaderListener

DispatcherServletContextLoaderListenerSpring框架有什么区别?当我们使用spring框架时,我们是否需要在web.xml中配置它们?

configuration spring

8
推荐指数
2
解决办法
6084
查看次数

Java I/O - Reuse InputStream Object

Is there anyway to reuse an inputStream by changing its content? (Without new statement).

For instance, I was able to something very close to my requirement, but not enough

In the following code I am using a SequenceInputStream, and everytime I am adding a new InputStream to that sequence.

But I would like to do the same thing by using the same inputStream (I don't care which implementation of InputStream).

I thought about mark()/reset() APIs, but …

java inputstream

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

在JAVA中生成特定范围内的随机日期

如何在JAVA中生成特定范围内的随机日期?我已经看到如何在Java中的特定范围内生成随机整数?生成随机数的链接.在JAVA中有类似/其他种类的方式来生成随机数吗?

java algorithm date

6
推荐指数
3
解决办法
2万
查看次数

如何使用数组流处理整数数组的和是否超过int max(2147483647)?

我有一个整数数组,想对所有元素求和。只要sum小于或等于int max值(2147483647),它就可以正常工作,但是当sum超过int max值时,它就会失败。以下是示例-:

int [] iArr = new int[]{2147483646 , 1 , 1};
    System.out.println(Arrays.stream(iArr).sum());
Run Code Online (Sandbox Code Playgroud)

结果-:--2147483648

如我们所见,以上结果是不正确的。我知道可以通过使用长数组代替int数组来解决它,如下所示。

long [] iArr = new long[]{2147483646 , 1 , 1};
    System.out.println(Arrays.stream(iArr).sum());
Run Code Online (Sandbox Code Playgroud)

结果-:2147483648

但是我不想使用长数组,因为我知道数组元素的最大大小限制是int max value(2147483647)。因此,我不想浪费内存。还有其他方法可以使用Array Stream对整数数组的总和((数组元素的总和)超过int max(2147483647))进行计算吗?

arrays java-8 java-stream

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

@TestSubject和@InjectMocks之间的区别?

在学习Mockito时,我在下面的参考文献中找到了两个不同的注释@TestSubject@InjectMocks.
@TestSubject Ref
@InjectMocks Ref

@InjectMocks annotation正如教程中所解释的那样工作得很好但@TestSubject不起作用而不是显示错误.
我在下面的代码片段中收到TestSubject cannot be resolved to a type@TestSubject注释错误,但是我已经做了正确的设置(包括构建路径中的JunitMockito jar文件).

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import com.infosys.junitinteg.action.MathApplication;
import com.infosys.junitinteg.service.CalculatorService;

@RunWith(MockitoJUnitRunner.class)
public class MathApplicationTester {

    // @TestSubject annotation is used to identify class which is going to use
    // the mock object
    @TestSubject
    MathApplication mathApplication = new MathApplication();

    // @Mock annotation is …
Run Code Online (Sandbox Code Playgroud)

java junit mockito java-8

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

JMeter中的循环计数和加速周期

我已经创建了一个集合,只是将其与“循环计数”和“上升时间”相混淆。我有一个带有以下参数的测试仪。

Threads        =  30
Ramp Up Period =  30
Loop Count     =  100
Run Code Online (Sandbox Code Playgroud)

按照quora上的页面 。

我想: 在此处输入图片说明

a) If Loop count is Zero, then each of the 30 threads will be starting every second. As per the shared the web page, I guess 30/30 * 100 ie 100 threads/requests will be hitting the server every second. Please correct me if I am wrong.  

b) As per the above parameters, there will be a total of 30 * 100 threads/requests. …
Run Code Online (Sandbox Code Playgroud)

jmeter

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