Junit 5 Pioneer @DefaultTimeZone

JUnit 5 pioneer provides @DefaultTimeZone annotation to temporarily change the timezone value for test execution. Once the test execution completes, its default value will be restored.

package com.example.junit5.sujan;

import java.util.TimeZone;

public class AppleCalculator {
    TimeZone getShortZoneId() {
        TimeZone timeZone = TimeZone.getTimeZone("CET");
        return timeZone;
    }

    TimeZone getLongZoneId() {
        TimeZone timeZone = TimeZone.getTimeZone("America/Los_Angeles");
        return timeZone;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.DefaultTimeZone;

import java.util.TimeZone;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AppleCalculatorTest {
    @Test
    @DefaultTimeZone("CET")
    void shortZoneIdTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(TimeZone.getDefault(), appleCalculator.getShortZoneId());
    }

    @Test
    @DefaultTimeZone("America/Los_Angeles")
    void longZoneIdTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(TimeZone.getDefault(), appleCalculator.getLongZoneId());
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.junit-pioneer:junit-pioneer:0.6.0'
}

 

follow us on