小编sth*_*der的帖子

如何测试 Spring Boot 处理程序拦截器

我们正在尝试使用 spring boot 1.4.0 版对我们的 spring boot 应用程序中的拦截器进行集成测试,但不确定如何;这是我们的应用程序设置

@Configuration
@EnableAutoConfiguration()
@ComponentScan
public class Application extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilderconfigure(SpringApplicationBuilder application) {
  return application.sources(Application.class);
}
Run Code Online (Sandbox Code Playgroud)

然后我们通过扩展 WebMvcConfigurerAdapter 来定制 webmvc

@Configuration
public class CustomServletContext extends WebMvcConfigurerAdapter{
  @Override
  public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(testInterceptor).addPathPatterns("/testapi/**");
  }
}
Run Code Online (Sandbox Code Playgroud)

所以我们想测试拦截器,但我们不想真正启动应用程序,因为有很多依赖bean需要读取外部定义的属性文件来构造

我们尝试了以下方法

@SpringBootTest(classes = CustomServletContext.class)
@RunWith(SpringRunner.class)
public class CustomServletContextTest {

  @Autowired
  private ApplicationContext applicationContext;

  @Test
  public void interceptor_request_all() throws Exception {
    RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) applicationContext
        .getBean("requestMappingHandlerMapping");
    assertNotNull(mapping);

    MockHttpServletRequest request = new MockHttpServletRequest("GET",
        "/test");

    HandlerExecutionChain chain = …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot spring-boot-test

9
推荐指数
1
解决办法
8834
查看次数

Iterable <Key>作为返回类型,这意味着什么?

我正在阅读有关sequencesearch的实现SequentialSearch ,发现没有什么意义(或者至少在我看来如此)

    public Iterable<Key> keys()  {
    Queue<Key> queue = new Queue<Key>();
    for (Node x = first; x != null; x = x.next)
        queue.enqueue(x.key);
    return queue;
}
Run Code Online (Sandbox Code Playgroud)

方法如何返回接口?我们不应该返回一个类/原始类型吗?

java return interface

4
推荐指数
1
解决办法
677
查看次数

将空值插入另一个集合中可以吗?

很抱歉,您没有天真的问题,可以使用range函数将一个空集插入另一个集吗?还是未定义的行为?

在试运行https://ideone.com/RNGIFT似乎罚款,检查基准如果容器是空的,返回的迭代器将等于结束()。

#include <iostream>
#include <set>
using namespace std;


int main() {
    std::set<string> to_be_inserted;
    std::set<string> res;
    cout << "check everything is fine" << endl;
    res.insert(to_be_inserted.begin(), to_be_inserted.end());
    cout << "how about now ?" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
1
解决办法
93
查看次数

为什么-2 >>> 1等于Java中的2147483647

-2一个人的补充是100000 ... 01

-2两个补码是1000000 ...... 10

-2 >>> 1 
Run Code Online (Sandbox Code Playgroud)

根据>>>定义,左侧移位为0

应该是这样的 01000......1,为什么变成0111111..11什么?

java twos-complement operator-keyword ones-complement

3
推荐指数
1
解决办法
129
查看次数

java字符串操作,将多个斜杠更改为一个斜杠

愚蠢的错误+用来代替+

所有,我试图将输入路径中的所有"/ +"转换为"/"以简化unix风格的路径.

 path.replaceAll( "/+", "/"); 
 path.replaceAll( "\\/+", "/"); 
Run Code Online (Sandbox Code Playgroud)

事实证明没有做任何事情,这样做的正确方法是什么?

public class SimplifyPath {
public String simplifyPath(String path) {
    Stack<String> direc = new Stack<String> ();
    path = path.replaceAll("/?", "/");
    System.out.println("now path becomes " + path);  // here path remains "///"

    int i = 0;
    while (i < path.length() - 1) {
        int slash = path.indexOf("/", i + 1);
        if (slash == -1) break;
        String tmp = path.substring(i, slash);
        if (tmp.equals("/.")){
            continue;
        } else if (tmp.equals("/..")) {
            if (! direc.empty()){
                direc.pop(); …
Run Code Online (Sandbox Code Playgroud)

java string replaceall

1
推荐指数
1
解决办法
1225
查看次数