如何断定日期时间戳是否在2分钟之内?

huangapple 未分类评论59阅读模式
标题翻译

how to assert date time stamp is within 2 minutes?

问题

以下是您要翻译的内容:

我想要添加断言来确保检索到的数据在以下时间范围内:

  1. 打印数据1
    2020-03-16 09:08:49

  2. 打印数据2
    2020-03-16 09:09:15

  3. 断言数据1和数据2之间的时间间隔不超过2分钟,通过。

我有一些示例代码,这是最佳方法吗?
有什么建议,请评论。

// data1 
Date data1 = new Date();
// data2 
Date data2 = new Date();
// 断言
assertThat(data1, DateMatchers.within(2, ChronoUnit.MINUTES, data2 ));

最新脚本:

import static org.assertj.core.api.Assertions.*
import java.sql.*
import java.text.SimpleDateFormat
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date
import com.kms.katalon.core.configuration.RunConfiguration

GlobalVariable.TestIssueKey = null

WebUI.delay(1)

// SQL 语句
dbQuery2 = /SELECT * FROM drugs.sync/

// 连接到 PostgresSQL,全局变量存储在配置文件中
List results = CustomKeywords.'test.database.getPostgresSQLResults'(GlobalVariable.dbConnString2 , GlobalVariable.dbUsername2 , GlobalVariable.dbPassword2 ,GlobalVariable.dbDriver2 ,dbQuery2 )

// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss", Locale.ENGLISH)
String date = sdf.format(new Date())

// 打印 "lastupdatedwm6" 列用于 PULL
String lastupdatedwm6 = results.get(0).get('lastupdatedwm6')

// 将 lastupdatedwm6 存储到文件
def lastupdatedwm6aft = new File(RunConfiguration.getProjectDir() + "/Data Files/lastupdatedwm6aft.txt")
lastupdatedwm6aft.newWriter().withWriter { it << lastupdatedwm6 }
println lastupdatedwm6aft.text

WebUI.delay(2)
// 在 drugsync Pull 之前读取数据
def lastupdatedwm6bef = new File(RunConfiguration.getProjectDir() + "/Data Files/lastupdatedwm6bef.txt")

Date data1 = sdf.parse(lastupdatedwm6bef.text);
Date data2 = sdf.parse(lastupdatedwm6aft.text);

long diffInMillies = Math.abs(data2.getTime() - data1.getTime());
long minute_millis_2 = 2 * 60 * 1000;
long diffTime = minute_millis_2 - diffInMillies;
assertTrue(diffTime > 0);

希望这些翻译对您有所帮助。如果您有任何其他问题,请随时问我。

英文翻译

I want to have assertion to make sure the data retrieved is within

  1. print data1
    2020-03-16 09:08:49

  2. print data2
    2020-03-16 09:09:15

  3. assertThat time gap between data1 and data2 is not more than 2 minute then Passed.

I have sample codes, is this the best way?
any suggestion, please comment.

 //data1 
 Date data1 = new Date();
 //data2 
 Date data2 = new Date();
 //assert
 assertThat(data1, DateMatchers.within(2, ChronoUnit.MINUTES, data2 ));

latest script

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

import static org.assertj.core.api.Assertions.*

import java.sql.*
import java.text.SimpleDateFormat

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable as GlobalVariable
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date
import com.kms.katalon.core.configuration.RunConfiguration

GlobalVariable.TestIssueKey = null


WebUI.delay(1)


//SQL statement
dbQuery2 = /SELECT * FROM drugs.sync/


//Connect to PostgresSQL, global variable is stored at profile
List results = CustomKeywords.&#39;test.database.getPostgresSQLResults&#39;(GlobalVariable.dbConnString2 , GlobalVariable.dbUsername2 , GlobalVariable.dbPassword2 ,GlobalVariable.dbDriver2 ,dbQuery2 )

//SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS&#39;Z&#39;&quot;)
SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-mm-dd hh:mm:ss&quot;, Locale.ENGLISH)
String date = sdf.format(new Date())

//print the &quot;lastupdatedwm6&quot; column for PULL
String lastupdatedwm6 = results.get(0).get(&#39;lastupdatedwm6&#39;)

//store the lastupdatedwm6 to file
def lastupdatedwm6aft = new File(RunConfiguration.getProjectDir() + &quot;/Data Files/lastupdatedwm6aft.txt&quot;)
lastupdatedwm6aft.newWriter().withWriter { it &lt;&lt; lastupdatedwm6 }
println lastupdatedwm6aft.text


WebUI.delay(2)
//Read data before drugsync Pull
def lastupdatedwm6bef = new File(RunConfiguration.getProjectDir() + &quot;/Data Files/lastupdatedwm6bef.txt&quot;)


	Date data1 = sdf.parse(lastupdatedwm6bef.text);
	Date data2 = sdf.parse(lastupdatedwm6aft.text);

	long diffInMillies = Math.abs(data2.getTime() - data1.getTime());
	long minute_millis_2 = 2 * 60 * 1000;
	long diffTime = minute_millis_2 - diffInMillies;
	assertTrue(diffTime &gt; 0);

<!-- end snippet -->

答案1

得分: 0

@Test
public void test() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss", Locale.ENGLISH);

    Date data1 = sdf.parse("2020-03-16 09:08:49");
    Date data2 = sdf.parse("2020-03-16 09:09:49");

    long diffInMillies = Math.abs(data2.getTime() - data1.getTime());
    long minute_millis_2 = 2 * 60 * 1000;
    long diffTime = minute_millis_2 - diffInMillies;
    assertTrue(diffTime > 0);
}
英文翻译
@Test
public void test() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-mm-dd hh:mm:ss&quot;, Locale.ENGLISH);

    Date data1 = sdf.parse(&quot;2020-03-16 09:08:49&quot;);
    Date data2 = sdf.parse(&quot;2020-03-16 09:09:49&quot;);

    long diffInMillies = Math.abs(data2.getTime() - data1.getTime());
    long minute_millis_2 = 2 * 60 * 1000;
    long diffTime = minute_millis_2 - diffInMillies;
    assertTrue(diffTime &gt; 0);
}

答案2

得分: 0

另一种方法是使用超时。

// 如果测试时间超过1000毫秒,这个测试将会失败
@Test(timeout=1000)
public void test() {
  objectToTest.methodThatTakesALongTime();
}
英文翻译

Another approach is to use timeout.

// This will fail if the test takes more than 1000 milliseconds
@Test(timeout=1000)
public void test() {
  objectToTest.methodThatTakesALongTime();
}

huangapple
  • 本文由 发表于 2020年3月16日 17:52:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/60703705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定