Thursday, 8 August 2013

How to save any object which is declared as a class variable and initialized inside a test in JUnit

How to save any object which is declared as a class variable and
initialized inside a test in JUnit

I am designing test cases in JUnit. My question is how to save any object
which is declared as a class variable and initialized inside a test.
Currently in such cases I am getting java.lang.NullPointerException.
Below is the brief description- I have three services listed below which
needs to be tested-
Service 1(login): It accept a username password pair and returns cookies
in response.
Service 2(postmessage): It accept a message, which must be supplied with
cookies in the request and returns an unique id of posted message
Service 3(markasread): It accept a message id, which must be supplied with
cookies. The message id, which should be used in Service 3 is returned by
service 2.
This is what I was expecting to work-
import static org.junit.Assert.*;
import org.junit.Test;
public class ProfileTest{
private String cookie;
private String messageId;
@Test
public void loginTest(){
String username = "demouser";
String password = "demopassword";
HttpResponse response = login(username, password);
cookie = response.getCookie();
assertEquals(response.getResponse(), "success");
}
@Test
public void postmessageTest(){
String message = "Hi, this is test message";
HttpResponse response = postmessage(message, cookie);
messageId = response.getCookie();
assertEquals(response.getResponse(), "success");
}
@Test
public void markasreadTest(){
HttpResponse response = postmessage(messageId, cookie);
assertEquals(response.getResponse(), "success");
}
}

No comments:

Post a Comment