Himanshu Yadav
Himanshu Yadav

Reputation: 13587

In-Container testing with JUnit

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?

Upvotes: 1

Views: 3261

Answers (2)

phoenix7360
phoenix7360

Reputation: 2907

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.

Upvotes: 5

boutta
boutta

Reputation: 24609

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.

Upvotes: -1

Related Questions