小编Jac*_*ark的帖子

JUnit 和 Mockito 空指针异常

我有一个名为 Pinger Service 的类,它调用 HttpAdapter 打开 HttpURLConnection 并返回它,然后调用 getResponseCode 来查明 URL 是否为 200。

我试图模拟 HttpURLConnection 并使 getResponseCode 返回 404,但是我收到空指针异常

测试

package com.uk.jacob.service;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.HttpURLConnection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.uk.jacob.SimplePingApplication;
import com.uk.jacob.adapter.HttpAdapter;
import com.uk.jacob.model.Website;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SimplePingApplication.class)
@WebAppConfiguration
public class PingerServiceTests {

    @Autowired
    PingerService pingerService;

    @Mock
    HttpAdapter httpAdapter;

    @Mock
    HttpURLConnection mockHttpURLConnection;

    @Test
    public void testPingerServiceReturnsOkWhenServiceIsUp() throws IOException{
        Website website = pingerService.ping("http://devnews.today");

        assertEquals(true, …
Run Code Online (Sandbox Code Playgroud)

java junit spring spring-boot

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

Objective-C 双、长计算

我正在尝试计算一个 long 值除以一个整数,以给出我期望的 double 值,尽管我得到的结果是 0。我正在使用的代码......

double daysByYear = daysSinceBirthdayToService/365;
NSLog(@"%d", daysByYear);
Run Code Online (Sandbox Code Playgroud)

在这段代码中, daysSinceBirthdayToService 变量是一个 Long Double,可以使用以下代码进行 NSLogged (long)daysSinceBirthdayToService

它在头文件中声明为

@property (nonatomic) NSInteger daysSinceBirthdayToService;
Run Code Online (Sandbox Code Playgroud)

谁能帮我解决这个问题,谢谢!

cocoa-touch objective-c ios

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

将向量复制到线程

考虑下面的代码,我想有两个接入clientrequests我的线程内,目前我不知道:

for _x in 0..100 {
        let handle = thread::spawn(move || {
            let start = time::precise_time_s();

            let res = client.get("http://jacob.uk.com")
                .header(Connection::close()) 
                .send().unwrap();

            let end = time::precise_time_s();

            requests.push(Request::new(end-start));
        });

        handle.join().unwrap()
    }
Run Code Online (Sandbox Code Playgroud)

我得到以下编译器错误:

   Compiling Herd v0.1.0 (file:///Users/jacobclark/Desktop/LearningRust/Herd)
src/main.rs:41:23: 41:29 error: capture of moved value: `client`
src/main.rs:41             let res = client.get("http://jacob.uk.com")
                                     ^~~~~~
src/main.rs:38:41: 48:10 note: `client` moved into closure environment here because it has type `[closure(())]`, which is non-copyable
src/main.rs:38         let handle = thread::spawn(move || {
src/main.rs:39             let start …
Run Code Online (Sandbox Code Playgroud)

concurrency multithreading rust

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

无法迭代Arc Mutex

考虑下面的代码,我将每个线程附加到Vector,以便在我生成每个线程之后将它们连接到主线程,但是我无法调用iter()JoinHandlers的向量.

我该怎么做呢?

fn main() {
    let requests = Arc::new(Mutex::new(Vec::new()));
    let threads = Arc::new(Mutex::new(Vec::new()));

    for _x in 0..100 {
        println!("Spawning thread: {}", _x);

        let mut client = Client::new();
        let thread_items = requests.clone();

        let handle = thread::spawn(move || {
            for _y in 0..100 {
                println!("Firing requests: {}", _y);

                let start = time::precise_time_s();

                let _res = client.get("http://jacob.uk.com")
                    .header(Connection::close()) 
                    .send().unwrap();

                let end = time::precise_time_s();

                thread_items.lock().unwrap().push((Request::new(end-start)));
            }
        });

        threads.lock().unwrap().push((handle));
    }

    // src/main.rs:53:22: 53:30 error: type `alloc::arc::Arc<std::sync::mutex::Mutex<collections::vec::Vec<std::thread::JoinHandle<()>>>>` does not implement any method in scope …
Run Code Online (Sandbox Code Playgroud)

multithreading asynchronous rust

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

无法设置Iron框架响应的标头

我想Response用以下代码设置Iron的标题:

extern crate iron;  // 0.3.0
extern crate hyper; // 0.8.1

use iron::prelude::*;
use iron::status;

use hyper::header::{Headers, ContentType};
use hyper::mime::{Mime, TopLevel, SubLevel};

use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

fn main() {
    fn hello_world(_: &mut Request) -> IronResult<Response> {
        let mut headers = Headers::new();
        let string = getFileAsString("./public/index.html");

        headers.set(
            ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![]))
        );

        Ok(Response::with((status::Ok, string, headers)))
    }

    Iron::new(hello_world).http("localhost:3000").unwrap();
    println!("On 3000");
}

fn getFileAsString(fileStr: &str) -> String {
    let path = Path::new(fileStr);
    let display = path.display();
    let mut …
Run Code Online (Sandbox Code Playgroud)

iron rust

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

Kubernetes与Google Cloud DNS

使用运行Kubernetes的Google Container Engine集群,将http://mydomain.co.uk指向LoadBalanced ReplicationController 的过程将是什么?

我知道Kubernetes支持SkyDNS-如何将域名的Google Cloud DNS委派给内部Kubernetes集群DNS服务?

google-cloud-platform kubernetes google-kubernetes-engine

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

在对象类型'UITextField'上找不到属性'origin'

当键盘出现在iPhone上时,我正在尝试向上滚动文本框,我使用以下代码但是我收到错误:

在对象类型'UITextField'上找不到属性'origin'

studentLoanTextBox.frame = CGRectMake(
    studentLoanTextBox.origin.x, 
    studentLoanTextBox.origin.y - 100.0, 
    studentLoanTextBox.frame.size.width, 
    studentLoanTextBox.frame.size.height
);
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c

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