Annyce Davis

Davis Technology Consulting

  • Home
  • About Me
  • Blog
  • Courses
  • Newsletter

Updated: Mocking Hibernate Create Criteria in Grails’ Unit Test with GMock

December 6, 2010 by Annyce Davis

Using the GMock Library I have updated my unit tests to mock out the create criteria methods the following way:

void testSomeFunction() {
def results = []

def mockCriteria = mock() {
list(instanceOf(Closure)).returns(results)
}

mock(Book).static.createCriteria().returns(mockCriteria)

play {
assertEquals null, bookService.getDefaultBook(null)
}
}

You need to add the following to the top of the unit test file with the import statements:

import org.gmock.*
import static org.hamcrest.Matchers.*

@WithGMock

Also add the following to your BuildConfig.groovy:

dependencies {
test "org.gmock:gmock:0.8.0"
test "org.hamcrest:hamcrest-all:1.0"
}

I learned this from the following blog post: http://adhockery.blogspot.com/2010/01/using-gmock-to-complement-grails.html

Unit Testing a Android Activity which contains a Runnable

November 16, 2010 by Annyce Davis

In order to unit test an Android Activity to verify that the Runnable is called you need to add a call to waitForIdle to the Instrumentation, which is called when all Runnables have been executed.

Here is the activity that we are testing:

public class SplashScreenActivity extends Activity {

public final int SPLASH_DISPLAY_LENGTH = 1200;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash);

new Handler().postDelayed(new Runnable() {

public void run() {
Intent mainIntent = new Intent(SplashScreenActivity.this, NextActivity.class);
startActivity(mainIntent);
finish();
}

}, SPLASH_DISPLAY_LENGTH);
}
}

Here is the method in the Unit Test Class:

public void testSubLaunch() {
SplashScreenActivity mActivity = startActivity(new Intent(), null, null);
getInstrumentation().waitForIdle(new Runnable() {
public void run() {
assertNotNull(getStartedActivityIntent());
assertTrue(isFinishCalled());
}
});
}

Why you should upgrade to Grails 1.3.5

November 13, 2010 by Annyce Davis

I did a profile of my Grails application with JVisualVM running Grails 1.3.4 and then again running Grails 1.3.5.  What a difference!  The memory leak issue that was referred to here http://jira.codehaus.org/browse/GRAILS-6622 was fixed in the 1.3.5 release.  I believe that a similar memory leak was affecting my application as well.  Across the board, the application is faster and has a much smaller memory footprint.  I have attached a snapshot of the memory comparison for the two versions to this post.

Memory Comparison of Grails 1.3.4 and Grails 1.3.5

Adding HTTP Access Logging to Glassfish 3.0 Server

November 13, 2010 by Annyce Davis

Recently I had the need to monitor the requests that were being sent to my Glassfish server. Here is the simple configuration change that needed to be done to the domain.xml file:

<configs>
  <config name="server-config">
 <http-service enabled="true"></http-service>
  <access-log format="%client.name% %auth-user-name% %datetime% %request% %status% %response.length% %header.referer%"></access-log>
  <virtual-server id="server" log="/mnt/httpd/logs/access" listeners="http-listener-1"></virtual-server>
  <virtual-server id="__asadmin" listeners="admin-listener"></virtual-server>
 ....
 </config>
 </configs>

Sun’s Documentation: Enabling Access Logging

« Previous Page
Next Page »

Follow Me

  • Bluesky

Categories

  • Android (61)
  • Career (5)
  • Communication (4)
  • Flutter (1)
  • Git (4)
  • Gradle (4)
  • Grails (23)
  • iOS (1)
  • Java (8)
  • JavaScript (6)
  • Kotlin (17)
  • Life (5)
  • Public Speaking (26)
  • Revenue (2)
  • RxJava (1)
  • Software Development (14)
  • Twitter (3)
  • Uncategorized (11)
  • Video Course (5)

Follow Me

  • Bluesky

Copyright © 2025 · All Rights Reserved · Log in