我的项目中有一些静态的util方法,其中一些只是传递或抛出异常.关于如何模拟具有除void之外的返回类型的静态方法,有很多例子.但是我如何模拟一个将void返回到" doNothing()" 的静态方法?
非void版本使用以下代码行:
@PrepareForTest(StaticResource.class)
Run Code Online (Sandbox Code Playgroud)
...
PowerMockito.mockStatic(StaticResource.class);
Run Code Online (Sandbox Code Playgroud)
...
Mockito.when(StaticResource.getResource("string")).thenReturn("string");
Run Code Online (Sandbox Code Playgroud)
但是,如果应用于StaticResources返回void,编译将抱怨when(T)不适用于void ...
有任何想法吗?
一个解决方法可能是让所有静态方法返回一些Boolean成功,但我不喜欢变通方法.
在集成测试spring mvc web应用程序时,有没有办法在模拟请求上传递整个表单对象?我能找到的就是将每个字段分别作为这样的参数传递:
mockMvc.perform(post("/somehwere/new").param("items[0].value","value"));
Run Code Online (Sandbox Code Playgroud)
这对于小型表格来说很好.但是,如果我发布的对象变大了怎么办?如果我可以发布整个对象,它也会使测试代码看起来更好.
具体来说,我想通过复选框测试多个项目的选择,然后发布它们.当然我可以测试发布一个项目,但我想知道..
我们使用spring 3.2.2和spring-test-mvc.
我的表单模型看起来像这样:
NewObject {
List<Item> selection;
}
Run Code Online (Sandbox Code Playgroud)
我试过这样的电话:
mockMvc.perform(post("/somehwere/new").requestAttr("newObject", newObject)
Run Code Online (Sandbox Code Playgroud)
到这样的控制器:
@Controller
@RequestMapping(value = "/somewhere/new")
public class SomewhereController {
@RequestMapping(method = RequestMethod.POST)
public String post(
@ModelAttribute("newObject") NewObject newObject) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是对象将是空的(是的,我在测试之前填充了它)
我发现唯一可行的解决方案就是使用@SessionAttribute: Spring MVC应用程序的集成测试:表单
但我不喜欢在我需要的每个控制器的末尾都要记得调用完成的想法.在所有表单数据不必在会话中之后,我只需要它来处理一个请求.
所以我现在唯一能想到的就是编写一些Util类,它使用MockHttpServletRequestBuilder将所有对象字段附加为.param,使用反射或单独为每个测试用例添加..
我不知道,觉得不直观..
关于如何让我变得更轻松的任何想法/想法?(除了直接调用控制器)
谢谢!
是否有一种内置的方法只能在使用Jackson(rapidxml.jackson 2.1.1)时序列化孩子的id?我们想发送一个Order有Person参考的REST .然而,person对象非常复杂,我们可以在服务器端刷新它,所以我们只需要主键.
或者我需要一个自定义序列化器吗?或者我需要@JsonIgnore所有其他属性吗?这会阻止Person数据在请求Order对象时被发回吗?我不确定我是否需要它,但如果可能的话我想控制它...
是否有一个教程或有没有人指出如何使用Spring-Security执行以下操作?
任务:
我需要从我的数据库中获取用于验证用户名的salt并使用它来加密提供的密码(从登录页面)以将其与存储的加密密码(也称为用户验证)进行比较.
附加信息:
我使用自定义数据库结构.一个UserDetails对象是通过自定义创建UserDetailsService这反过来使用自定义DAOProvider来从数据库中获取信息.
我的security.xml文件到目前为止:
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)
现在我想我需要
<password-encoder hash="sha" />
Run Code Online (Sandbox Code Playgroud)
还有什么?如何告诉spring security使用databaseprovided salt来编码密码?
编辑:
我发现这个SO帖子是信息性的,但还不够:如果我在我的xml中定义一个盐源来供密码编码器使用,如下所示:
<password-encoder ref="passwordEncoder">
<salt-source ref="saltSource"/>
</password-encoder>
Run Code Online (Sandbox Code Playgroud)
我将不得不编写一个自定义SaltSource来使用我的自定义盐.但是在UserDetails对象内部找不到.所以...
备选方案1:
我可以使用可能具有salt属性的UserDetails的自定义实现吗?
<beans:bean id="saltSource" class="path.to.MySaltSource"
p:userPropertyToUse="salt"/>
Run Code Online (Sandbox Code Playgroud)
和
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
// ...
return buildUserFromAccount(account);
}
@Transactional(readOnly = true)
UserDetailsImpl buildUserFromAccount(Account account){
// ... build User object that contains salt property
} …Run Code Online (Sandbox Code Playgroud) 我们有一个页面打开一个模式对话框,其形式如下所示.然而,当我们点击应该处理表单操作的控制器时,表单对象是未定义的,我太过一个Angular新手来理解为什么......
这是父页面控制器保存打开模态对话框的功能:
app.controller('organisationStructureController', ['$scope', ..., '$modal', function ($scope, ..., $modal) {
$scope.openInvitationDialog = function (targetOrganisationId) {
$modal.open({
templateUrl: 'send-invitation.html',
controller: 'sendInvitationController',
resolve: {$targetOrganisationId: function () {
return targetOrganisationId;
}
}
}
);
};
Run Code Online (Sandbox Code Playgroud)
在这样的页面上:
// inside a loop over organisations
<a ng-click="openInvitationDialog({{organisation.id}})">Invite new member</a>
Run Code Online (Sandbox Code Playgroud)
邀请对话框html看起来像这样:
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<!-- ... -->
</div>
<div class="modal-body">
<form name="invitationForm">
<div class="form-group">
<label for="email" style="color:white;">Email</label>
<input type="email" class="form-control" autocomplete="off" placeholder="New member email" id="email" name="email" ng-model="invitation.email" required="true"/>
<span class="error animated fadeIn" ng-show="invitationForm.email.$dirty …Run Code Online (Sandbox Code Playgroud) 我已经建立了一个简单的多对多关系帐户:Hibernate的角色,但是当我尝试在单元测试中保存帐户后添加了其角色,我得到一个UnsupportedOperationException:
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:144)
at java.util.AbstractList$Itr.remove(AbstractList.java:360)
at java.util.AbstractList.removeRange(AbstractList.java:559)
at java.util.AbstractList.clear(AbstractList.java:217)
at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:502)
at org.hibernate.type.CollectionType.replace(CollectionType.java:582)
at org.hibernate.type.TypeHelper.replace(TypeHelper.java:178)
at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:563)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:288)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:261)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:84)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:867)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:851)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:855)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:686)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy33.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:360)
at ....JpaProvider.save(JpaProvider.java:161)
at ....DataModelTest.testAccountRole(DataModelTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at …Run Code Online (Sandbox Code Playgroud) 如果我想为一个客户端保护REST服务器,那么spring-security OAuth2的最小设置是什么?我不想使用任何不必要的设置或实现任何不必要的bean.对于spring-security + OAuth2,可能已经有一个"简单"的教程/示例了吗?(虽然我试图避免过于充满希望)
我当前的工作设置(使用来自sparklr上下文的副本+过去+ wtf)感觉太多了:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices">
<oauth:client-credentials />
</oauth:authorization-server>
<sec:authentication-manager alias="clientAuthenticationManager">
<sec:authentication-provider user-service-ref="clientDetailsUserService" />
</sec:authentication-manager>
<http pattern="/oauth/token" create-session="stateless"
authentication-manager-ref="clientAuthenticationManager"
xmlns="http://www.springframework.org/schema/security">
<intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
<anonymous enabled="false" />
<http-basic entry-point-ref="clientAuthenticationEntryPoint" />
<!-- include this only if you need to authenticate clients via request parameters -->
<custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" />
<access-denied-handler ref="oauthAccessDeniedHandler" />
</http>
<oauth:resource-server id="resourceServerFilter"
resource-id="rest_server" token-services-ref="tokenServices" />
<oauth:client-details-service id="clientDetails">
<oauth:client client-id="the_client" authorized-grant-types="client_credentials"
authorities="ROLE_RESTREAD" secret="1234567890" />
</oauth:client-details-service> …Run Code Online (Sandbox Code Playgroud) 我们正在以模块化的方式开发Spring,Hibernate和Maven的Web应用程序.有定义数据访问和查看特定内容的核心项目,然后有定义逻辑和实体的模块(@Entity)然后是定义控制器和视图的Web应用程序.
现在我们有一个安全模块定义了帐户和角色等安全实体,我们有一个原型模块定义了一些示例实体,如客户和订单.两者都有一个PersistenceUnit定义的内部a persistence.xml,除了PersistenceUnit名称之外几乎是空的,因为所有数据库配置都是在带有datasource.xml的web应用程序中完成的.Web应用程序应该将两个jar加载为maven依赖项.
这两个项目都可以构建好的,自动扫描所有实体并为各自的单元测试创建它们.如果单独添加,它们也将成功加载到Web应用程序中.
但是,只要两者同时加载,第二个将覆盖PersistenceUnit第一个,从而IllegalArgumentException : Not an entity为第一个实体创建一个.如果两个项目都有不同的持久性单元,加载Web应用程序将抛出另一个异常,说明已no single default persistence unit定义.
那么..我怎样才能@Entity在我的网络应用程序中加载所有带注释的类而不必在里面定义它们persistence.xml(比如这里),而是通过组件扫描?
这似乎是一个想法,虽然我不知道如何使用和测试它...
我想我们要么必须在Web应用程序中合并所有PersistenceUnits,要么以编程方式加载所有实体.在persistence.xml中定义硬编码不是我们的选择.
我们使用username-password grant从我们的auth服务器获取访问令牌.我们希望使用提供的刷新令牌在访问令牌到期之前刷新访问令牌,直到用户注销或关闭客户端应用程序.
但是我找不到任何关于如何发出此刷新令牌请求的示例.
要获得令牌,我们称之为:
curl -v --data "grant_type=password&username=user&password=pass&client_id=my_client" http://localhost:8080/oauth/token
Run Code Online (Sandbox Code Playgroud)
所以要刷新我希望调用看起来像这样:
curl -v --data "grant_type=refresh_token&access_token=THE_ACCESS_TOKEN&refresh_token=THE_REFRESH_TOKEN" http://localhost:8080/oauth/token
Run Code Online (Sandbox Code Playgroud)
或者可能
curl -v -H "Authorization: Bearer THE_ACCESS_TOKEN" --data "grant_type=refresh_token&refresh_token=THE_REFRESH_TOKEN" http://localhost:8080/oauth/token
Run Code Online (Sandbox Code Playgroud)
但它只会给我一个401 ..
哦是的,也许我需要添加clientId?我不能使用客户端密钥,因为没有(请参阅上面获取令牌的请求).毕竟使用用户名和密码进行身份验证.
我认为我们的服务器配置正确,所以我不会在这里发布.如果我的一个示例请求应该工作,并且您需要查看重要的配置部分,我将添加它们.
谢谢!
spring spring-security access-token oauth-2.0 spring-security-oauth2
我们的Web应用程序使用SystemPropertyPlaceholder根据系统属性的值加载属性文件(参见下文)
在本地运行它的默认设置存储在application.properties.在生产服务器上,我们目前只是在部署应用程序之前将"env"设置为"production",它将加载production.properties.
现在,为了测试应用程序,test.properties应该使用一个文件.
如果我在我们的jenkins构建中运行所有测试,则添加-Denv=test将按预期工作.但是,如果我只想在Eclipse中使用集成的JUnit运行器运行单个测试呢?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = WebContextLoader.class, locations = {"classpath:application-context.xml" })
public class SomeTest {
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉我的测试它应该在Spring加载之前将系统属性"env"设置为"test"?因为using MethodInvokingFactoryBean只会出于某种原因设置它,即使我在加载属性文件之前设置它:
<bean id="systemPrereqs"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" value="#{@systemProperties}" />
<property name="targetMethod" value="putAll" />
<property name="arguments">
<!-- The new Properties -->
<util:properties>
<prop key="env">test</prop>
</util:properties>
</property>
</bean>
<bean
class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchContextAttributes" value="true" />
<property name="contextOverride" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>classpath:${env}.properties</value>
<value>${config}</value>
</list>
</property>
</bean>
<bean id="managerDataSource" …Run Code Online (Sandbox Code Playgroud) java ×4
spring ×4
hibernate ×2
jpa ×2
oauth-2.0 ×2
access-token ×1
angularjs ×1
annotations ×1
eclipse ×1
entity ×1
forms ×1
jackson ×1
javascript ×1
json ×1
junit ×1
many-to-many ×1
mocking ×1
mockito ×1
modal-dialog ×1
passwords ×1
powermock ×1
salt ×1
spring-mvc ×1
static ×1
testing ×1
void ×1