Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use powermock/mockito technology as alternative to Aspectj #16

Open
mkotsbak opened this issue Jun 14, 2010 · 3 comments
Open

Use powermock/mockito technology as alternative to Aspectj #16

mkotsbak opened this issue Jun 14, 2010 · 3 comments

Comments

@mkotsbak
Copy link
Contributor

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
Copy link
Contributor

jhannes commented Jun 14, 2010

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
Copy link
Contributor Author

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
Copy link
Contributor

jhannes commented Jun 14, 2010

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants