All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.aj Maven / Gradle / Ivy

There is a newer version: 6.2.1
Show newest version
/*
 * Copyright 2002-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.mock.staticmock;

/**
 * Annotation-based aspect to use in test builds to enable mocking of static methods
 * on JPA-annotated {@code @Entity} classes, as used by Spring Roo for so-called
 * finder methods.
 *
 * 

Mocking will occur within the call stack of any method in a class (typically a * test class) that is annotated with {@code @MockStaticEntityMethods}. * *

This aspect also provides static methods to simplify the programming model for * setting expectations and entering playback mode. * *

Usage: *

    *
  1. Annotate a test class with {@code @MockStaticEntityMethods}. *
  2. In each test method, {@code AnnotationDrivenStaticEntityMockingControl} * will begin in recording mode. *
  3. Invoke static methods on JPA-annotated {@code @Entity} classes, with each * recording-mode invocation being followed by an invocation of either the static * {@link #expectReturn(Object)} method or the static {@link #expectThrow(Throwable)} * method on {@code AnnotationDrivenStaticEntityMockingControl}. *
  4. Invoke the static {@link #playback()} method. *
  5. Call the code you wish to test that uses the static methods. *
  6. Verification will occur automatically. *
* * @author Rod Johnson * @author Ramnivas Laddad * @author Sam Brannen * @see MockStaticEntityMethods */ public aspect AnnotationDrivenStaticEntityMockingControl extends AbstractMethodMockingControl { /** * Expect the supplied {@link Object} to be returned by the previous static * method invocation. * @see #playback() */ public static void expectReturn(Object retVal) { AnnotationDrivenStaticEntityMockingControl.aspectOf().expectReturnInternal(retVal); } /** * Expect the supplied {@link Throwable} to be thrown by the previous static * method invocation. * @see #playback() */ public static void expectThrow(Throwable throwable) { AnnotationDrivenStaticEntityMockingControl.aspectOf().expectThrowInternal(throwable); } /** * Stop recording mock expectations and enter playback mode. * @see #expectReturn(Object) * @see #expectThrow(Throwable) */ public static void playback() { AnnotationDrivenStaticEntityMockingControl.aspectOf().playbackInternal(); } // Apparently, the following pointcut was originally defined to only match // methods directly annotated with @Test (in order to allow methods in // @MockStaticEntityMethods classes to invoke each other without resetting // the mocking environment); however, this is no longer the case. The current // pointcut applies to all public methods in @MockStaticEntityMethods classes. protected pointcut mockStaticsTestMethod() : execution(public * (@MockStaticEntityMethods *).*(..)); protected pointcut methodToMock() : execution(public static * (@javax.persistence.Entity *).*(..)); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy