mardi 4 août 2015

Passing System properties into a void method in jUnit

I'm trying to unit test some code utilizing Hazelcast. Most of the code is straightforward, but I've been having hangups with the class constructor.

public ClientViewer() {
    setup();
}

private void setup() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getGroupConfig.setName(System.getProperty("hazelcast.group")).setPassword(System.getProperty("hazelcast.password"));
    clientConfig.getNetworkConfig().addAddress(System.getProperty("hazelcast.url"));
    client= HazelcastClient.newHazelcastClient(clientConfig);
}

So any time I try to create a test object to call/test its methods, it keeps throwing an IllegalArgumentException. So I decided to set each property individually before creating the test object using test data.

TEST_GROUP = "testGroup";
TEST_PASSWORD = "testPassword";
TEST_URL = "testUrl";

System.setProperty("hazelcast.group", TEST_GROUP);
System.setProperty("hazelcast.password", TEST_PASSWORD);
System.setProperty("hazelcast.url", TEST_URL);

But now I'm getting an InvocationTargetException. What am I missing? Is there a way to fool the setup method without dependency injection?

(BTW: I'm aware dependency injection is kind of an obvious good practice, but that idea is somewhat lost among my peers. Suffice to say, that's not an option in this scenario.)

Aucun commentaire:

Enregistrer un commentaire