小编Yug*_*hou的帖子

为什么我的gradle项目在Intellij Idea中为main和test创建了独立的模块

最近,我发现我在Idea中的所有gradle项目导入了main和test的分离模块.模块看起来像这样:

在此输入图像描述

如您所见,有一个"主"模块,其内容根是src/main,仅包含主类和资源,还有一个"测试"模块.模块看起来不对劲.这是预期的行为吗?

想法是Intellij Idea 2016.1.1和gradle是2.11

这是build.gradle的内容

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: "jacoco"

version = getVersion()

sourceCompatibility = 1.8
targetCompatibility = 1.8

configurations {
    provided
}


sourceSets {
    main {
        compileClasspath += configurations.provided
    }
    test {
        resources {
            srcDir 'src/test/data'
        }

        compileClasspath += configurations.provided
    }
}

processResources {
    filter { String line -> line.replace("{version}", getVersion()) }
}

processTestResources {
    filter { String line -> line.replace("{version}", getVersion()) }
}

idea {
    module { …
Run Code Online (Sandbox Code Playgroud)

intellij-idea gradle

36
推荐指数
3
解决办法
8052
查看次数

使用Mockito时如何在弹簧测试中清理模拟

我对Mockito很新,在清理方面遇到了一些麻烦.

我曾经使用JMock2进行单元测试.据我所知,JMock2在上下文中保留了期望和其他模拟信息,这些信息将针对每种测试方法进行重建.因此,每种测试方法都不会受到其他测试方法的干扰.

我在使用JMock2时采用了相同的弹簧测试策略,我发现我在帖子中使用的策略存在潜在问题:应用程序上下文是针对每个测试方法重建的,因此减慢了整个测试过程.

我注意到许多文章建议在春季测试中使用Mockito,我想尝试一下.它运行良好,直到我在测试用例中编写两个测试方法.每个测试方法在单独运行时通过,其中一个如果一起运行则失败.我推测这是因为模拟信息保存在模拟本身中(因为我没有在JMock中看到任何类似的上下文对象)并且模拟(和应用程序上下文)在两个测试方法中共享.

我通过在@Before方法中添加reset()来解决它.我的问题是处理这种情况的最佳做法是什么(reset()的javadoc说如果你需要reset(),代码就闻到了?)?任何想法都是欣赏,提前谢谢.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "file:src/main/webapp/WEB-INF/booking-servlet.xml",
    "classpath:test-booking-servlet.xml" })
@WebAppConfiguration
public class PlaceOrderControllerIntegrationTests implements IntegrationTests {

@Autowired
private WebApplicationContext wac;

private MockMvc mockMvc;

@Autowired
private PlaceOrderService placeOrderService;

@Before
public void setup() {
    this.mockMvc = webAppContextSetup(this.wac).build();

    reset(placeOrderService);// reset mock
}

@Test
public void fowardsToFoodSelectionViewAfterPendingOrderIsPlaced()
        throws Exception {

    final Address deliveryAddress = new AddressFixture().build();
    final String deliveryTime = twoHoursLater();
    final PendingOrder pendingOrder = new PendingOrderFixture()
            .with(deliveryAddress).at(with(deliveryTime)).build();

    when(placeOrderService.placeOrder(deliveryAddress, with(deliveryTime)))
            .thenReturn(pendingOrder);

    mockMvc.perform(...);

}

@Test
public void returnsToPlaceOrderViewWhenFailsToPlaceOrder() throws Exception …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-test mockito

26
推荐指数
5
解决办法
4万
查看次数

在buildscript闭包中访问项目额外属性

我是新手,对项目属性有一些疑问.

我需要在build.gradle中的多个位置声明spring boot依赖项,并且我想使用变量来定义版本.在gradle中最好的方法是什么?(在Maven中,我使用属性)

我的尝试是使用额外的属性,但它无法访问buildscript闭包中的属性.我搜索了许多文章,阅读自定义任务中的属性.我错过了什么?

ext {
    springBootVersion = '1.1.9.RELEASE'
}

buildscript {

    print project.springBootVersion //fails here

    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${project.springBootVersion}")
    }
}

install {
    repositories.mavenInstaller {
        pom.project {
            parent {
                groupId 'org.springframework.boot'
                artifactId 'spring-boot-starter-parent'
                version "${project.springBootVersion}" //this one works
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

gradle

15
推荐指数
3
解决办法
2935
查看次数

phpMyAdmin相当于Redis的MySQL?

我用Predis来操作Redis.如果我有像PhpMyAdmin这样的工具会很有帮助,这在开发支持MySQL的应用程序时非常有用.

什么是相同于MySQL的phpMyAdmin?

更新:最后,我找到了phpRedisAdmin.如果您想要直观地查看整个数据集,这非常方便.它的github链接:https://github.com/ErikDubbelboer/phpRedisAdmin

php redis

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

如何在CQRS中建立银行转账模型

我正在阅读会计模式,并对在CQRS中实施会计模式感到好奇.

我认为AccountingTransaction是一个聚合根,因为它保护了不变量:

没有钱泄漏,它应该从一个帐户转移到另一个帐户.

public class AccountingTransaction {
    private String sequence;
    private AccountId from;
    private AccountId to;
    private MonetaryAmount quantity;
    private DateTime whenCharged;

    public AccountingTransaction(...) {
        raise(new AccountingEntryBookedEvent(sequence, from, quantity.negate(),...);
        raise(new AccountingEntryBookedEvent(sequence, to, quantity,...);
    }
}
Run Code Online (Sandbox Code Playgroud)

将AccountingTransaction添加到其存储库时.它发布了几个AccountingEntryBookedEvent,用于更新查询端的相应帐户余额.

每个db事务更新一个聚合根,最终一致性,到目前为止一直很好.

但是,如果某些帐户应用转移限制,例如无法将数量转移到当前余额,会怎样?我可以使用查询方来获得帐户的余额,但我担心来自查询方的数据是陈旧的.

public class TransferApplication {
    public void transfer(...) {
        AccountReadModel from = accountQuery.findBy(fromId);
        AccountReadModel to = accountQuery.findBy(toId);
        if (from.balance() > quantity) {
            //create txn
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该在命令端建模帐户吗?我必须为每个数据库事务更新至少三个聚合根(从/到帐户和帐户txn).

public class TransferApplication {
    public void transfer(...) {
        Account from = …
Run Code Online (Sandbox Code Playgroud)

domain-driven-design cqrs event-sourcing

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

为什么每个事务只修改一个聚合实例?

我正在阅读Vernon的文章Effective Aggregate Design.我有一个问题,为什么每个事务只修改一个聚合实例?

让我们举个例子,考虑一下Warehouse逆变管理故事.

库存表示仓库中具有数量的物料.5 例如,在上海仓库实施领域驱动设计书籍.

Entry表示有关Inventory的进/出操作的日志.例如,在上海仓库中输入2个实施领域驱动设计书籍.

一个库存的数量需要的,如果要改变输入提交.

我很容易想到,这是一个不变的,可以通过事务一致性来实现.

解决方案A:使用一个聚集和集群进入库存.

public class Inventory implements Aggregate<Inventory> {
     private InventoryIdentity id;
     private Sku sku;
     private int quantity;
     private List<Entry> entries;

     public void add(Entry entry) {
         this.quantity += entry.getQuantity();
         this.entries.add(entry);
     }
}

public class Entry implements LocalEntity<Entry> {
    private int quantity;
    // some other attributes such as whenSubmitted
}

public class TransactionalInventoryAdminService impelments InventoryAdminService, …
Run Code Online (Sandbox Code Playgroud)

domain-driven-design

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

如何自定义gradle android test html报告

我正在为Android应用程序开发验收测试.android gradle插件为测试生成了漂亮的html报告.现在我想自定义报告,将屏幕截图链接嵌入到失败测试用例中,如下所示:

在此输入图像描述

这是我目前的解决方案:

在生成报告后,按adb拉取屏幕截图并装饰html报告

task fetchScreenshots(type: Exec) {  
    executable "adb"
    args "pull", 
        "/sdcard/Robotium-Screenshots/",   
        "${buildDir}/outputs/reports/androidTests/connected/Robotium-Screenshots/"
}

task decoratingAndroidTestsHtmlReport << {
    def screenshots = fileTree("${buildDir}/outputs/reports/androidTests/connected/Robotium-Screenshots")

    screenshots.each { screenshot ->

        def testName = screenshot.name.substring(0, screenshot.name.lastIndexOf("."))
        def testClassName = testClassName(testName)
        def testMethodName = testMethodName(testName)

        def html = file("${buildDir}/outputs/reports/androidTests/connected/${testClassName}.html")
        def patternToFind = "<h3 class=\"failures\">${testMethodName}</h3>"
        def patternToReplace = "${patternToFind}<a href=\"Robotium-Screenshots/${screenshot.name}\">Screenshot</a>"

        replacePatternInFile(html){
            it.replaceAll(patternToFind, patternToReplace)
        }
    }
}

gradle.projectsEvaluated {

    decoratingAndroidTestsHtmlReport.dependsOn(fetchScreenshots)

    connectedAndroidTest {
        finalizedBy decoratingAndroidTestsHtmlReport
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方法有效,但我认为这不是正确的方法.我认为修改html模板而不是黑客攻击html内容会更加清晰.但我没有找到开始的线索.

junit android gradle android-gradle-plugin

7
推荐指数
0
解决办法
739
查看次数

如何使用spring-test-mvc打印请求体

我正在使用spring-test-mvc测试我的控制器,但我找不到打印请求体的方法,这非常不方便.

与MockMvcResultHandlers.print()

mvc.perform(put("/payment/1234")
    .content("{\"amount\":2.3")
    .contentType(MediaType.APPLICATION_JSON))
    .andDo(print());
Run Code Online (Sandbox Code Playgroud)

我找到了一些身体信息,但没有找到身体部位:

MockHttpServletRequest:
     HTTP Method = PUT
     Request URI = /payment/1234
      Parameters = {}
         Headers = {Content-Type=[application/json]}

         Handler:
            Type = com.restbucks.ordering.rest.PaymentResource
          Method = public org.springframework.hateoas.Resource<com.restbucks.ordering.domain.Payment> com.restbucks.ordering.rest.PaymentResource.handle(com.restbucks.ordering.commands.MakePaymentCommand)

           Async:
   Async started = false
    Async result = null
Run Code Online (Sandbox Code Playgroud)

更新

在阅读了一些源代码之后,似乎我应该扩展MockMvcResultHandlers来添加一些打印项目?

//PrintingResultHandler.java
protected void printRequest(MockHttpServletRequest request) throws Exception {
    this.printer.printValue("HTTP Method", request.getMethod());
    this.printer.printValue("Request URI", request.getRequestURI());
    this.printer.printValue("Parameters", getParamsMultiValueMap(request));
    this.printer.printValue("Headers", getRequestHeaders(request));
    // add body print?
}
Run Code Online (Sandbox Code Playgroud)

更新 概念证明代码:

public static class CustomMockMvcResultHandlers {

    public static ResultHandler print() {
        return new ConsolePrintingResultHandler(); …
Run Code Online (Sandbox Code Playgroud)

spring-test-mvc spring-boot

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

由于“无法打开嵌套条目”而导致无法运行重新包装的spring boot jar

我正在为Spring Boot项目设置构建管道。

到目前为止,它分为三个阶段:

build: compile-->unit test-->archive the jar
deploy acceptance test: repack the jar for acc environment (replacing datasource.properties etc)
deploy uat test: repack the jar for uat environment (replacing datasource.properties etc)
Run Code Online (Sandbox Code Playgroud)

我不想为不同的环境从头开始构建jar,因为这样会浪费时间,并且可能会生成不一致的工件。对于传统战争项目,我只提取战争内容,替换配置文件并重新打包。但是这次用弹簧靴,以某种方式它不起作用。当我运行重新包装的罐子时,报告

java.lang.IllegalStateException: Unable to open nested entry 'lib/antlr-2.7.7.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file
    at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:378)
    at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:355)
    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:341)
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:108)
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:92)
    at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:68)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:60)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:45) …
Run Code Online (Sandbox Code Playgroud)

spring-boot

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

是否可以使用声纳扫描仪禁用特定 java 项目的 Findbugs 传感器

我有一个 android 项目,被要求设置声纳分析。

Sonarqube 服务器上安装了 findbugs 插件,但我无法删除它,因为其他 java 项目正在使用它。

问题是我不想要 findbugs 分析,但当我像这样配置 sonar-project.properties 时,它看起来是强制性的:

# Language
sonar.language=java
sonar.profile=Android Lint
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我试过

sonar.findbugs.skip=true
sonar.findbugs.disabled=true
Run Code Online (Sandbox Code Playgroud)

但没有运气

那么如何禁用这个特定项目的 findbugs 传感器呢?

android findbugs sonarqube

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