小编Ole*_*iak的帖子

Spring Boot WebMvcTest 需要 EntityManager

为什么简单@WebMvcTest需要运行实体管理器?

我有以下控制器测试

@WebMvcTest(Controller.class)
class ControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private Service service;

    @SneakyThrows
    @Test
    void test1() {
        when(service.getById(anyString())).thenReturn(responseDto());

        mockMvc
            .perform(
                get("/endpoint", "id")
                    .accept(MediaType.APPLICATION_JSON_VALUE)
            )
            .andExpect(status().isOk())
            .andExpect(content().string(equalToJson(FileUtils.readSystemResource("expectedresponse.json"))));
    }

    @TestConfiguration
    @AutoConfigureDataJpa
    @ComponentScan(basePackageClasses = Controller.class,
        useDefaultFilters = false,
        includeFilters = {
            @ComponentScan.Filter(
                type = FilterType.ASSIGNABLE_TYPE,
                value = Controller.class
            )
        }
    )
    static class TestConfig {

    }

}
Run Code Online (Sandbox Code Playgroud)

该测试将运行正常,但如果我@AutoConfigureDataJpa从中删除TestConfig,则会失败

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.postProcessFields(MockitoTestExecutionListener.java:99)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.injectFields(MockitoTestExecutionListener.java:79)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:54)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) …
Run Code Online (Sandbox Code Playgroud)

java hibernate spring-mvc spring-boot junit5

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

创建jquery扩展.范围问题

我创建了一个简单的jQuery扩展(这是我的第一个).

(function($){
  var MyClass = function(opt){
     //..
  };
  //one of the methods of my extension
  $.fn.myExtension = function(opt){
    this._ext = new MyClass(opt);
    return this;
  };
  $.fn.myExtensionOtherMethod = function(){
    if(this._ext){
      //do something ..
    }
    return this;
  };
}(jQuery));

//using ..
$(document).ready(function(){
  $('#selector').myExtension({
    //options ..
  });
  $('#selector').myExtensionOtherMethod();
});
Run Code Online (Sandbox Code Playgroud)

当我调用方法时$('#selector').myExtensionOtherMethod();,this不包含this._ext变量.我知道这是其他范围,但我知道有两种方法可以在两种方法中访问该变量.我可以这样做吗?

javascript jquery

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

MySQL自动增量主键增加10

在蔚蓝中,我创建了一个新的MySQL数据库实例。在此数据库中,我使用以下脚本创建表:

CREATE TABLE ROLES(
  ID INTEGER PRIMARY KEY AUTO_INCREMENT,
  ROLE_NAME VARCHAR(30) NOT NULL
);
Run Code Online (Sandbox Code Playgroud)

然后,我使用此脚本插入值:

INSERT INTO `beezzy`.`roles` (`ROLE_NAME`) VALUES ('admin');
INSERT INTO `beezzy`.`roles` (`ROLE_NAME`) VALUES ('owner');
INSERT INTO `beezzy`.`roles` (`ROLE_NAME`) VALUES ('consultant');
Run Code Online (Sandbox Code Playgroud)

执行表包含以下行后:

在此处输入图片说明

为什么DB会生成“ 11”和“ 21”之类的ID?我在本地计算机上运行相同的脚本,并且一切正常。ID为“ 1”,“ 2”,“ 3”

mysql azure

5
推荐指数
2
解决办法
1867
查看次数

如何为swagger API添加通用参数

我的项目中有很多带有此类注释的控制器

    @ApiOperation(value = "description")
    @RequestMapping(value = "/{param1}", method = RequestMethod.POST)
    public @ResponseBody Response<Map<String, Object>> someMethod(
       @ApiParam(name = "param1", value = "about param1", required = true)
       @PathVariable("param1") int param1,

       @ApiParam(name = "param2", value = "about param2", required = false, defaultValue = "default)
       @RequestParam(value = "param2", defaultValue = "default") String param2
    ){
           // ..
    }
Run Code Online (Sandbox Code Playgroud)

几乎每种方法都接受通用参数,例如access_token。如果将描述添加到所有方法中,将是不好的解决方案。也许还有其他解决方案?

我发现我可以json使用这样的配置来定义文件,例如:https://github.com/OAI/OpenAPI-Specification/blob/master/fixtures/v2.0/json/resources/reusableParameters.json,但是据我所知我可以使用json或注释。或者,也许我可以以某种方式将它们结合起来?

json restful-architecture swagger-ui

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

java 8中的复杂比较器

有人可以解释复杂Comparators的以下变体之间的区别吗?

List<String> listOfStrings = Arrays.asList("algo", "test", "is", "a", "common");

listOfStrings.stream()
             .sorted(Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder()))
             .sorted(Comparator.naturalOrder().thenComparing(Comparator.comparingInt(String::length))
             .forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)

为什么第一次调用sorted是可以的,而第二次调用甚至无法编译?

java type-inference comparator java-8 java-stream

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

从文件中读取GLSL着色器

我正在尝试从看起来像这样的文件中读取顶点和片段着色器

#version 330 core
in vec3 ourColor;

out vec4 color;

void main()
{
    color = vec4(ourColor, 1.0f);
}
Run Code Online (Sandbox Code Playgroud)

但是当我正在编译着色器时,我得到的错误就像'语法错误'. 在此输入图像描述

从文件中读取着色器代码的代码

const GLchar* readFromFile(const GLchar* pathToFile)
{
    std::string content;
    std::ifstream fileStream(pathToFile, std::ios::in);

    if(!fileStream.is_open()) {
        std::cerr << "Could not read file " << pathToFile << ". File does not exist." << std::endl;
        return "";
    }

    std::string line = "";
    while(!fileStream.eof()) {
        std::getline(fileStream, line);
        content.append(line + "\n");
    }

    fileStream.close();
    std::cout << "'" << content << "'" << std::endl;
    return content.c_str();
}
Run Code Online (Sandbox Code Playgroud)

c++ opengl visual-studio-2012

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