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; } 
  • write the whole spectrum - Artem Konovalov
  • 3
    you in the getProperties() method refer to the PROPERTIES field, which must be initialized with the result of the method operation. - zRrr
  • one
    @zRrr make the answer. - andreycha
  • Yes, exactly, the type of cycle is obtained. "PROPERTIES.load (inputStream);" This is a missprint. C "properties.load (inputStream);" works. That's just not clear why it falls earlier, on the line "Properties properties = new Properties ();". - Eugene
  • How can I make a message a response? There is no such button - Eugene

0