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


i designing test cases in junit. question how save object declared class variable , initialized inside test. in such cases getting java.lang.nullpointerexception.

below brief description- have 3 services listed below needs tested-

  1. service 1(login): accept username password pair , returns cookies in response.
  2. service 2(postmessage): accept message, must supplied cookies in request , returns unique id of posted message
  3. service 3(markasread): accept message id, must supplied cookies. message id, should used in service 3 returned service 2.

this expecting 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, test message";         httpresponse response = postmessage(message, cookie);         messageid = response.getcookie();          assertequals(response.getresponse(), "success");     }      @test     public void markasreadtest(){         httpresponse response = markasread(messageid, cookie);          assertequals(response.getresponse(), "success");     } } 

the answer cannot , should not it. each run of test cases have own copy of class variables. idea behind each test case should run independently , should not depend on each other.

it may possible through junk code don't have such recommendation you.

if possible try club tescases , run single one.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -