1

I am testing an algorithm for StringSearch and the algorithm has many IllegalAccessExceptions in it.

I wanted to test that the code is working fine with the exceptions...and it is important also to test them as they call some functions when the exception occurs.

I just wanted to know how can i implicitly generate the exception.

I am trying to use the Java Security Manager but don't know how to use it to revoke the program's access to String class.

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
Nirav
  • 49
  • 1
  • 14
  • 1
    I don't understand. What has a StringSearch algorithm to do with IllegalAccessExceptions? – mhaller Dec 09 '12 at 01:49
  • Please write the code where you throws that exception – Bhavik Ambani Dec 09 '12 at 01:55
  • 1
    Why not just `throw new WhateverException()` in places that you like to test? http://stackoverflow.com/questions/5184284/illegalaccessexception-on-using-reflection problem might be your solution – zapl Dec 09 '12 at 01:56

1 Answers1

1

try using Mockito to stub the method throws exception

 doThrow(new RuntimeException()).when(mockedList).clear();

 //following throws RuntimeException:
 mockedList.clear();

http://gojko.net/2009/10/23/mockito-in-six-easy-examples/

http://mockito.googlecode.com/svn/tags/1.7/javadoc/org/mockito/internal/stubbing/Stubber.html

whunmr
  • 2,435
  • 2
  • 22
  • 35
  • Here is the code that throws the exception try { int o = offset.getInt(text); char[] t = (char[]) value.get(text); return instance.searchChars(t, textStart + o, textEnd + o, getChars(pattern), processed) - o; } catch (IllegalAccessException ex) { synchronized (activeStringAccess) { activeStringAccess = new StringAccess(); } – Nirav Dec 09 '12 at 02:13
  • `private INSTANCE_CLASS instance; doThrow(new IllegalAccessException()).when(instance).searchChars(any(), any(), any(), any(), any());` – whunmr Dec 09 '12 at 02:23
  • Thanks for reply but The program is saying that it is not able to resolve doThrow – Nirav Dec 09 '12 at 02:35
  • You need add mockito lib to your classpath. – whunmr Dec 09 '12 at 02:38
  • I did this import org.mockito.*; Stringsearchss4 = new BNDMWildcardsCI(); Mockito.doThrow(new IllegalAccessException()).when(ss4).searchChars(cc,pattern3); it is saying argument to when is not a mock – Nirav Dec 09 '12 at 03:20
  • Stringsearch ss4 = mock(BNDMWildcardsCI.class); – whunmr Dec 09 '12 at 03:35
  • I am getting following exception Exception in thread "main" org.mockito.exceptions.base.MockitoException: Checked exception is invalid for this method! Invalid: java.lang.IllegalAccessException – Nirav Dec 09 '12 at 07:10
  • The issue that I have identified is SearcChars does not throw that exception it is the offset.getInt(text) that throws the IllegalAccessException but I can't mock the method as the class for offset is final – Nirav Dec 09 '12 at 18:53
  • I have tried to use SecurityManager like below SecurityManager SecMgr = new SecurityManager(); SecMgr.checkPackageAccess("java.lang.reflect.Field"); but dont know how to revoke permission to a method in the package or revoke access to package itself – Nirav Dec 11 '12 at 00:21