DAO unit testing, am I doing it right?

I am starting unit testing and reviewing Java web programming. The fact is that I do not know if I am doing the right thing.

I am creating a mini blog. I use EasyMock for mock objects.

Here is my code:

Test scenario

Postdao

Message Bean

I am very grateful for your comments and suggestions on how I could improve my code and become a better programmer. Thanks in advance!

+5
source share
6 answers

Here are some considerations for reviewing the mini code. Take them for what they stand, just do not be offended:

  • JUnit 4.x. TestCase. "@Test".
  • DAO . , . DAO .
  • DAO: . , , . , , - , , , . - , . - .
  • " ", - . . , , , , . ? ? .
  • . . , log4j.
  • createPost . . ORM, Hibernate, .
  • DAO . , .
  • , /. , DAO, , .
  • , , "id" , , INSERT . .
  • , Spring 's JDBC. . , -.
+24

, createPost (..). , Post, , . .

+2
  • ? , , , SQL, DAO .
  • , "new PostDAO (null)", , .
  • hsqldb , .
  • , JUnit 4 , TestCase, JUnit 3?
+2

, ( String SQL, , btw).

, , , - ( ). ( ?), , / . , ( ) .

. 2 ( , ). , , , ( , , )

public void testPostDAO() {
        try {
                new PostDAO(null);
                fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException ex) {}
        new PostDAO(connectionMock);
}

new PostDAO(null) - . . , , , , . , - .

+1

:

public void testPostDAO() {
                try {
                        new PostDAO(null);
                        fail("Expected IllegalArgumentException");
                } catch (IllegalArgumentException ex) {}
                new PostDAO(connectionMock);
        }

, - , , :

  • - .

  • - , - , testPostDAOWithNullArg

  • , , , WeryBadStange? .
+1

Unit Test . ? ? ?

, . . .

I think you should use integration or functional testing here. Use a real database. This will make your tests much easier, plus it will test your code and, more importantly, it will prove that your code is correct.

+1
source

All Articles