小编jav*_*999的帖子

使用Mockito模拟枚举?

我需要模拟以下枚举:

public enum PersonStatus
{
    WORKING,
    HOLIDAY,
    SICK      
}
Run Code Online (Sandbox Code Playgroud)

这是因为在我正在测试的以下类中使用了它:

被测课程:

public interface PersonRepository extends CrudRepository<Person, Integer>
{
    List<Person> findByStatus(PersonStatus personStatus);
}
Run Code Online (Sandbox Code Playgroud)

这是我当前的测试尝试:

当前测试:

public class PersonRepositoryTest {

    private final Logger LOGGER = LoggerFactory.getLogger(PersonRepositoryTest.class);

    //Mock the PersonRepository class
    @Mock
    private PersonRepository PersonRepository;

    @Mock
    private PersonStatus personStatus;

    @Before
    public void setUp() throws Exception {

        MockitoAnnotations.initMocks(this);
        assertThat(PersonRepository, notNullValue());
        assertThat(PersonStatus, notNullValue());
    }

    @Test
    public void testFindByStatus() throws ParseException {

        List<Person> personlist = PersonRepository.findByStatus(personStatus);
        assertThat(personlist, notNullValue());
    }
}
Run Code Online (Sandbox Code Playgroud)

产生以下错误:

错误:

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class …
Run Code Online (Sandbox Code Playgroud)

java enums unit-testing final mockito

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

Java REST 调用与 Web 应用程序的前端有何关系?

我知道Web 应用程序REST中的调用是如何工作的Java。例如,当到达 a 时,URL将使用 调用其方法HTTP

例如:

 @GET
    @Path("people/{id}")
    public Response getPersonWithId(@PathParam("id") id) {

      //return the person object with that Id

    }
Run Code Online (Sandbox Code Playgroud)

不确定这是如何链接到前端的?

UI 的作用(即javascript)只是将用户带到特定的 URL 以便可以调用后端方法吗?

例如,如果用户按下“获取详细信息”按钮,该按钮是否会将他们重定向到负责返回详细信息的 URL,然后调用后端功能?

javascript java rest http

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

重构依赖循环的方法

我的下面的java代码会增加一个int值,具体取决于a中有多少只狗list超过一定年龄:

for (int i = 0; i < dogList.size(); i++) {
    if (dogList.get(i).getAge() > 5) {
        dogsOverFive++
        continue;
    }           
}
Run Code Online (Sandbox Code Playgroud)

将我的代码重构为自己的方法最佳做法是什么?
我应该通过ilist作为参数吗?

java methods refactoring for-loop list

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

在Spring数据中将模式名称添加到实体?

我使用Oracle DB和时收到错误Spring Data.错误是:

ORA-00942: table or view does not exist
Run Code Online (Sandbox Code Playgroud)

导致此错误的原因是我连接的用户无法访问我希望连接到的模式中的表.

我读到这2个修复程序是synonyms在我的数据库中创建或指定schema每个实体/表所属的.

我将首先尝试Schema方法.我该怎么办?

我的例子实体下方,一个兽医架构:

@Entity
@Table(name = "Dog")
public class Dog
{
    @Id
    private String id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "Owner")
    private String owner;

  //getters and setters etc...
Run Code Online (Sandbox Code Playgroud)

java oracle spring entity-framework spring-data

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

Oracle-授予所有权限?

每当我在 ORACLE 中授予用户“所有权限”(下面的示例)时,这实际上会做什么?

我的理解是,它赋予用户任何特权,例如在该模式中插入、删除等,但授予数据库中的任何模式?

grant all privileges to my_user;
Run Code Online (Sandbox Code Playgroud)

oracle

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

CrudRepository .delete()方法是事务性的吗?

使用Spring-data时,可以扩展CrudRepository.

这个存储库.delete()方法如何在"引擎盖下"工作?

还有,这个方法Transactional呢?如果是这种情况,使用@Transactional时是否需要使用注释Spring-data.

例如,在@Transactional这里需要的?:

扩展CrudRepository:

public interface PersonRepository extends CrudRepository<Person, Integer> {

}
Run Code Online (Sandbox Code Playgroud)

在服务类中使用delete方法:

 @Transactional
 public void deletePerson(Person person) {

        personRepository.delete(person);
    }
Run Code Online (Sandbox Code Playgroud)

编辑:怎么会@Transactional在这里工作?

 @Transactional
     public void deletePersonAndTable(Person person, Table table) {

            personRepository.delete(person);

            tableRepository.delete(Table);

        }
Run Code Online (Sandbox Code Playgroud)

java spring crud transactional spring-data

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

如何将命令行参数添加到 Intelij 中的运行配置?

使用 . 时如何将 a 添加command line argument到我的应用程序中Spring bootIntelij

当前打开编辑配置时我的命令是spring-boot:run

在此输入图像描述

我想在此之后添加一个命令行参数。

作为参考,我的主类扩展了实现CommandLineRunner

java spring arguments cmd intellij-idea

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

Junit:为删除实体的方法编写测试?

对于下面的代码,我可以编写哪些最详尽的测试?

public void deleteFromPerson(person person) {
    person = personRepository.returnPerson(person.getId());
    personRepository.delete(person);
}
Run Code Online (Sandbox Code Playgroud)

这个方法在a service课堂上.该方法调用JpaRepository,然后delete()在实体上调用它的方法.

如果无法测试实体是否被删除,那么我可以在该方法上运行其他 任何东西tests cases吗?

java junit hibernate jpa spring-data

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

测试使用Junit调用了Logback Log语句?

下面是我尝试为其编写Junit测试的方法:

我想测试的方法:

   //logger declared outside of method in Class
   private static Logger LOGGER = LoggerFactory.getLogger(PersonService.class);

    public void showOutputOfIdentifications(int age) {

        if(age>25){

            LOGGER.info("Over 25");

        }else{

            LOGGER.info("25 or Under");

        }
     }
Run Code Online (Sandbox Code Playgroud)

如何测试以验证是否Logback已调用正确的Log语句?

在我的测试中,我尝试模拟logger并验证它是否被调用,但是这没有用?

当前的测试尝试:

 @Test
    public void testShowOutputOfIdentifications() throws ParseException{

    int age = 10;

    Logger LOGGER_mock = mock(Logger.class);

    //call method under test
    showOutputOfIdentifications(age);

    verify(LOGGER_mock).info("25 or under");


}
Run Code Online (Sandbox Code Playgroud)

当前失败的测试输出:

Wanted but not invoked:
logger.info(
    "25 or under "
);

Actually, there were zero interactions with this mock.
Run Code Online (Sandbox Code Playgroud)

java logging junit unit-testing logback

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

如何清空原始数组?

我有以下课程:

public class Person {

    private String id;

    private Score[] scores;

    public Person() {
    }

    //getters and setters etc
}
Run Code Online (Sandbox Code Playgroud)

如何最好地删除此Score对象的score数组中的所有对象?

java arrays

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