我MockMVc使用新的Spring Boot 1.4以不同的方式设置了很少的工作代码@WebMvcTest.我理解standaloneSetup方法.我想知道的是设置之间的差异MockMvc,通过WebApplicationContext和自动装配MockMvc.
代码片段1:通过WebApplicationContext设置MockMvc
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = ProductController.class)
public class ProductControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@MockBean
private ProductService productServiceMock;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testShowProduct() throws Exception {
Product product1 = new Product();
/*Code to initialize product1*/
when(productServiceMock.getProductById(1)).thenReturn(product1);
MvcResult result = mockMvc.perform(get("/product/{id}/", 1))
.andExpect(status().isOk())
/*Other expectations*/
.andReturn();
}
}
Run Code Online (Sandbox Code Playgroud)
根据WebMvcTestAPI文档,默认情况下,使用@WebMvcTest注释的测试也将自动配置Spring Security和MockMvc.所以,我希望这里有一个401 Unauthorized状态代码,但测试通过200状态代码.
接下来,我尝试了自动接线MockMvc,但测试失败了401 …
我正在探索Java 8的新java.time API.我特别想检索当前时间(我当前的时区,不同的时区和不同的偏移量).
代码是:
public static void getCurrentLocalTime(){
LocalTime time = LocalTime.now();
System.out.println("Local Time Zone: "+ZoneId.systemDefault().toString());
System.out.println("Current local time : " + time);
}
public static void getCurrentTimeWithTimeZone(){
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZoneId zoneId = ZoneId.of("America/Los_Angeles");
ZonedDateTime dateAndTimeInLA = ZonedDateTime.of(localtDateAndTime, zoneId);
String currentTimewithTimeZone =dateAndTimeInLA.getHour()+":"+dateAndTimeInLA.getMinute();
System.out.println("Current time in Los Angeles: " + currentTimewithTimeZone);
}
public static void getCurrentTimeWithZoneOffset(){
LocalTime localtTime = LocalTime.now();
ZoneOffset offset = ZoneOffset.of("-08:00");
OffsetTime offsetTime = OffsetTime.of(localtTime, offset);
String currentTimewithZoneOffset =offsetTime.getHour()+":"+offsetTime.getMinute();
System.out.println("Current time with offset -08:00: " + …Run Code Online (Sandbox Code Playgroud) 我正在尝试新的Spring Boot 1.4 MVC测试功能.我有以下控制器.
@Controller
public class ProductController {
private ProductService productService;
@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
}
@RequestMapping(value = "/products", method = RequestMethod.GET)
public String list(Model model){
model.addAttribute("products", productService.listAllProducts());
return "products";
}
}
Run Code Online (Sandbox Code Playgroud)
我最小的ProductService实现是:
@Service
public class ProductServiceImpl implements ProductService {
private ProductRepository productRepository;
@Autowired
public void setProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@Override
public Iterable<Product> listAllProducts() {
return productRepository.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
ProductRepository的代码是:
public interface ProductRepository extends CrudRepository<Product,
Integer>{
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用新的@WebMvcTest来测试控制器.我的观点是百里香的teamplate.我的控制器测试是这样的:
@RunWith(SpringRunner.class)
@WebMvcTest(ProductController.class)
public …Run Code Online (Sandbox Code Playgroud) 我的要求是在 Angular 2 应用程序中捕获实时音频(用户在麦克风上讲话)并将其流式传输到 Spring Boot REST API。
我搜索了很多但找不到任何方向。任何有关相同内容的指示都会非常有帮助。
提前致谢。
我正在尝试在log4j2.yaml中使用属性。等效的XML是这个。
<Configuration>
<Properties>
<Property name="log-path">logs</Property>
<Property name="archive">${log-path}/archive</Property>
</Properties>
<Appenders>
. . .
Run Code Online (Sandbox Code Playgroud)
我试过了
Configutation:
name: Default
properties:
property:
name: log-path
value: "logs"
name: archive
value: ${log-path}/archive
Appenders:
Run Code Online (Sandbox Code Playgroud)
但是这些属性没有被选择。例如,以下代码创建一个$ {log-path}文件夹来存储日志文件,而不是所需的日志文件夹。
fileName: ${log-path}/rollingfile.log
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
虽然我有一些基本的JavaScript背景,但我偶然发现了我写的这段代码:
var data=[{"_id":"57b3e7ec9b209674f1459f36","fName":"Tom","lName":"Moody","email":"Tom@example.com","age":30},{"_id":"57b3e8079b209674f1459f37","fName":"Pat","lName":"Smith","email":"pat@example.com","age":32},{"_id":"57b3e8209b209674f1459f38","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28},{"_id":"57b3e8219b209674f1459f39","fName":"Sam","lName":"Dawn","email":"sam@example.com","age":28}]
var tempArr=[];
var table=[];
var dataArr = Object.keys(data).map(function(k) { return data[k] });
dataArr.forEach(function(user) {
tempArr[0]=user.fName;
tempArr[1]=user.lName;
tempArr[2]=user.email;
tempArr[3]=user.age;
table.push(tempArr);
console.log('table'+JSON.stringify(table));
});
Run Code Online (Sandbox Code Playgroud)
在最后一个循环中,我希望表包含Tom,Pat和Sam的数组.相反,这就是我得到的:
table[["Tom","Moody","Tom@example.com",30]]
table[["Pat","Smith","pat@example.com",32],["Pat","Smith","pat@example.com",32]]
table[["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28]]
table[["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28],["Sam","Dawn","sam@example.com",28]]
Run Code Online (Sandbox Code Playgroud)
为什么push()替换表中的前一个条目?任何帮助将受到高度赞赏.
在尝试从与插入的 Nodejs 模块不同的 Nodejs 模块中检索 redis 时,我收到“找不到模块的错误”:
ERROR in ./~/redis/index.js
Module not found: Error: Cannot resolve module 'net' in E:\Code-
Asst\node_modules\redis
@ ./~/redis/index.js 3:10-24
ERROR in ./~/redis/index.js
Module not found: Error: Cannot resolve module 'tls' in E:\Code-
Asst\node_modules\redis
@ ./~/redis/index.js 4:10-24
ERROR in ./~/redis/index.js
Module not found: Error: Cannot resolve module 'net' in E:\Code-
Asst\node_modules\redis
@ ./~/redis/index.js 3:10-24
ERROR in ./~/redis/index.js
Module not found: Error: Cannot resolve module 'tls' in E:\Code-
Asst\node_modules\redis
@ ./~/redis/index.js 4:10-24
ERROR in ./~/redis-parser/lib/hiredis.js
Module not …Run Code Online (Sandbox Code Playgroud) 我是 AWS 的新手,我正在尝试从 Java 程序对本地 DynamoDB 执行 CRUD 操作。Java 程序是一个 AWS 示例。
我安装了 AWS CLI 并设置了以下配置 - 根据 AWS 文档,我不需要本地 DynamoDB 的真正 AWS 访问和密钥。
我通过在 AWS CLI 中运行 aws configure 在 ~/.aws/config 和 ~/.aws/credentials 中设置了以下值。
[default]
aws_access_key_id = ''
aws_secret_access_key = ''
[default]
region = ap-south-1
Run Code Online (Sandbox Code Playgroud)
我有运行这个的本地 DYnamoDB JAR。
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
我试图运行的代码是这样的。
然而,我收到了这个异常。
AmazonDynamoDBException:请求中包含的安全令牌无效。
完整的堆栈是这样的。
Exception in thread "main" com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The security token included in the request is invalid. (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使所有 Log4J 2 记录器与 IMAP Disruptor 异步。我正确设置了干扰器依赖项,并且在 IntelliJ 中,我在 VM 选项下设置了以下系统属性。
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
Run Code Online (Sandbox Code Playgroud)
我的 log4j2.xml 文件是这样的。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>>
</PatternLayout>
</Console>
<File name="File-Appender" fileName="logs/xmlfilelog.log" >
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="guru.springframework.blog.log4j2async" level="debug">
<AppenderRef ref="File-Appender"/>
</Logger>
<Root level="debug">
<AppenderRef ref="Console-Appender"/>
</Root>
</Loggers>
</Configuration>
Run Code Online (Sandbox Code Playgroud)
我的记录器类有这个代码。
package guru.springframework.blog.log4j2async;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4J2AsyncLogger {
private static …Run Code Online (Sandbox Code Playgroud) 完全是Mockito的新手,这是我开始的:
受测试的类User.java:
package com.test.mockito;
public class User {
private ProductManager productManager;
public boolean buy(Product product, int quantity) throws InsufficientProductsException {
boolean transactionStatus=false;
int availableQuantity = productManager.getAvailableProducts(product);
if (quantity < availableQuantity) {
throw new InsufficientProductsException();
}
productManager.orderProduct(product, quantity);
transactionStatus=true;
return transactionStatus;
}
public void setProductManager(ProductManager productManager) {
this.productManager = productManager;
}
}
Run Code Online (Sandbox Code Playgroud)
模拟对象:Product.java
package com.test.mockito;
public class Product {
}
Run Code Online (Sandbox Code Playgroud)
ProductManager.java
package com.test.mockito;
public interface ProductManager {
int getAvailableProducts(Product product);
int orderProduct(Product product, int num);
}
Run Code Online (Sandbox Code Playgroud)
异常类:InsufficientProductsException.java
package com.test.mockito;
public class …Run Code Online (Sandbox Code Playgroud) java ×4
log4j2 ×2
spring-boot ×2
spring-mvc ×2
angular ×1
arrays ×1
dynamo-local ×1
java-8 ×1
java-time ×1
javascript ×1
junit ×1
log4j ×1
logging ×1
mockito ×1
node.js ×1
testing ×1
unit-testing ×1
webpack ×1
yaml ×1