我想断言以确保检索到的数据在


打印数据1
2020-03-16 09:08:49
打印数据2
2020-03-16 09:09:15
断言data1和data2之间的时间间隔不超过2分钟,然后通过。


我有示例代码,这是最好的方法吗?
任何建议,请发表评论。

 //data1
 Date data1 = new Date();
 //data2
 Date data2 = new Date();
 //assert
 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 statement
dbQuery2 = /SELECT * FROM drugs.sync/


//Connect to PostgresSQL, global variable is stored at profile
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())

//print the "lastupdatedwm6" column for PULL
String lastupdatedwm6 = results.get(0).get('lastupdatedwm6')

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


WebUI.delay(2)
//Read data before 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);

	

最佳答案

@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);
}

关于java - 如何断言日期时间戳在2分钟内?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60703705/

10-13 07:07