小编kry*_*ger的帖子

当类有外部依赖时,如何编写有效的单元测试?

我是测试驱动开发的新手,无法弄清楚如何为我编写的类编写有效的测试.该类如下(Java):

public class MyServiceClassImpl implements MyService {
    private someExternalClient client;
    private anotherExternalClient anotherClient;

    public MyServiceClassImpl() {
        client = someExternalClient.getInstance();
        anotherClient = anotherExternalClient(client);
    }

    public String methodWhichDoesSomething(String query) {
        return anotherClient.getResponse(query);
    }
}
Run Code Online (Sandbox Code Playgroud)

对于测试,我尝试了几个查询,并将我得到的响应与我期望的响应进行比较(我期待它,因为我知道anotherClient将返回什么).它工作正常但这在技术上是一个集成测试,因为我调用外部依赖.在这种情况下,我不明白如何编写"单元"测试.更具体地说,我不知道如何模拟依赖项,因为字段是私有的,没有setter,构造函数不接受任何参数.即使我创建了它们,我如何用我的模拟"提供"类的实例?我自己也写了这个课程,所以请告诉我是否应该重新设计课程,或者提供入门者和制定者?

junit integration-testing unit-testing easymock mocking

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

如何使用ng-bind-html?

任何想法为什么这个绑定不起作用?

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
  $scope.myHTML = "<a href='#'>a link</a>";
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-controller="MyCtrl">
  <p ng-bind-html="myHTML"></p>
</div>
Run Code Online (Sandbox Code Playgroud)

angularjs

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

Spring - 我应该使用@Bean还是@Component?

这是我工作中的当前代码.

方法1

@Configuration
public class AppConfig {

    @Bean
    @Autowired(required = false)
    public HttpClient createHttpClient() {
        // do some connections configuration
        return new HttpClient();
    }

    @Bean
    @Autowired
    public NameClient nameClient(HttpClient httpClient,
                                 @Value("${ServiceUrl:NotConfigured}") 
                                 String serviceUrl) {
        return new NameClient(httpClient, serviceUrl);
    }

}
Run Code Online (Sandbox Code Playgroud)

NameClient是一个简单的POJO,如下所示

public class NameClient {
    private HttpClient client;
    private String url;

    public NameClient(HttpClient client, String url) {
        this.client = client;
        this.url = url;
    }

    // other methods 
}
Run Code Online (Sandbox Code Playgroud)

@Bean我没有使用配置,而是想要遵循这种模式:

方法2

@Configuration
public class AppConfig {

  @Bean
  @Autowired(required …
Run Code Online (Sandbox Code Playgroud)

spring dependency-injection spring-mvc autowired

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

获取所有与角色相关的Sentinel用户

如何通过Sentinel获取与用户角色相关的所有用户?以及如何让Sentinel中的所有用户使用?这不起作用:

Sentinel::all()
Run Code Online (Sandbox Code Playgroud)

laravel cartalyst-sentinel

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

清除 NetBeans 8.2 缓存

我在 NetBeans 8.2 中创建了一个项目,我想清除 NetBeans 缓存。

netbeans netbeans-8

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

Spring MVC 3 + JSON

我正在尝试使用带有JSON的Spring MVC.当从控制器返回一个对象时,它工作得很好,但是当我尝试将一个自定义对象作为参数传递给AJAX时,我收到HTTP 415错误.

我的spring-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <tx:annotation-driven /> 
   <context:annotation-config/>
    <context:component-scan
        base-package="com.sommer.controller" />
        <tx:annotation-driven transaction-manager="transactionManager"/>

    <context:component-scan base-package="com.sommer.service" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>


 <!-- ========= [ADDED FOR JSON SUPPORT] ========= -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
       <property name="messageConverters">
           <list>
                <ref bean="jsonConverter" />
           </list>
       </property>
    </bean>

    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
       <property name="supportedMediaTypes" value="application/json" /> …
Run Code Online (Sandbox Code Playgroud)

json spring-mvc

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


如何使用grep只搜索特定的文件类型?

我有很多文件,我想找到它的位置MYVAR.

我确定它是在一个.yml文件中但我在grep手册中找不到如何指定文件类型.

grep

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

Spring JDBC指定表以在运行时进行查询

我的应用程序动态创建表,而且我不知道如何在不将其硬编码到字符串查询中的情况下使用Spring jdbc读取表。我在想这样的事情:

jdbcTemplate.query("SELECT * FROM ?", new Object[] { tableName }, new TableMapper());
Run Code Online (Sandbox Code Playgroud)

但是春天不喜欢问号:-(

谢谢你的帮助!

mysql spring jdbc

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

jquery验证未加载requirejs

我在RequireJS中有一个主模块:

require([
    'jquery',
    'jquery.validate',
    'jquery.validate.unobtrusive'
], function ($) {
    $(document).ready(function () {
        var validator = $("form").validate();
        if ($("#txtFirstName").val() !== "")
            validator.element("#txtFirstName");
    });
});
Run Code Online (Sandbox Code Playgroud)

当我加载此页面时,出现JavaScript错误:

TypeError:$(...).validate不是函数var validator = $("form").validate();**

我现在不知道为什么?加载所有脚本:

在此输入图像描述

jquery unobtrusive-javascript jquery-validate requirejs unobtrusive-validation

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