In-Container testing with JUnit

╄→гoц情女王★ 提交于 2019-12-11 12:47:10

问题


I am new to JUnit and trying to implement it in a legacy project. It is a Java EE project. Its UI layer is in Flex and the backend has Hibernate as the ORM layer with a SQL Server Database. It runs on Tomcat.

I am taking baby steps to learn JUnit myself and implement it into the codebase. There is a class which has its findByFK method. This method returns an array of class objects if any are found.

The method runs fine on a web application but I can't run its JUnit test case. The test case is out of the container and doesn't have access to a datasource.

How can I run a JUnit test case inside a container?

I have just started with JUnit and don't want to overcomplicate it with integration issues. Is there any way to work it out without getting into stubbing the datasource and web server etc?


回答1:


Arquillian tests allow you to run your unit test inside your container.

Doing something like :

@RunWith(Arquillian.class)
public class Test {
    @Test
    public void test(){
    //your test
    }
}

will allow you to deploy your test in your container, giving it access to your database.




回答2:


You have 2 Options here:

  1. Start the test with a runner (or otherwise) who sets up a DB with a Datasource, like in the answer from phoenix7360. But this isn't actually a Unit-Test per definition.
  2. Mock the the interaction with the DB with a Mocking framework like EasyMock or Mockito.

I'd go with the second option, if you really want to do unit-tests. But your setting isn't exactly that of a Unit-Test more a kind of Integration-Test.



来源:https://stackoverflow.com/questions/15524687/in-container-testing-with-junit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!