Question about junit testing

6 views
Skip to first unread message

Paul van Hoven

unread,
Jul 5, 2008, 5:47:35 AM7/5/08
to Google Web Toolkit
I build a very small and simple project for testing the junit testing.
Junit works fine. The simple program I build looks like this:

-----------------------------------------------
public class MainTest extends GWTTestCase implements AsyncCallback {

public String getModuleName() {
return "junitGWT.Main";
}

public void testSimple() {
assertTrue(true);
}

public void onSuccess( Object result ) {}
public void onFailure( Throwable caught ) {}

public void testTextService() {

final Main main = new Main();

Timer timer = new Timer() {
public void run() {
delayTestFinish(100);
main.getTextFromServer("null");
assertEquals( "this here should fail but it does not" ,
main.textFromServer );
delayTestFinish(100);
main.getTextFromServer("codeword");
assertEquals( "success" , main.textFromServer );
finishTest();
}
};

TextServiceAsync textService = ( TextServiceAsync )
GWT.create( TextService.class );
ServiceDefTarget target = (ServiceDefTarget) textService;
target.setServiceEntryPoint( GWT.getHostPageBaseURL() +
"TextServiceImpl");
textService.setupSessionObject( this );

timer.schedule(300);
}
-----------------------------------------------


On the serverside the servlet looks like this:
-----------------------------------------------
public class TextServiceImpl extends RemoteServiceServlet implements
TextService {

public void setupSessionObject() {
if( getThreadLocalRequest().getSession(false) != null )
getThreadLocalRequest().getSession(false).invalidate();
HttpSession session = getThreadLocalRequest().getSession();
session.setAttribute("returnValue", new String("success") );
}

public String getText( String code ) {
if( code.equals("codeword") ) {
HttpSession session = getThreadLocalRequest().getSession( false );
return (String) session.getAttribute("returnValue");
} else
return null;
}
}
-----------------------------------------------

The reason why I build such a test for this toy servlet:
1. The method of interest in the servlet needs a valid session.
2. This session has to be created before actually testing the rpc
method of interest because the method of interest shouldn't contain
unnecessary code
3. In my real project I'm storing a lot of data in a session object
that is used in my rpc methods. Hence for testing my rpc methods I
need to setup a dummy session object before executing the tests of
interest.

In the testcase the method "assertTrue(true);" yields a green bar in
the unit testing window. But also the second test yields a green bar
althought it should not. The test "assertEquals( "this here should
fail but it does not" , main.textFromServer );" should fail but it
isn't even executed.

I asking myself why the run-method in the timer is not executed?

Paul van Hoven

unread,
Jul 5, 2008, 1:31:58 PM7/5/08
to Google Web Toolkit
Okay I found out that I had to modify the test. Now it the test looks
like this:
----------------------------------------------------------
public class MainTest extends GWTTestCase implements AsyncCallback {

public String getModuleName() {
return "junitGWT.Main";
}

public void testSimple() {
assertTrue(true);
}

public void onSuccess( Object result ) {}
public void onFailure( Throwable caught ) {}

public void testTextService() {

final Main main = new Main();

TextServiceAsync textService = ( TextServiceAsync )
GWT.create( TextService.class );
ServiceDefTarget target = (ServiceDefTarget) textService;
target.setServiceEntryPoint( GWT.getHostPageBaseURL() +
"TextServiceImpl");
textService.setupSessionObject( this );

Timer timer = new Timer() {
public void run() {
main.getTextFromServer("codeword");
//here I need a delay.
assertEquals( "success" , main.textFromServer );
finishTest();
}
};

delayTestFinish(500);
timer.schedule(300);
}
}
----------------------------------------------------------

The Main class looks like this (at least the interesting part):
----------------------------------------------------------
public class Main implements EntryPoint, AsyncCallback {
...
public void getTextFromServer( String option ) {
textService.getText( option , this);
}

public void onFailure( Throwable caught ) {
label.setText(caught.getMessage());
}

public String textFromServer = null;
public void onSuccess( Object result ) {
if( result != null ) {
label.setText( (String) result );
textFromServer = (String) result;
} else {
textFromServer = null;
label.setText( "null" );
}
}
-------------------------------------------------------------

Now the run method of my Timer object in the TestCase is executed but
the test fails anyway. The problem is that I need some kind of delay
before assertEquals( "success" , main.textFromServer ); is called. I
manually added a Window.alert and waited a short time before clicking
okay. Then it worked. But in automated mode if fails.

How can this be solved?


On Jul 5, 11:47 am, Paul van Hoven <paul.van.ho...@googlemail.com>
wrote:

Paul van Hoven

unread,
Jul 5, 2008, 1:58:51 PM7/5/08
to Google Web Toolkit
Okay I modified again and added a new inner timer in the test case
---------------------------
Timer timer = new Timer() {
public void run() {
main.getTextFromServer("codeword2");
Timer innerTimer = new Timer() { public void run(){
assertEquals( "success" , main.textFromServer );
finishTest();
} };
innerTimer.schedule( 500 );
}
};

delayTestFinish(1000);
timer.schedule(1);
-----------------------------

But also this fails although the assertion is 500 milliseconds
delayed. Actually I have no idea what is going on here and why this
doesn't work.

On Jul 5, 7:31 pm, Paul van Hoven <paul.van.ho...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages