-1
I have a spring-boot project and I am programming some tests using Junit, in Eclipse I ran the tests without having problems, however, in the terminal, while trying to execute the command mvn test or mvn build an error occurs in my Testutil class that never happened in Eclipse saying 'cannot find Symbol'.
Can’t find the class ?
That is the mistake:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[8,20] package org.hamcrest does not exist
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[9,20] package org.hamcrest does not exist
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[61,54] cannot find symbol
symbol: class TypeSafeDiagnosingMatcher
location: class br.eti.krebscode.ecommercespringboot.utils.TestUtil
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[70,54] cannot find symbol
symbol: class Description
location: class br.eti.krebscode.ecommercespringboot.utils.TestUtil.ZonedDateTimeMatcher
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[86,32] cannot find symbol
symbol: class Description
location: class br.eti.krebscode.ecommercespringboot.utils.TestUtil.ZonedDateTimeMatcher
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[69,9] method does not override or implement a method from a supertype
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[85,9] method does not override or implement a method from a supertype
[INFO] 7 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.309 s
[INFO] Finished at: 2018-06-24T00:15:51-03:00
[INFO] Final Memory: 31M/285M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project ecommercespringboot: Compilation failure: Compilation failure:
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[8,20] package org.hamcrest does not exist
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[9,20] package org.hamcrest does not exist
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[61,54] cannot find symbol
[ERROR] symbol: class TypeSafeDiagnosingMatcher
[ERROR] location: class br.eti.krebscode.ecommercespringboot.utils.TestUtil
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[70,54] cannot find symbol
[ERROR] symbol: class Description
[ERROR] location: class br.eti.krebscode.ecommercespringboot.utils.TestUtil.ZonedDateTimeMatcher
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[86,32] cannot find symbol
[ERROR] symbol: class Description
[ERROR] location: class br.eti.krebscode.ecommercespringboot.utils.TestUtil.ZonedDateTimeMatcher
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[69,9] method does not override or implement a method from a supertype
[ERROR] /home/andre/Documentos/workspace-sts/ecommercespringboot/src/main/java/br/eti/krebscode/ecommercespringboot/utils/TestUtil.java:[85,9] method does not override or implement a method from a supertype
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[
And the code of my Testutil class is this:
package br.ecommercespringboot.utils;
import java.io.IOException;
import java.nio.charset.Charset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.springframework.http.MediaType;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
public class TestUtil {
/** MediaType para JSON UTF8 */
public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(
MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
/**
* Converte um objeto para JSON byte array.
*
* @param object
* o objeto para converter
* @return JSON byte array
* @throws IOException
*/
public static byte[] convertObjectToJsonBytes(Object object)
throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
JavaTimeModule module = new JavaTimeModule();
mapper.registerModule(module);
return mapper.writeValueAsBytes(object);
}
/**
* Crie uma matriz de bytes com um tamanho específico preenchido com dados especificos.
*
*
* @param size tamanho do byte array
* @param data dados para colocar no byte array
* @return o JSON byte array
*/
public static byte[] createByteArray(int size, String data) {
byte[] byteArray = new byte[size];
for (int i = 0; i < size; i++) {
byteArray[i] = Byte.parseByte(data, 2);
}
return byteArray;
}
/**
* Um correspondente que testa que a cadeia examinada representa o mesmo instante que a data e hora de referência.
*/
public static class ZonedDateTimeMatcher extends TypeSafeDiagnosingMatcher<String> {
private final ZonedDateTime date;
public ZonedDateTimeMatcher(ZonedDateTime date) {
this.date = date;
}
@Override
protected boolean matchesSafely(String item, Description mismatchDescription) {
try {
if (!date.isEqual(ZonedDateTime.parse(item))) {
mismatchDescription.appendText("was ").appendValue(item);
return false;
}
return true;
} catch (DateTimeParseException e) {
mismatchDescription.appendText("was ").appendValue(item)
.appendText(", which could not be parsed as a ZonedDateTime");
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("a String representing the same Instant as ").appendValue(date);
}
}
/**
* Cria um correspondente que corresponde quando a cadeia examinada representa a mesma instância que a data e hora de referência
* @param date datetime de referência em relação ao qual a sequência examinada é verificada
*/
public static ZonedDateTimeMatcher sameInstant(ZonedDateTime date) {
return new ZonedDateTimeMatcher(date);
}
}
Maybe it’s worth you adding the
pom.xml
also.– StatelessDev