小编Mac*_*ski的帖子

如何导入@VisibleForTesting

我知道存在注释@VisibleForTesting。我想使用它,但我不明白如何导入它。我该怎么做?

testing spring mockito guava

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

WebDriverException:消息:'浏览器似乎已经退出,然后才能连接.输出为:错误:未指定显示

运行我的测试用例时,任何我的测试程序都尝试启动firefox,我得到了错误.我正在使用robotframework,Selenium2Library和python 2.7.

1Login   [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: No browser is open
| FAIL |
WebDriverException: Message: 'The browser appears to have exited before we could connect. The output was: Error: no display specified\n'
Run Code Online (Sandbox Code Playgroud)

我的CentOS服务器上有Xwindows.我用firefox安装了yum.我的firefox安装在firefox: /usr/bin/firefox /usr/lib64/firefox /usr/share/man/man1/firefox.1.gz

这有什么不对?有没有人有类似的经历?任何参考或建议?谢谢

编辑:

结果,我运行演示后.

==============================================================================
Login Tests                                                                   
==============================================================================
Login Tests.Invalid Login :: A test suite containing tests related to inval...
==============================================================================
[ WARN ] Keyword 'Capture Page Screenshot' could not …
Run Code Online (Sandbox Code Playgroud)

python selenium webdriver robotframework selenium-webdriver

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

Spring Data JPA - 如何将Query结果转换为实体类

我使用Spring Data JPA和spring boot应用程序.我有一个具有很少属性的实体类.考虑我有10个与该实体关联的属性User,我想只检索其中的几个(用户名,密码,名字,姓氏,电子邮件).

所以我编写了一个查询来只获取5个字段,但该方法不返回实体对象,而是返回一个普通对象.

如何将查询结果强制转换为Spring Data JPA中的实体?

@Query("select userName,password,firstName,lastName,email from User")
public List<User> getUsers();
Run Code Online (Sandbox Code Playgroud)

java orm spring jpql spring-data-jpa

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

如何使用JPA注释Map <Entity,INTEGER>?

我有这张地图:

Map<Owner, Integer> ownerSharesMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

我知道如何在地图上HashMap@OneToMany,有一个基本类型为重点的关系,但我GOOGLE了很多,并没有找到一个方法来映射上面HashMap使用JPA.我看到两个选择:

将我HashMap改为:(其中uuid是关键列Owner)

Map<UUID, Integer> ownerIdSharesMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

或者创建一个这样的类:

public class Share{
  int count;
  Owner owner
}
Run Code Online (Sandbox Code Playgroud)

然后坚持这个集合:

Set<Share> shares;
Run Code Online (Sandbox Code Playgroud)

你有什么建议?有没有办法用JPA注释第一个Map,还是应该使用后面的解决方案?

请注意,在我的项目中,查询性能是主要问题,但我也想要干净的OOD.

谢谢.

java hibernate jpa hashmap

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

具有休眠条件的 COALESCE 函数

我有一个需要COALESCE与休眠标准一起使用的要求?有没有办法实现这个?我正在使用休眠 5.0。

java hibernate jpa

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

如何在 jpa 查询中使用自定义函数?

我是 Spring Jpa 和 Hibernate 的新手。我正在尝试使用 Oracle 数据库中的自定义函数获取数据。我可以定义一个实体及其相关的服务、实现和存储库。此外,我创建了一个新的自定义 Oracle 方言,registerFunction如下所示。

所以我有两个问题:

1) 在我的 Oracle 数据库中,该函数位于不同的架构下。我需要指定它的架构吗?如果是这样怎么办?或者休眠会自动找到它?

在提供完整的堆栈跟踪后,我将在本文末尾提出我的第二个问题...

这是我的完整堆栈跟踪:

MyOracle10g 方言

package blog;

import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.dialect.function.StandardSQLFunction;


public class MyOracle10gDialect extends Oracle10gDialect {

    public MyOracle10gDialect() {
        super();
        registerFunction("my_function", new StandardSQLFunction("my_function"));
    }

}
Run Code Online (Sandbox Code Playgroud)

应用程序属性

...
spring.jpa.database-platform=blog.MyOracle10gDialect
...
Run Code Online (Sandbox Code Playgroud)

实体:

package blog.models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "item", schema = "WOS_SOURCE")
public class WosItem {

    @Id
    @Column(nullable = false)
    private String UT;

    @Column(nullable = false)
    private String …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa spring-boot

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

如何存根用@InjectMocks 注释的类的方法?

下面的MyDictionary.get方法使用map通过调用注入的map.get

出于好奇,我对MyDictionary.get方法进行了stub处理,就像我对模拟和间谍所做的那样,所以我覆盖了注入。

但这仅在 MyDictionary.get确实调用map.get. 如果map.get返回一些字符串(此处为空字符串),则存根Mockito.when不起作用。这种行为就好像它不存在一样。在断言行中,dictionary.get("key")等于空字符串。这是我不明白的。

@RunWith(MockitoJUnitRunner.class)
public class MockitoTest {

    @Mock
    Map<String, String>map;

    @InjectMocks
    MyDictionary dictionary;

    @Test
    public void testMyDictionary(){
        Mockito.when(dictionary.get("key")).thenReturn("value");
        Assert.assertEquals("value", dictionary.get("key"));
    }

    private static class MyDictionary{
        private Map<String, String> map; 
        public String get(String key){
            return map.get(key);
// or,
            return "";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing mockito

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

在Spring Boot中一次保存一个实体及其所有相关实体

我正在使用Spring Boot,REST和JPA构建我的应用程序。在应用程序中,有2个具有一对多关系的实体。

实体1:

@Entity
@Table( name = "report")
@JsonIgnoreProperties(ignoreUnknown = true)
public class CustomReport {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "REPORT_SEQ")
@SequenceGenerator(sequenceName = "REPORT_SEQ", allocationSize = 1, name = "REPORT_SEQ")
private Long id;

private String name;
private Long createdBy;
private Timestamp lastModifiedTimestamp;


@OneToMany(mappedBy = "customReport", cascade = CascadeType.ALL)
private Set<CustomReportActivity> customReportActivitySet;



public Set<CustomReportActivity> getCustomReportActivitySet() {
    return customReportActivitySet;
}

public void setCustomReportActivitySet(Set<CustomReportActivity> customReportActivitySet) {
    this.customReportActivitySet = customReportActivitySet;
}



public Long getId() {
    return id;
}

public void setId(Long id) { …
Run Code Online (Sandbox Code Playgroud)

java spring jpa one-to-many spring-boot

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

如何模拟应用程序上下文

我们如何模拟应用程序上下文?我有一个演示者,我打算为此编写测试。它接收的参数是view和Context。我如何创建一个模拟来使上下文起作用?

public TutorProfilePresenter(TutorProfileScreenView view, Context context){
     this.view = view;
     this.context = context
}

public void setPrice(float price,int selectedTopics){
      int topicsPrice = 0;
      if(selectedTopics>2)
      {
        topicsPrice = (int) ((price/5.0)*(selectedTopics-2));
      }


      view.setBasePrice(price,topicsPrice,selectedTopics,
                        price+topicsPrice);
}
Run Code Online (Sandbox Code Playgroud)

java android unit-testing mockito

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

Mockito:如何模拟javax.inject.Provider创建的原型bean?

我有一个单例Spring bean,它创建原型bean。这些是从javax.inject.Provider字段中检索的:

@Component
public class MySingleton {
    @Autowired
    private javax.inject.Provider<MyPrototype> prototypeFactory;

    public void doStuff() {
        MyPrototype bean = prototypeFactory.get();
        bean.invoke();
    }
}

@Component
@Scope("prototype")
public class MyPrototype {
    public void invoke() {}
}
Run Code Online (Sandbox Code Playgroud)

现在,我想为Singleton创建一个JUnit-Test:

@Mock 
MyPrototype prototype;
@InjectMocks 
MySingleton sut;
@Test
public void testPrototype() {
    sut.doStuff();
    verify(prototype, times(1)).invoke();
}
Run Code Online (Sandbox Code Playgroud)

但这可以理解为无法正确设置Singleton's Provider

有什么办法吗?我想避免创建创建Prototype实例的Singleton Factory bean。

或者,使用@LookupSingleton 的-factory方法可能很优雅吗?我还没有研究过。

java junit spring unit-testing mockito

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