为什么简单@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) 我创建了一个简单的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变量.我知道这是其他范围,但我知道有两种方法可以在两种方法中访问该变量.我可以这样做吗?
在蔚蓝中,我创建了一个新的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”
我的项目中有很多带有此类注释的控制器
@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或注释。或者,也许我可以以某种方式将它们结合起来?
有人可以解释复杂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是可以的,而第二次调用甚至无法编译?
我正在尝试从看起来像这样的文件中读取顶点和片段着色器
#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) java ×2
azure ×1
c++ ×1
comparator ×1
hibernate ×1
java-8 ×1
java-stream ×1
javascript ×1
jquery ×1
json ×1
junit5 ×1
mysql ×1
opengl ×1
spring-boot ×1
spring-mvc ×1
swagger-ui ×1