尝试在Junit测试中模拟服务的属性时遇到问题:
@ContextConfiguration("classpath:application-config.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class FooServiceTests {
@Autowired
private FooServiceImpl fooService;
@Test
public void testFoo() {
String str = fooService.foo();
assertEquals("Var", str);
}
@Before
public void mockFooDao() throws Exception {
FooDao mockFooDao = Mockito.mock(FooDao.class);
Mockito.when(mockFooDao.foo()).thenReturn("Var");
ReflectionTestUtils.setField(fooService, "fooDao", mockFooDao);
}
}
Run Code Online (Sandbox Code Playgroud)
嘲弄fooDao没有效果,因为结果不是预期的.这是服务和dao的代码:
@Service("fooService")
public class FooServiceImpl implements FooService {
@Autowired
protected FooDao fooDao;
@Override
public String foo() {
return fooDao.foo();
}
}
@Repository
public class FooDaoImpl implements FooDao {
@Override
public String foo() {
return "foo";
}
}
Run Code Online (Sandbox Code Playgroud)
正如我们所看到的,实际服务意味着返回"foo",但是测试会模仿dao,因此服务返回"var".我知道这是一个与CGLIB代理相关的东西,但我无法弄清楚如何在不使用fooDao属性的setter的情况下使其工作.任何帮助,将不胜感激.
提前问候并表示感谢.
我们暂时考虑以下代码
Rx.Observable.merge(
Rx.Observable.just(1),
Rx.Observable.just(1).delay(1000)
).distinctUntilChanged()
.subscribe(x => console.log(x))
Run Code Online (Sandbox Code Playgroud)
我们希望1只记录一次.但是,如果我们想要允许重复一个值,如果它的最后一次发射是可配置的时间量呢?我的意思是记录两个事件.
例如,拥有如下内容会很酷
Rx.Observable.merge(
Rx.Observable.just(1),
Rx.Observable.just(1).delay(1000)
).distinctUntilChanged(1000)
.subscribe(x => console.log(x))
Run Code Online (Sandbox Code Playgroud)
其中distinctUntilChanged()接受某种超时以允许重复下一个元素.然而,这样的事情不存在,我想知道是否有人知道通过使用高级操作员实现这一点的优雅方式,而不会弄乱需要处理状态的过滤器
我想这个下实现与Spring Security的定制无国籍身份验证的文章
我面临的问题是框架没有调用我的自定义过滤器,即使我的SecurityConfig看起来与上一个链接看起来几乎相同(稍微简单一点):
@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("appAuthenticationProvider")
private AuthenticationProvider authenticationProvider;
@Autowired
@Qualifier("appAuthenticationFilter")
private AppAuthenticationFilter appAuthenticationFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable().
sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests().anyRequest().authenticated()
.and()
.anonymous().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedEntryPoint());
http.addFilterBefore(appAuthenticationFilter, BasicAuthenticationFilter.class);
}
@Bean
public AuthenticationEntryPoint unauthorizedEntryPoint() {
return (request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
}
Run Code Online (Sandbox Code Playgroud)
我没有发布authenticationProvider和appAuthenticationFilter的代码,因为前者工作正常(我可以使用/ login端口登录)而后者只是实现了GenericFilterBean而且甚至没有被调用.
任何帮助将非常感激!