nomuda / jackbox

BlackBoxRecorder port
http://follesoe.github.com/BlackBoxRecorder/
14 stars 6 forks source link

Use powermock/mockito technology as alternative to Aspectj #16

Open mkotsbak opened 14 years ago

mkotsbak commented 14 years ago

Powermock/mockito is able to do the same as we do with Aspectj without any special command line, making the usage easier. It seems to do bytecode manipulation to do the work.

Instead of doing the same, maybe powermock + mockito can be used instead.

After somehow discovered all the annotation, generate code that calls:

For Record:

ClassToRecord classToRecord(new ClassToRecord()); ClassToRecord classUnderTestSpy = PowerMockito.spy(classToRecord);

whenNew(ClassToRecord.class).withNoArguments().thenReturn(classUnderTestSpy); Mockito.when(classUnderTest.methodToMock()).thenAnswer(recordingAnswer);

An answer class receives parameters so we can record and then call the real class:

http://mockito.googlecode.com/svn/branches/1.7/javadoc/org/mockito/stubbing/Answer.html#answer%28org.mockito.invocation.InvocationOnMock%29

Dependency:

Do the same. On playback we can let answer class return recorded values instead.

jhannes commented 14 years ago

This looks really nice, but I can't get it to work outside a unit test. Powermock requires a test class that's annotated with @RunWith(PowerMockRunner.class) and @PrepareForTest(ExampleRecordedObject.class) (in our case).

I've tried taking the PowerMockRunner apart to get it to work without being inside a test context, but AbstractTestSuiteChunkerImpl is pretty complex and hard to pull apart.

The approach looks real cool, so I hope we find a way to use it.

mkotsbak commented 14 years ago

What about this one, is it doing the prepareForTest stuff?:

http://powermock.googlecode.com/svn/docs/powermock-1.2/apidocs/org/powermock/core/classloader/MockClassLoader.html

I thought they had API to use Powermock functionality without using annotations.

jhannes commented 14 years ago

MockClassLoader is being used, but what the RunWith(PowerMockRunner) does seems to be to run the classes in a new thread with MockClassLoader as the class loader. It will take a long time to extract the parts.