21 Aralık 2011 Çarşamba

log4j NDC

Here is a link about logging best practices:  http://c2.com/cgi/wiki?LoggingBestPractices
The last rule of thumb:
If you're logging the output of a web service or something like that, make sure you log an item which is unique to the session in each log message. With log4j, you can do this using a NDC. (We didn't use the session id, thats too long, we grabbed a unique integer and put it into the session). This way you can trace whats happening to a single session very easily. You can also log the request parameters as well if you're concerned about that sort of thing

for code sample:

public ReturnType method( InputType input) throws ExceptionType{

      try {
         // do something
      } catch(ExceptionType e) {
         throw e;
      } finally {

14 Aralık 2011 Çarşamba

delete log files automatically with log4j

With RollingFileAppender, there is a maxBackupIndex property which specifies the number of log files that will be kept.. Unfortunately, DailyRollingFileAppender does not support such property. Therefore, to use maxBackupIndex with DailyRollingFileAppenderhttp://www.codeproject.com/KB/java/CustomDailyRollingFileApp.aspx (and http://wiki.apache.org/logging-log4j/DailyRollingFileAppender) provides a custom DailyRollingFileAppender.

I included the file http://wiki.apache.org/logging-log4j/DailyRollingFileAppender in my project.. By the way, since some classes used (such as org.apache.log4j.RollingCalendar) is not visible outside the package org.apache.log4j, do not try to put the file in another package..

12 Aralık 2011 Pazartesi

mockito code samples

In a previous post, i promised to publish my own mockito code samples.Now, i have time to do that:

package mypackage;

import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyList;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

// some more imports..

public class MyMockitoTest {
   private MockedClass mocked;
   private TestedClass tested;


public void setup() {

   mocked= mock(MockedClass .class);
   tested = new TestedClass (mocked);

public void test1() {
     // mocked method returns a new MockedReturnClass instance
     when(mocked.test1CallsThis(anyString(), anyLong(), anyInt(), anyList(), any(MyClass.class)).thenReturn(new MockedReturnClass() ));
     TestedReturnClass ret = tested.test1(//...test1 parameters);
     // required assertions
     // verify what parameters test1CallsThis actually called with      
     verify(mocked).test1CallsThis("anyString", 2L, 1, ....);      

public void test2() {
     // mocked method returns a new, prepared MockedReturnClass instance, when called with THIS_PARAM
     when(mocked.test2CallsThis(eq(EnumType.THIS_PARAM) )).thenAnswer(new Answer() {
      public MockedReturnClass answer(InvocationOnMock invocation) throws Throwable {
        MockedReturnClass retClass = new MockedReturnClass();
        // prepare return data
        return retClass ;
     // mocked method returns a new MockedReturnClass instance, when called with THAT_PARAM
     when(mocked.test2CallsThis(eq(EnumType.THAT_PARAM) )).thenReturn(new MockedReturnClass() ));
     TestedReturnClass ret = tested.test2(//...test2 parameters);
     // required assertions
     // verify both calls  

public void test3() {
     when(mocked.test3CallsThis(any(CapturedClass.class)).thenReturn(new MockedReturnClass() ));
     TestedReturnClass ret = tested.test3(//...test3 parameters);
     ArgumentCaptor captor = ArgumentCaptor.forClass(CapturedClass.class);

     // required assertions
     //capture CapturedClass here
     // and do required assertions
     // ... more assertions


public void test4() {
     try {
       // mocked method throws MockedException (return type of test4CallsThis is non-void )
       when(mocked.test4CallsThis(anyString()).thenThrow(new MockedException() ); 
       tested.test4(//...test4 parameters);
     catch(MockedException ex) {
     // ... do required assertions   


public void test5() {
     try {
       // mocked method throws MockedException (return type of test5CallsThisTwiceis void)
       doThrow(new MockedException()).when(mocked.test5CallsThisTwice(anyString()); 
       tested.test5(//...test5 parameters);
       verify(mocked, times(2)).test5CallsThisTwice("anyString");
     catch(MockedException ex) {
     // ... do required assertions