9

I already checked with most similar questions and could not find a helpful answer.

I am making a WebApplication via JHipster using Maven v3.3.9. Among other things, there is a PDF-generation that takes place. The web app is multilingual and uses the

/resources/i18n/..

resource files for the different translation. On live testing everything works perfectly with no problems or errors.

I am now trying to create a test which will generate a PDF and compare it to an existing PDF so development can continue. The problem is, no matter which locale I use for testing, I get the same error:

org.springframeworkcontext.NoSuchMessageException: No message found under code 'pdf.klasse' for locale 'de'

I am not sure what the cause of the error is. Since the messages are there and during live testing they work perfectly.

Here is the test code:

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

}


}

The PDFGenerator creates the testPDF2.pdf file and saves it.

UPDATE Adding the:

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

to the config did not do much. I also changed the locale from

String locale = "de"

to

Locale locale = Locale.GERMAN;

and I threw in the line:

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

directly at the beginning of the test. I get the same error. It seems the problem is not in finding the file but in reading it.

Emir Masic
  • 184
  • 1
  • 2
  • 8

3 Answers3

6

In your config you need :

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

My test is

@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));
    }
}
Essex Boy
  • 7,565
  • 2
  • 21
  • 24
  • Thanks for the quick reply mate. I added the config to the test but to no avail. I don't think the problem is in finding the file (since the file is located in both /src/test/resource as well as /src/main/resource) but it seems more like something with the locale, as if the file is being "read wrong" for whatever reason... I believe, I am trying all possible solutions and will update as soon as i find an answer. – Emir Masic Jan 16 '17 at 11:01
  • Thank you very much for pointing that bean out. It was creating an empty messageSource (DelegatedMessageSource) which is why the NotNull test kept returning true. However the object itself was empty and missing the path. Once I added the Bean to the Application.java it worked perfectly. :) Cheers – Emir Masic Jan 17 '17 at 08:29
2

My solution.

Declare MessageSource in the test class and autowired DelegatingMessageSource in the test class.

private MessageSource messageSource;

@Autowired
private DelegatingMessageSource delegatingMessageSource;

Create the setup as shown below

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

It should work fine.

2

I'm not yet sure why, but it seems that MessageSource autoconfiguration was not read during tests, I had to add it explicitly:

@SpringBootTest(classes = {
        StrategyValidationAutoconfiguration.class,
        MessageSourceAutoConfiguration.class
})
Vitaly Chura
  • 704
  • 8
  • 13