Remove Powermock
Powermock does not support JUnit5 yet, so we need to remove it to support JUnit 5. Additionally, maintaining additional libraries adds extra work for the team. Mockito now supports final classes and static method mocking. This commit replaces Powermock with mockito-inline. Closes gh-6025
This commit is contained in:
@@ -19,13 +19,12 @@ package org.springframework.security.config;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.security.config.util.InMemoryXmlApplicationContext;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -41,9 +40,7 @@ import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
* @author Rob Winch
|
||||
* @since 3.0
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ ClassUtils.class })
|
||||
@PowerMockIgnore({ "org.w3c.dom.*", "org.xml.sax.*", "org.apache.xerces.*", "javax.xml.parsers.*" })
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SecurityNamespaceHandlerTests {
|
||||
|
||||
// @formatter:off
|
||||
@@ -60,6 +57,9 @@ public class SecurityNamespaceHandlerTests {
|
||||
|
||||
private static final String FILTER_CHAIN_PROXY_CLASSNAME = "org.springframework.security.web.FilterChainProxy";
|
||||
|
||||
@Mock(answer = Answers.CALLS_REAL_METHODS)
|
||||
private MockedStatic<ClassUtils> classUtils;
|
||||
|
||||
@Test
|
||||
public void constructionSucceeds() {
|
||||
new SecurityNamespaceHandler();
|
||||
@@ -83,24 +83,18 @@ public class SecurityNamespaceHandlerTests {
|
||||
@Test
|
||||
public void initDoesNotLogErrorWhenFilterChainProxyFailsToLoad() throws Exception {
|
||||
String className = "javax.servlet.Filter";
|
||||
PowerMockito.spy(ClassUtils.class);
|
||||
PowerMockito.doThrow(new NoClassDefFoundError(className)).when(ClassUtils.class, "forName",
|
||||
eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
||||
Log logger = mock(Log.class);
|
||||
SecurityNamespaceHandler handler = new SecurityNamespaceHandler();
|
||||
ReflectionTestUtils.setField(handler, "logger", logger);
|
||||
expectClassUtilsForNameThrowsNoClassDefFoundError(className);
|
||||
handler.init();
|
||||
PowerMockito.verifyStatic(ClassUtils.class);
|
||||
ClassUtils.forName(eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
||||
verifyZeroInteractions(logger);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterNoClassDefFoundError() throws Exception {
|
||||
String className = "javax.servlet.Filter";
|
||||
PowerMockito.spy(ClassUtils.class);
|
||||
PowerMockito.doThrow(new NoClassDefFoundError(className)).when(ClassUtils.class, "forName",
|
||||
eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
||||
expectClassUtilsForNameThrowsNoClassDefFoundError(className);
|
||||
assertThatExceptionOfType(BeanDefinitionParsingException.class)
|
||||
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
|
||||
.withMessageContaining("NoClassDefFoundError: " + className);
|
||||
@@ -109,9 +103,7 @@ public class SecurityNamespaceHandlerTests {
|
||||
@Test
|
||||
public void filterNoClassDefFoundErrorNoHttpBlock() throws Exception {
|
||||
String className = "javax.servlet.Filter";
|
||||
PowerMockito.spy(ClassUtils.class);
|
||||
PowerMockito.doThrow(new NoClassDefFoundError(className)).when(ClassUtils.class, "forName",
|
||||
eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
||||
expectClassUtilsForNameThrowsNoClassDefFoundError(className);
|
||||
new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
|
||||
// should load just fine since no http block
|
||||
}
|
||||
@@ -119,9 +111,7 @@ public class SecurityNamespaceHandlerTests {
|
||||
@Test
|
||||
public void filterChainProxyClassNotFoundException() throws Exception {
|
||||
String className = FILTER_CHAIN_PROXY_CLASSNAME;
|
||||
PowerMockito.spy(ClassUtils.class);
|
||||
PowerMockito.doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName",
|
||||
eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
||||
expectClassUtilsForNameThrowsClassNotFoundException(className);
|
||||
assertThatExceptionOfType(BeanDefinitionParsingException.class)
|
||||
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
|
||||
.withMessageContaining("ClassNotFoundException: " + className);
|
||||
@@ -130,9 +120,7 @@ public class SecurityNamespaceHandlerTests {
|
||||
@Test
|
||||
public void filterChainProxyClassNotFoundExceptionNoHttpBlock() throws Exception {
|
||||
String className = FILTER_CHAIN_PROXY_CLASSNAME;
|
||||
PowerMockito.spy(ClassUtils.class);
|
||||
PowerMockito.doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName",
|
||||
eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
||||
expectClassUtilsForNameThrowsClassNotFoundException(className);
|
||||
new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
|
||||
// should load just fine since no http block
|
||||
}
|
||||
@@ -140,11 +128,19 @@ public class SecurityNamespaceHandlerTests {
|
||||
@Test
|
||||
public void websocketNotFoundExceptionNoMessageBlock() throws Exception {
|
||||
String className = FILTER_CHAIN_PROXY_CLASSNAME;
|
||||
PowerMockito.spy(ClassUtils.class);
|
||||
PowerMockito.doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName",
|
||||
eq(Message.class.getName()), any(ClassLoader.class));
|
||||
expectClassUtilsForNameThrowsClassNotFoundException(className);
|
||||
new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
|
||||
// should load just fine since no websocket block
|
||||
}
|
||||
|
||||
private void expectClassUtilsForNameThrowsNoClassDefFoundError(String className) {
|
||||
this.classUtils.when(() -> ClassUtils.forName(eq(FILTER_CHAIN_PROXY_CLASSNAME), any()))
|
||||
.thenThrow(new NoClassDefFoundError(className));
|
||||
}
|
||||
|
||||
private void expectClassUtilsForNameThrowsClassNotFoundException(String className) {
|
||||
this.classUtils.when(() -> ClassUtils.forName(eq(FILTER_CHAIN_PROXY_CLASSNAME), any()))
|
||||
.thenThrow(new ClassNotFoundException(className));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,10 +23,9 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
@@ -56,11 +55,8 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
||||
* @author Rob Winch
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ SpringFactoriesLoader.class, WebAsyncManager.class })
|
||||
@PowerMockIgnore({ "org.w3c.dom.*", "org.xml.sax.*", "org.apache.xerces.*", "javax.xml.parsers.*",
|
||||
"javax.xml.transform.*" })
|
||||
public class WebSecurityConfigurerAdapterPowermockTests {
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class WebSecurityConfigurerAdapterMockitoTests {
|
||||
|
||||
ConfigurableWebApplicationContext context;
|
||||
|
||||
@@ -70,6 +66,9 @@ public class WebSecurityConfigurerAdapterPowermockTests {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Mock
|
||||
private MockedStatic<SpringFactoriesLoader> springFactoriesLoader;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
@@ -79,11 +78,10 @@ public class WebSecurityConfigurerAdapterPowermockTests {
|
||||
|
||||
@Test
|
||||
public void loadConfigWhenDefaultConfigurerAsSpringFactoryhenDefaultConfigurerApplied() {
|
||||
PowerMockito.spy(SpringFactoriesLoader.class);
|
||||
DefaultConfigurer configurer = new DefaultConfigurer();
|
||||
PowerMockito
|
||||
.when(SpringFactoriesLoader.loadFactories(AbstractHttpConfigurer.class, getClass().getClassLoader()))
|
||||
.thenReturn(Arrays.<AbstractHttpConfigurer>asList(configurer));
|
||||
this.springFactoriesLoader.when(
|
||||
() -> SpringFactoriesLoader.loadFactories(AbstractHttpConfigurer.class, getClass().getClassLoader()))
|
||||
.thenReturn(Arrays.asList(configurer));
|
||||
loadConfig(Config.class);
|
||||
assertThat(configurer.init).isTrue();
|
||||
assertThat(configurer.configure).isTrue();
|
||||
@@ -16,17 +16,11 @@
|
||||
|
||||
package org.springframework.security.config.annotation.web.configurers;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@@ -50,13 +44,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
/**
|
||||
* @author Rob Winch
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore({ "org.w3c.dom.*", "org.xml.sax.*", "org.apache.xerces.*", "javax.xml.parsers.*" })
|
||||
public class SessionManagementConfigurerServlet31Tests {
|
||||
|
||||
@Mock
|
||||
Method method;
|
||||
|
||||
MockHttpServletRequest request;
|
||||
|
||||
MockHttpServletResponse response;
|
||||
|
||||
@@ -16,18 +16,11 @@
|
||||
|
||||
package org.springframework.security.config.http;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.mock.web.MockFilterChain;
|
||||
@@ -38,7 +31,6 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextImpl;
|
||||
import org.springframework.security.web.context.HttpRequestResponseHolder;
|
||||
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -46,9 +38,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Rob Winch
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ ReflectionUtils.class, Method.class })
|
||||
@PowerMockIgnore({ "org.w3c.dom.*", "org.xml.sax.*", "org.apache.xerces.*", "javax.xml.parsers.*" })
|
||||
public class SessionManagementConfigServlet31Tests {
|
||||
|
||||
// @formatter:off
|
||||
@@ -61,9 +50,6 @@ public class SessionManagementConfigServlet31Tests {
|
||||
+ "</authentication-manager>";
|
||||
// @formatter:on
|
||||
|
||||
@Mock
|
||||
Method method;
|
||||
|
||||
MockHttpServletRequest request;
|
||||
|
||||
MockHttpServletResponse response;
|
||||
|
||||
@@ -19,15 +19,13 @@ package org.springframework.security.config.http;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareOnlyThisForTest(ParserContext.class)
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class WebConfigUtilsTests {
|
||||
|
||||
public static final String URL = "/url";
|
||||
|
||||
Reference in New Issue
Block a user