小编pez*_*tem的帖子

编写下载文件的单元测试

目前我写了一个小的downloadService,让用户下载一个文件(目前只有excel).代码工作正常,但我不知道如何为它编写单元测试.那是我的代码:

package com.pzm.service;

import com.pzm.model.UserBillingsMock;
import com.pzm.model.report.ExcelReport;
import com.pzm.model.report.Report;
import com.pzm.model.report.ReportFactory;
import org.springframework.stereotype.Repository;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by akfaz on 6/26/14.
 */

@Repository
public class DownloadService {

    private Report report;
    private List<UserBillings> userBillings;

    public void setBill(List<UserBillings> userBillings) {
        this.userBillings = userBillings;
    }

    public void download(HttpServletResponse response, String reportType) {

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls");

        report = new ReportFactory().create(reportType, userBillings);
        saveFile(response, report);
    }

    private void saveFile(HttpServletResponse response, Report report) {
        try {
            ServletOutputStream outputStream = …
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing mockito

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

如何使用stream reduce保留所有列表列表

我遇到了以下问题.我有一个列表列表,我只想保留所有.我正在尝试使用流

private List<List<Long>> ids = new ArrayList<List<Long>>();

// some ids.add(otherLists);  

List<Long> reduce = ids.stream().reduce(ids.get(0), (a, b) -> a.addAll(b));
Run Code Online (Sandbox Code Playgroud)

不幸的是我收到了错误

Error:(72, 67) java: incompatible types: bad return type in lambda expression
    boolean cannot be converted to java.util.List<java.lang.Long> 
Run Code Online (Sandbox Code Playgroud)

java-8 java-stream

7
推荐指数
2
解决办法
8795
查看次数

Bash - 命令未找到,但仍然有效

我为音量修改编写了这样的代码:

#!/bin/bash

case "$1" in
        down)
            $(amixer -c 0 sset Speaker  5%-)
            $(amixer -D pulse sset Master 5%-)  
            ;;  
        up)
            $(amixer -c 0 sset Speaker  5%+)
            $(amixer -D pulse sset Master 5%+)
            ;;
        mute)
            $(amixer -c 0 sset Speaker  0)
            $(amixer -D pulse sset Master 0)
            ;;
        *)
            echo $"Usage: $0 {down|up|mute}"
            exit 1
esac
Run Code Online (Sandbox Code Playgroud)

那么代码工作正常,但是当我在终端中执行它时,我得到了以下信息:

[akfaz@localhost utils]$ ./audioControl.sh up
./audioControl.sh: line 9: Simple: command not found
./audioControl.sh: line 10: Simple: command not found
Run Code Online (Sandbox Code Playgroud)

你知道发生了什么吗?

bash

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

为什么@RequestBody不需要arg构造函数?

我只是在玩 spring-boot,只是想创建一个控制器method = RequestMethod.POST

我的控制器:

@RequestMapping(value = "/user/signup",
        method = RequestMethod.POST)
private String signUpNewUser(@Valid @RequestBody SignInUserForm userForm){
        // validation + logic
}
Run Code Online (Sandbox Code Playgroud)

注册用户表单类:

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class SignInUserForm {
    @NotEmpty
    @Email
    private String email;

    @NotEmpty
    @Size(min = 8, max = 24)
    private String password;

    @NotEmpty
    @Size(min = 8, max = 24)
    private String repeatedPassword;
}
Run Code Online (Sandbox Code Playgroud)

最后是我的测试:

@Test
    public void shouldCallPostMethod() throws Exception {

        SignInUserForm signInUserForm = new SignInUserForm("test@mail.com", "aaaaaaaa", "aaaaaaaa");

        String json = new Gson().toJson(signInUserForm);

        mockMvc.perform(
                MockMvcRequestBuilders.post("/user/signup")
                    .contentType(MediaType.APPLICATION_JSON_VALUE) …
Run Code Online (Sandbox Code Playgroud)

java rest spring-boot

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

标签 统计

java ×2

bash ×1

java-8 ×1

java-stream ×1

junit ×1

mockito ×1

rest ×1

spring-boot ×1

unit-testing ×1