小编ham*_*est的帖子

对不同的测试方法使用不同的Spring测试上下文配置

我们有一个基于Spring的JUnit测试类,它使用内部测试上下文配置类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ServiceTest.Config.class)
public class ServiceTest {

    @Test
    public void someTest() {
    ...

    @Configuration
    @PropertySource(value = { "classpath:application.properties" })
    @ComponentScan({ "..." })
    public static class Config {
    ...
Run Code Online (Sandbox Code Playgroud)

最近已向Service类引入了新功能,应将相关测试添加到ServiceTest中.但是,这些还需要创建一个不同的测试上下文配置类(现有Config类的内部相当复杂,如果可能的话,将其更改为新旧测试似乎非常困难)

有没有办法实现一个测试类中的某些测试方法将使用一个配置类,而其他方法将使用另一个?@ContextConfiguration似乎只适用于类级别,因此解决方案可能是为新测试创建另一个测试类,它将使用自己的上下文配置类; 但这意味着通过两个不同的测试类覆盖相同的Service类

java junit spring spring-test junit4

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

Assert集合包含自定义类的对象,它不会覆盖equals/hashcode

我们有一个包含多个字段的自定义类,由于业务域原因,我们无法覆盖equals/hashcode方法

然而,在单元测试期间,我们应断言集合是否包含此类的项目

List<CustomClass> customObjectList = classUnderTest.methodUnderTest();
//create customObject with fields set to the very same values as one of the elements in customObjectList
//we should assert here that customObjectList contains customObject
Run Code Online (Sandbox Code Playgroud)

但是,到目前为止,我们没有找到任何可以在不覆盖equals/hashcode的情况下工作的解决方案,例如Hamcrest

assertThat(customObjectList, contains(customObject));
Run Code Online (Sandbox Code Playgroud)

导致AssertionError引用

Expected: iterable containing [<CustomClass@578486a3>]
but: item 0: was <CustomClass@551aa95a>
Run Code Online (Sandbox Code Playgroud)

是否有解决方案,而无需逐场比较?

java collections junit equals hamcrest

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

Hamcrest使用"is"前缀getter方法断言布尔字段

我们想声明一个自定义对象列表包含一个对象,其中某些字段具有某些值,并带有一系列断言,如下所示

assertThat(customObjectList, hasItem(hasProperty("someField", equalTo(someValue))));
Run Code Online (Sandbox Code Playgroud)

但是自定义对象也有布尔类型字段,其中getter方法有一个"is"前缀而不是"get",并且断言似乎失败了

java.lang.AssertionError: Expected: a collection containing hasProperty("booleanField", <true>) but: property "booleanField" is not readable
Run Code Online (Sandbox Code Playgroud)

是否有一个开箱即用的解决方案来解决这个问题,或者它应该用某种自定义匹配器来处理?

java junit hamcrest

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

使用一个map-filter lambda表达式从项列表中检索对象字段

我们必须从中我们想获取一个特定的项目,在该项目上(过滤器)进行检查SOMETYPE项目的列表,并且如果条件满足,那么检索项目(图)一个特定的字符串类型字段,所有在一个表达式

有关的方法接口如下.一切都很顺利,直到最后一步,我们希望将过滤结果映射Optional<SomeType>Optional<String>.不幸的是,我们无法确定地图调用所需的语法

public Optional<String> transform(final List<SomeType> aList)        
    return getAnItemFromTheList(aList)
            .filter(someFilterClass::anItemFulfillsCriteria)
            .map(???use getAStringTypeFieldFromTheItem() here???);

private Optional<SomeType> getAnItemFromTheList(final List<SomeType> aList) {...
public boolean anItemFulfillsCriteria(final SomeType anItem) {... //in a separate class of filter methods
private Optional<String> getAStringTypeFieldFromTheItem(final SomeType anItem) {...
Run Code Online (Sandbox Code Playgroud)

java lambda optional java-8

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

使用Grails/Groovy注入构造函数参数Spring Resource文件

从我们的Grails/Groovy应用程序中,我们需要使用遗留Java服务类中的服务,其构造函数具有org.springframework.core.io.Resource类型的参数,例如

public ServiceClass(Resource someResource)
Run Code Online (Sandbox Code Playgroud)

我们需要使用Spring DSL将服务类的实例注入到我们应用程序的Groovy类中,资源引用/ src/main/resources中的XML文件.我试图为此目的创建Spring配置,但到目前为止我找不到一个可行的解决方案.配置文件的相关部分如下所示

beans = {
    xmlns aop:"http://www.springframework.org/schema/aop",
    sec:"http://www.springframework.org/schema/security",
    context:"http://www.springframework.org/schema/context"

    serviceClass(com.somepackage.ServiceClass) {
      //here we need to refer to the constructor arg XML file some way
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了各种教程中的多种语法,例如关闭beanDefinition.constructorArgs,但遗憾的是到目前为止还没有成功.虽然应用程序编译(grails:war)和启动(grails:run-app)都没有表明bean接线存在任何问题,但当应用程序实际加载到浏览器中时,我们收到一个NPE,说明我们的Groovy类到哪个注入服务类,是一个null对象.所以看起来豆接线毕竟不成功.任何帮助表示赞赏

grails groovy resources spring classpath

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

在SQL FOR-IN(SELECT)循环中使用字符串变量的值

我有一个SQL过程,应该执行FOR-IN(SELECT ..)循环,其中SELECT内容应根据一些输入参数而变化.我的想法是将SELECT字符串存储到一个变量中,然后尝试在FOR-IN循环中提取变量值但到目前为止没有成功(之前有一个修复的SELECT语句,那就是我现在要替换的内容) ).代码如下所示

PROCEDURE run(p_boolean BOOLEAN)
IS
BEGIN
  DECLARE
    v_mystring VARCHAR(50);
  BEGIN
    IF p_boolean = TRUE
    THEN
      v_mystring := 'SELECT something...';
    ELSE
      v_mystring := 'SELECT something else...';
    END IF;

    FOR p_table_name IN (would-like-to-use-the-value-of-v_mystring-here-some-way)
    LOOP
      ...
    END LOOP;

  END;
END;
Run Code Online (Sandbox Code Playgroud)

在SQL中相当新手,很可能会发生在这里尝试使用字符串变量值的整个概念是错误的.我浏览了一些教程并尝试了其他一些想法(例如光标),但没有结果.任何想法都表示赞赏

sql string oracle plsql cursor

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

使用Optional对象字段返回null或new对象

我们有一种方法,其中我们接收一个Optional<SomeType>对象.如果包含的SomeType对象不为null,那么我们必须SomeOtherType使用对象的字段初始化SomeType对象并返回该新对象; 否则我们必须返回null

我们找到了多个不同的解决方案,我们用两个语句执行此任务,首先检索可选对象,然后再创建另一个类型对象,例如

private SomeOtherType ourMethod() {
  SomeType someObject = getOptionalSomeTypeFromSomeWhere().orElse(null);
  return someObject != null ? new SomeOtherType(someObject.getField1(), someObject.getField2(), ...) : null;
}
Run Code Online (Sandbox Code Playgroud)

有可能用一个陈述来涵盖这个吗?到目前为止,我们无法进行空检查,访问字段,创建新对象等

基本上这个问题的一个更复杂的情况:获取可选对象的字段或返回null

java lambda optional java-8

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

使用 LAG/LEAD 分析功能优化自联接 Oracle SQL 查询?

我们有一个 Oracle SQL 查询来识别表列的值已从一个记录更改为另一个记录的记录。相关列是 (ID, SOME_COLUMN, FROM_DATE, TO_DATE) 其中 ID 不是唯一的,并且 FROM_DATE 和 TO_DATE 确定该 ID 的特定行有效的时间间隔,即

(ID1, VAL1, 01/01/2016, 03/01/2016)
(ID1, VAL2, 04/01/2016, 09/01/2016)
(ID1, VAL3, 10/01/2016, 19/01/2016) 
Run Code Online (Sandbox Code Playgroud)

等等。

我们可以使用以下自连接来实现

SELECT N.ID
       O.SOME_COLUMN OLD_VALUE,
       N.SOME_COLUMN NEW_VALUE
FROM OUR_TABLE N, OUR_TABLE O
WHERE N.ID = O.ID
  AND N.FROM_DATE - 1 = O.TO_DATE
  AND N.SOME_COLUMN <> O.SOME_COLUMN
Run Code Online (Sandbox Code Playgroud)

但是,由于该表包含 1 亿条记录,因此对性能非常不利。有没有更有效的方法来做到这一点?有人暗示了分析函数(例如 LAG),但到目前为止我们无法找出可行的解决方案。任何想法,将不胜感激

sql oracle sqlperformance oracle11g

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