java 测试期间未正确读取MessageSource

btxsgosb  于 2023-01-24  发布在  Java
关注(0)|答案(3)|浏览(139)

我已经检查了大多数类似的问题,找不到一个有帮助的答案。
我正在使用Maven v3.3.9通过JHipster制作一个Web应用程序。除此之外,还有一个PDF生成过程。该Web应用程序是多语言的,使用
/资源/i18 n/..
资源文件的不同翻译。在现场测试一切工作完美,没有任何问题或错误。
我现在正在尝试创建一个测试,它将生成一个PDF并将其与现有的PDF进行比较,以便继续开发。问题是,无论我使用哪个语言环境进行测试,我都得到相同的错误:

我不确定错误的原因是什么。由于消息在那里,并且在现场测试期间,它们工作得很完美。
下面是测试代码:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DeviceDataCheckApp.class)
public class PDFGeneratorTest
{
private MockMvc mockMvc;

private CalculationMapperImpl calculationMapperImpl;

private Calculation calculation;
private CalculationDTO calculationDTO;
private CalculationResult calculationResult;
private String langval = "de";
private String name = "";

private PDFGenerator pdfGenerator;

@Autowired
private MessageSource messageSource;

@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setup() throws IOException, URISyntaxException, IllegalAccessException, InstantiationException
{
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

    calculation = new Calculation()
            .producerName("test")
            .airVolumeSupply(2.0)
            .airVolumeExhaust(2.0)
            .statedAirVelocitySupply(2.0)
            .statedAirVelocityExhaust(2.0)
            .staticPressureLossSupply(2.0)
            .staticPressureLossExhaust(2.0)
            .pMSupply(2.0)
            .pMExhaust(2.0)
            .pMAnfSupply(2.0)
            .pMAnfExhaust(2.0)
            .pressureLossWrgSupply(2.0)
            .pressureLossWrgExhaust(2.0)
            .pressureLossMediumSupply(2.0)
            .pressureLossMediumExhaust(2.0)
            .enginePowerRotor(0.0)
            .dryThermalPerformance(0.0)
            .ofHeatRecovery(2.0)
            .wrgType(WrgType.PLATTENTAUSCHER)
            .earlyPressureLossFilterSupply(2.0)
            .earlyPressureLossFilterExhaust(2.0)
            .dimensioningPressureLossFilterSupply(2.0)
            .dimensioningPressureLossFilterExchaust(2.0)
            .referenceConfigurationFilterSupply(false)
            .referenceConfigurationFilterExhaust(false)
            .additionalFilterStageSupply(false)
            .additionalFilterStageExhaust(false)
            .hepaFilterSupply(false)
            .hepaFilterExhaust(false)
            .gasFilterSupply(false)
            .gasFilterExhaust(false)
            .highPerformanceCondenserSupply(false)
            .highPerformanceCondenserExhaust(false)
            .heightInnerDimensionSupply(2.0)
            .heightInnerDimensionExhaust(2.0)
            .depthInnerDimensionSupply(2.0)
            .depthInnerDimensionExhaust(2.0);

    calculationMapperImpl = new CalculationMapperImpl();
    calculationDTO = calculationMapperImpl.calculationToCalculationDTO(calculation);

    calculationResult = new CalculationResult();
    calculationResult.setRealAreaSA(0.00);
    calculationResult.setRealAreaEA(0.00);
    calculationResult.setRealAirVelocitySA(138.89);
    calculationResult.setRealAirVelocityEA(138.89);
    calculationResult.setCheckedVClassSA("V9");
    calculationResult.setCheckedVClassEA("V9");
    calculationResult.setDeclaredVClassSA("V4");
    calculationResult.setDeclaredVClassEA("V4");

    calculationResult.setpMRefSA(0.00);
    calculationResult.setpMRefEA(0.00);
    calculationResult.setpMMaxSA(0.00);
    calculationResult.setpMMaxEA(0.00);
    calculationResult.setpClassSA("-");
    calculationResult.setpClassEA("-");

    calculationResult.setPressureLossWrg(4);
    calculationResult.setPressureLossMedium(0);
    calculationResult.setRatedAirVolume(2);
    calculationResult.setDryThermalPower(0.00);
    calculationResult.setAuxiliaryPower(0.00);
    calculationResult.setElectricalPowerInput(0.00);
    calculationResult.setPerformanceReferenceNumber(72.29);
    calculationResult.setEfficiency(1.97);
    calculationResult.sethClass("H6");

    calculationResult.setpSfpVSA(3600000.00);
    calculationResult.setpSfpVEA(3600000.00);
    calculationResult.setExtraPSfpSA(0);
    calculationResult.setExtraPSfpEA(0);
    calculationResult.setSfpVClassSA("SFP7");
    calculationResult.setSfpVClassEA("SFP7");

    calculationResult.setSystemEfficiencySA(0.00);
    calculationResult.setSystemEfficiencyEA(0.00);

    calculationResult.setInternalPressureLossSA(2);
    calculationResult.setInternalPressureLossEA(2);
    calculationResult.setInternalPressureLossTotal(4);
    calculationResult.setSfpIntSA(3600000);
    calculationResult.setSfpIntEA(3600000);
    calculationResult.setSfpIntTotal(7200000);

    calculationResult.seteBonus2016(0);
    calculationResult.seteBonus2018(0);
    calculationResult.setfCorrection2016(360);
    calculationResult.setfCorrection2018(340);
    calculationResult.setSfpMax2016(840);
    calculationResult.setSfpMax2018(760);
    calculationResult.setAchieved2016(false);
    calculationResult.setAchieved2018(false);


    pdfGenerator = new PDFGenerator(calculationDTO, calculationResult, langval, messageSource, name);
}

@Test
public void compareFilesTest() throws IOException
{
    System.out.println(calculation);

    File f1 = new File("../PDFData/testPDF.pdf");
    File f2 = new File("../PDFData/testPDF2.pdf");

    boolean areSame = FileUtils.contentEquals(f1, f2);
    System.out.println("Files were compared...");
    assertTrue(areSame);

}

}

PDF生成器创建testPDF2.pdf文件并保存。

更新添加:

@PropertySource("classpath:/src/main/resources/i18n/messages_de.properties")

到配置没有做太多。我还改变了区域设置从

String locale = "de"

Locale locale = Locale.GERMAN;

然后我加了一句

System.out.println(messageSource.getMessage("pdf.klasse", null, this.locale));

直接在测试开始的时候。我得到了同样的错误。看起来问题不在于找到文件而是在于阅读它。

kwvwclae

kwvwclae1#

在您的配置中,您需要:

@Bean
public ResourceBundleMessageSource messageSource() {
    ResourceBundleMessageSource source = new ResourceBundleMessageSource();
    source.setBasenames("i18n/messages");
    return source;
}

我的测试是

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Test1 {

    @Autowired
    private MessageSource messageSource;

    @Test
    public void test1() {
        assertNotNull(messageSource);
        assertEquals("test greg de", messageSource.getMessage("pdf.klasse", null, Locale.GERMANY));
    }
}
ipakzgxi

ipakzgxi2#

我的解决方案。
在测试类中声明MessageSource,并在测试类中自动连接DelegatingMessageSource。

private MessageSource messageSource;

@Autowired
private DelegatingMessageSource delegatingMessageSource;

创建如下所示的设置

@Before
public void setUp() {
  messageSource = Mockito.mock(MessageSource.class);
  when(messageSource.getMessage(anyString(), any(Object[].class),any(Locale.class))).thenReturn("");
  delegatingMessageSource.setParentMessageSource(messageSource);
}

应该没问题。

vbopmzt1

vbopmzt13#

我还不知道为什么,但似乎在测试期间没有读取MessageSource自动配置,我不得不显式地添加它:

@SpringBootTest(classes = {
        StrategyValidationAutoconfiguration.class,
        MessageSourceAutoConfiguration.class
})

相关问题