Skip to content

Commit 6fd7bf7

Browse files
Fixed test not to rely on changing date but on fixed one (#76)
1 parent d4f36f6 commit 6fd7bf7

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

decoder/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,9 @@ dependencies {
5858
testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.1"
5959
testImplementation "org.junit.jupiter:junit-jupiter-params:5.7.1"
6060
testImplementation "org.hamcrest:hamcrest:2.2"
61+
testImplementation "org.mockito:mockito-core:3.9.0"
62+
testImplementation "org.mockito.kotlin:mockito-kotlin:3.2.0"
63+
testImplementation "org.mockito:mockito-inline:2.7.21"
64+
6165
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.1"
6266
}

decoder/src/test/java/dgca/verifier/app/decoder/QrCodeTests.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,45 @@ import dgca.verifier.app.decoder.prefixvalidation.PrefixValidationService
2020
import dgca.verifier.app.decoder.schema.DefaultSchemaValidator
2121
import dgca.verifier.app.decoder.schema.SchemaValidator
2222
import dgca.verifier.app.decoder.services.X509
23+
import org.junit.After
2324
import org.junit.Assert
2425
import org.junit.Assert.assertTrue
26+
import org.junit.Before
2527
import org.junit.Test
28+
import org.mockito.MockedStatic
29+
import org.mockito.Mockito.*
2630
import java.io.ByteArrayInputStream
2731
import java.io.IOException
2832
import java.io.InputStream
2933
import java.security.cert.CertificateException
3034
import java.security.cert.CertificateFactory
3135
import java.security.cert.X509Certificate
36+
import java.time.Clock
37+
import java.time.Instant
3238
import java.util.*
3339

40+
3441
class QrCodeTests {
42+
43+
private var clockMock: MockedStatic<Clock>? = null
44+
45+
@Before
46+
fun setup() {
47+
mockInstant(1640000000) // set desired return value 2021-12-20T11:33:20Z
48+
}
49+
50+
@After
51+
fun destroy() {
52+
clockMock!!.close()
53+
}
54+
55+
private fun mockInstant(expected: Long) {
56+
val spyClock: Clock = spy(Clock::class.java)
57+
clockMock = mockStatic(Clock::class.java)
58+
clockMock!!.`when`<Any> { Clock.systemUTC() }.thenReturn(spyClock)
59+
`when`(spyClock.instant()).thenReturn(Instant.ofEpochSecond(expected))
60+
}
61+
3562
@Throws(CertificateException::class)
3663
fun toCertificate(pubKey: String?): X509Certificate {
3764
val `in` = Base64.getDecoder().decode(pubKey)

0 commit comments

Comments
 (0)