Search code examples
unit-testingmaven-3

Unit Tests passing in IDE and pipeline but failing with mvn clean install


I'm working in a project with Spring Boot and Java 11.

I have created the following UT, that when I launch it with the IDE (IntelliJ) it passes correctly and when I commit and push it to the pipeline in GitLab it passes as well. BUT when I launch a mvn clean install in my local it gives me the following error:

 @Test
    public void process_test() throws Exception {

        ClassPathResource resource = new ClassPathResource("data/data.csv");
        StringBuffer reponse = reader.process(resource.getInputStream());

        File logs = File.createTempFile("tempFile-", ".txt");
        var out = new FileWriter(logs);
        out.write(reponse.toString());
        out.close();

        String resultatLog =  Files.readString(logs.toPath());

        assertTrue(resultatLog.contains("abcd"));

        logs.deleteOnExit();
    }
[INFO] 
[ERROR] Errors: 
[ERROR]   ReaderTest.process_test:170 » MalformedInput

The line that gives that error is the following:

String resultatLog =  Files.readString(logs.toPath());

and I have tried to insert the charset also: (the charset is also defined in the POM)

String resultatLog =  Files.readString(logs.toPath(), Charsets.UTF_8);

In the gitlab-ci.yml I have the following:

build:maven:
  image: maven:3.6.0-jdk-11
  stage: build
  script:
    - "mvn clean install -B -U"
...

unit:test:
  image: maven:3.6.0-jdk-11
  stage: test
  script:
    - "mvn test -B -U"

I don't know why there are differences in the tests results between the pipeline/ide launch and the local mvn clean install.

Could anybody help me correct my mvn clean install?

Thank you very much!


Solution

  • Finally I've resolved the problem doing a refactor of the code:

    @Test
        public void process_test() throws Exception {
    
            ClassPathResource resource = new ClassPathResource("data/data.csv");
            StringBuffer reponse = reader.process(resource.getInputStream());
    
            File logs = File.createTempFile("tempFile-", ".txt");
            var out = new FileWriter(logs);
            out.write(reponse.toString());
            out.close();
    
            //Modified code
            byte[] b = {};
            try{
                b = Files.readAllBytes(logs.toPath());
            }catch (IOException e) {
                e.printStackTrace();
            }
            String resultatLog = new String(b);
            //End of the modified code
    
            assertTrue(resultatLog.contains("abcd"));
    
            logs.deleteOnExit();
        }
    

    I still doesn't know why the mvn clean install it was failing, so if anybody knows I still want to know why it was wrong before.