Total Pageviews

2020/12/01

[Java] [JUnit] [SpringBoot] How to assign active profile in unit test?

Requirement
If I have multiple profiles in Spring Boot project, how to assign specific profile in my unit test program?


How-To
Added @ActiveProfiles onto your unit test class:
package com.test.tool.filegenerator.ftl.cp937;

import com.test.tool.filegenerator.util.Cp973FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("office")
public class TS0110_050110_70118_R01FileTest {

    @Autowired
    private Cp973FileUtils cp973FileUtils;

    @ParameterizedTest
    @ValueSource(strings = {"delivery/test data/LN3112P2.LN3112P2"})
    public void testReadTS0110_050110_70118_R01(String file) {
        Assertions.assertThatCode(
                () -> cp973FileUtils.readAndPrintContent(file)).doesNotThrowAnyException();
    }

}


No comments: