小编AHu*_*ist的帖子

使用RestTemplate,如何首先将请求发送到代理,以便我可以将我的junits与JMeter一起使用?

我在使用Spring-MVC 3.0实现的开发盒上运行了一个Web服务.我有各种JUnit使用RestTemplate测试该服务.我想要做的是让JMeter在运行时获取这些JUnits REST请求.但是,要做到这一点,我需要让Spring的RestTemplate将它们发送到我正在运行JMeter的代理.所以,问题是,我该怎么做?

我已经用CXF和他们的http:conduit和http:client做了类似的东西,但我真的不知道怎么用Spring-MVC做这个.

java rest proxy spring-mvc jmeter

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

如何使用Spring测试具有@PostConstruct方法的类的构造函数?

如果我有一个带有@PostConstruct方法的类,我如何使用JUnit和Spring测试其构造函数及其@PostConstruct方法?我不能简单地使用新的ClassName(param,param),因为它不使用Spring - @PostConstruct方法没有被触发.

我错过了一些明显的东西吗?

public class Connection {

private String x1;
private String x2;

public Connection(String x1, String x2) {
this.x1 = x1;
this.x2 = x2;
}

@PostConstruct
public void init() {
x1 = "arf arf arf"
}

}


@Test
public void test() {
Connection c = new Connection("dog", "ruff");
assertEquals("arf arf arf", c.getX1();
}
Run Code Online (Sandbox Code Playgroud)

我有类似的东西(虽然稍微复杂一些)并且@PostConstruct方法没有被击中.

java junit spring unit-testing postconstruct

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

Spring RestTemplate处理状态为NO_CONTENT的响应时的行为

好的,我有一个名为NamedSystems的类,它的唯一字段是一组NamedSystem.

我有一种方法可以通过某些标准找到NamedSystems.那不是很重要.当它得到结果时,一切正常.但是,当它找不到任何东西,从而返回null(或空 - 我已尝试过两种方式)设置时,我就会遇到问题.让我解释.

我正在使用Spring RestTemplate类,我在单元测试中进行这样的调用:

ResponseEntity<?> responseEntity = template.exchange(BASE_SERVICE_URL + "?
  alias={aliasValue}&aliasAuthority={aliasAssigningAuthority}", 
  HttpMethod.GET, makeHttpEntity("xml"), NamedSystems.class, 
  alias1.getAlias(), alias1.getAuthority());
Run Code Online (Sandbox Code Playgroud)

现在,因为这通常会返回200,但我想返回204,我的服务中有一个拦截器,它确定ModelAndView是否是NamedSystem,如果它的集合为null.如果是,我然后将状态代码设置为NO_CONTENT(204).

当我运行junit测试时,我收到此错误:

org.springframework.web.client.RestClientException: Cannot extract response: no Content-Type found
Run Code Online (Sandbox Code Playgroud)

将状态设置为NO_CONTENT似乎擦除了内容类型字段(当我考虑它时这确实有意义).那为什么还要看呢?

Spring的HttpMessageConverterExtractor extractData方法:

public T extractData(ClientHttpResponse response) throws IOException {
    MediaType contentType = response.getHeaders().getContentType();
    if (contentType == null) {
        throw new RestClientException("Cannot extract response: no Content-Type found");
    }
    for (HttpMessageConverter messageConverter : messageConverters) {
        if (messageConverter.canRead(responseType, contentType)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Reading [" + responseType.getName() + "] as \"" + contentType
                    +"\" using …
Run Code Online (Sandbox Code Playgroud)

java rest unit-testing web-services spring-mvc

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

键入时的平均Inter-Keypress时间

我试图谷歌寻求答案,但也许没有广泛可用的研究,或者我可能没有使用正确的条款.

基本上,我想知道打字时按键之间的平均时间.我想知道这个的原因是我正在进行模糊搜索,这将在下拉列表中使用.我们可以采取一些措施来提高结果的准确性,但这会导致速度变慢.但是,如果这样的速度仍然低于按键间时间的合理阈值,则实施该改变是有意义的.

任何帮助,将不胜感激.

performance typing keypress response-time

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

Spring Java配置上下文的事务配置

我几个小时以来一直在努力.

我正在尝试将我的Spring XML配置迁移到完全基于Java的配置.

我正在使用AnnotationConfigApplicationContext上下文实现.

我从旧的XML配置中找不到这行的Java等价物:

<tx:annotation-driven transaction-manager="transactionManager" />
Run Code Online (Sandbox Code Playgroud)

因此,Spring不管理事务.

在我的Java配置中,我已初始化了事务的相关bean:会话工厂,事务管理器等,但没有该行,则不使用事务代理,因此实际上没有事务处理.

所以我的问题是如何将该行转换为我的Java上下文配置或如何以另一种方式解决问题.

任何帮助表示赞赏.谢谢.

java spring annotations transactions spring-aop

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

如何在Spring中拦截静态方法?

主题行基本上都说明了一切.我有一个静态方法,我想拦截,以便可以应用它周围的建议.我可以使用任何非静态方法,但我不确定如何允许拦截静态方法.

java aop spring interceptor

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

将Spring任务XML配置转换为代码配置

我正在尝试将使用Spring的任务框架的XML配置转换为纯粹的代码配置.我能够重现这个功能,但每当我关闭Tomcat服务器上的任务调度程序的战争时,它就会挂起(它不会挂起XML配置).我已经调试过来检查调度程序和执行程序的实例,但是我没有看到差异所以我不确定是什么导致它挂起.

以下是适用的XML配置:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">

   <task:executor id="com.work.gwx.pix.executor"
      pool-size="${pix.job.executor.pool.size:1-40}"
      queue-capacity="${pix.job.executor.queue.capacity:0}"
      rejection-policy="CALLER_RUNS"/>

   <task:scheduler id="com.work.gwx.pix.scheduler" pool-size="${pix.job.scheduler.pool.size:4}"  />

    <task:annotation-driven executor="com.work.gwx.pix.executor" scheduler="com.work.gwx.pix.scheduler" />

    <bean id='queueProcessor' class="com.work.gwx.queueing.QueueProcessor" /> 

 </beans>
Run Code Online (Sandbox Code Playgroud)

这是代码配置:

@EnableAsync
@EnableScheduling
@Configuration
public class TaskConfiguration implements AsyncConfigurer, SchedulingConfigurer {

    @Value("${pix.job.executor.max.pool.size:1}")
    private int executorMaxPoolSize;

    @Value("${pix.job.executor.queue.capacity:0}")
    private int executorQueueCapacity;

    @Value("${pix.job.scheduler.pool.size:4}")
    private int schedulerPoolSize;

    @Bean(destroyMethod = "shutdown")
    public Executor pixTaskScheduler() {
        final ScheduledThreadPoolExecutor ex = new ScheduledThreadPoolExecutor(schedulerPoolSize, new ThreadPoolTaskExecutor());
        // ex.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
        return ex;
    }

    @Bean
    public Executor pixExecutor() {
        final ThreadPoolTaskExecutor executor = …
Run Code Online (Sandbox Code Playgroud)

java configuration spring scheduled-tasks task

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

一个方法中的每个可能的分支都应该有一个单独的junit吗?

这更像是一个设计问题.

假设您有这样的方法(作为示例):

if (x == 5) {
  c = 1;
} else {
 if (z != 2) {
  b = 6;
} else {
   a = 3;
}
Run Code Online (Sandbox Code Playgroud)

您是否认为为每个可能的分支设置junit是最佳做法?即,testx5,测试xnot5znot2,testxnot5z2等,或类似的东西:

void testMethod() {
// x is 5
test/assert code;

// x not 5, z not 2
test/assert code;

// x not 5, z is 2
test/assert code

// etc
}
Run Code Online (Sandbox Code Playgroud)

编辑:为了清楚起见,我的目标是完整的代码覆盖率.我只想知道是否应该为每个分支进行新测试或将它们组合在一个测试中.谢谢您的意见.

java testing junit unit-testing

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

是否有更好的方法来保持地图的价值是一组?

我看起来很多能够使用Hibernate来保持地图,就像Map<String, Set<Entity>>运气一样(特别是因为我希望它全部放在一张桌子上).

使用Hibernate映射MultiMaps是最常被引用的东西,它详细描述了如何使用a来实现它UserCollectionType.

我想知道,因为这是四年前写的,现在还有更好的方法吗?

所以,例如,我想在EntityA地图上有Map<String, Set/List<EntityB>>.

将有两个表:EntityAEntityB(EntityB返回一个外键EntityA).

我不想要任何中间表.

java hibernate map

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

如何从XML Spring调度配置转向注释/代码配置?

我试图将以下Spring任务xml配置转换为纯粹基于代码/注释的版本:

<task:executor id="xyz.executor"
    pool-size="${xyz.job.executor.pool.size:1-40}"
    queue-capacity="${xyz.job.executor.queue.capacity:0}"
    rejection-policy="CALLER_RUNS"/>

<task:scheduler id="xyz.scheduler" pool size="${xyz.job.scheduler.pool.size:4}"  />

<task:annotation-driven executor="xyz.executor" scheduler="xyz.scheduler" />

<bean id='xyzProcessor' class="xyz.queueing.QueueProcessor" /> 

<task:scheduled-tasks scheduler="xyz.scheduler" >
    <task:scheduled ref="partitioner" method="createPartitions" cron="${xyz.job.partitioner.interval:0 0 3 * * *}" />
</task:scheduled-tasks>
Run Code Online (Sandbox Code Playgroud)

根据Spring规范28.4.1(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html),他们说要像这样从XML 转发:

<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>
Run Code Online (Sandbox Code Playgroud)

代码配置就像启用@EnableScheduling和/或@EnableAsync一样简单.

但是,我没有看到任何可以实际实例化调度程序的地方.@EnableScheduling的javadoc(http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html)展示了如何插入我自己创建的Executor,虽然我不确定它应该是什么类(我仍然希望能够控制池大小,队列容量和拒绝策略).它还显示了如何使用configureTasks覆盖来调度我的createPartitions方法.但是,我希望能够命名我的调度程序(以便我可以识别其线程)并控制其池大小.

所以,我想知道这些事情:

1)我可以使用哪个类来设置XML所具有的执行程序字段?

2)有没有办法创建一个我可以控制名称和池大小的调度程序实例?

java configuration spring annotations scheduled-tasks

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