Could it be that the code below is loaded during the JUnit test? These parameters are needed in many tests, and I just wanted to count these parameters only once.
class TestParams { static final Properties PROPERTIES = getProperties(); static final int UNIT_ID = getUnitID(); static final SerialParameters SERIAL_PARAMETERS = getSerialParameters(); private static Properties getProperties() { Properties properties = new Properties(); try (InputStream inputStream = TestParams.class.getClassLoader().getResourceAsStream("META-INF/ConnectionParams.properties")) { PROPERTIES.load(inputStream); } catch (IOException e) { fail(); e.printStackTrace(); } return properties; } private static SerialParameters getSerialParameters() { return new SerialParameters( PROPERTIES.getProperty("connection.port"), Integer.parseInt(PROPERTIES.getProperty("connection.baudRate"))); } private static int getUnitID() { return Integer.parseInt(PROPERTIES.getProperty("vlf-60.id"), 16); }
But when addressing somewhere in the tests in the @Before block like this:
params = TestParams.SERIAL_PARAMETERS;
I get a null output. An error java.lang.reflect.InvocationTargetException occurs on a string
Properties properties = new Properties();
It is strange that the simple creation of an object through the constructor takes place through reflection. Just how to avoid it?
UPD: But for some reason, it works well:
private static Properties getProperties() { Properties properties = null; try (InputStream inputStream = TestParams.class.getClassLoader().getResourceAsStream("META-INF/ConnectionParams.properties")) { properties = new Properties(); properties.load(inputStream); } catch (IOException e) { fail(); e.printStackTrace(); } return properties; }
getProperties()
method refer to thePROPERTIES
field, which must be initialized with the result of the method operation. - zRrr