forked from powermock/powermock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sebastian Saip
authored and
Sebastian Saip
committed
Jun 30, 2016
1 parent
97f2aa6
commit 9939fac
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...ito/junit4/src/test/java/samples/powermockito/junit4/getannotation/GetAnnotationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2010 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 samples.powermockito.junit4.getannotation; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.core.MockGateway; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import samples.annotationbased.AnnotatedClassDemo; | ||
import samples.annotationbased.testannotations.RuntimeAnnotation; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.powermock.api.mockito.PowerMockito.*; | ||
|
||
/** | ||
* Assert that "isAnnotationPresent" and "getAnnotation" works correctly when mockStatic is used | ||
* @see <a href="https://github.com/jayway/powermock/issues/676">Issue 676</a> | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(AnnotatedClassDemo.class) | ||
public class GetAnnotationTest { | ||
|
||
@Before | ||
public void setUp() { | ||
mockStatic(AnnotatedClassDemo.class); | ||
when(AnnotatedClassDemo.staticMethod()).thenReturn(true); | ||
} | ||
|
||
@Test | ||
public void getClassAnnotationsReturnActualAnnotationsByDefault() throws Exception { | ||
assertTrue(AnnotatedClassDemo.class.isAnnotationPresent(RuntimeAnnotation.class)); | ||
assertNotNull(AnnotatedClassDemo.class.getAnnotation(RuntimeAnnotation.class)); | ||
assertTrue(AnnotatedClassDemo.staticMethod()); | ||
} | ||
|
||
@Test | ||
public void nonExistingAnnotationsAreNotReturnedByDefault() throws Exception { | ||
assertFalse(AnnotatedClassDemo.class.isAnnotationPresent(Deprecated.class)); | ||
assertNull(AnnotatedClassDemo.class.getAnnotation(Deprecated.class)); | ||
assertTrue(AnnotatedClassDemo.staticMethod()); | ||
} | ||
|
||
// behavior before the fix: | ||
|
||
@Test | ||
public void isAnnotationPresentReturnsFalseWhenMethodsAreMocked() throws Exception { | ||
MockGateway.MOCK_ANNOTATION_METHODS = true; | ||
try { | ||
assertFalse(AnnotatedClassDemo.class.isAnnotationPresent(RuntimeAnnotation.class)); | ||
assertTrue(AnnotatedClassDemo.staticMethod()); | ||
} finally { | ||
MockGateway.MOCK_ANNOTATION_METHODS = false; | ||
} | ||
} | ||
|
||
@Test | ||
public void getAnnotationReturnsNullWhenMethodsAreMocked() throws Exception { | ||
MockGateway.MOCK_ANNOTATION_METHODS = true; | ||
try { | ||
assertNull(AnnotatedClassDemo.class.getAnnotation(RuntimeAnnotation.class)); | ||
assertTrue(AnnotatedClassDemo.staticMethod()); | ||
} finally { | ||
MockGateway.MOCK_ANNOTATION_METHODS = false; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/utils/src/main/java/samples/annotationbased/AnnotatedClassDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package samples.annotationbased; | ||
|
||
import samples.annotationbased.testannotations.RuntimeAnnotation; | ||
|
||
@RuntimeAnnotation | ||
public class AnnotatedClassDemo { | ||
public static boolean staticMethod() { | ||
return false; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/utils/src/main/java/samples/annotationbased/testannotations/RuntimeAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package samples.annotationbased.testannotations; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(value = {TYPE}) | ||
public @interface RuntimeAnnotation { | ||
} |