0

I am very new to testing and I'm playing right now with integration testing.

I'm writing this piece of code to insert a new entry in the database and test the before and after arrays. But for some reason, it seems to return false and I'm not sure if I'm doing everything right:

Here is the JUnit Class:

public class TestJunit {

private Question question;
private QuestionDAO questionDaoMock;

protected void setUp(){
    question = new Question();
    questionDaoMock = mock(QuestionDAO.class);  
    question.setQuestiondao(questionDaoMock);
}

@Test
public void testAdd() {
    questionDaoMock.openCurrentSessionwithTransaction();
    List<Question> currentQuestions = new ArrayList<Question>();
    currentQuestions = questionDaoMock.findAll();

    question.setChapterId(64);
    question.setText("Rezultatul calculului 54*2-76:2 este...");

    questionDaoMock.persist(question);
    currentQuestions.add(question);

    List<Question> newQuestions = new ArrayList<Question>();
    newQuestions = questionDaoMock.findAll();

    questionDaoMock.closeCurrentSessionwithTransaction();

    assertEquals(currentQuestions.size(), newQuestions.size());
}
}

This is my TestRunner:

public class TestRunner {
   public static void main(String[] args) {
  Result result = JUnitCore.runClasses(TestJunit.class);

  for (Failure failure : result.getFailures()) {
     System.out.println(failure.toString());
  }

  System.out.println(result.wasSuccessful());
   }
}   

I tried already the code in the testAdd() function separately in the main function just to check if insertion works and it does. I compared the arrays size and it works as well when I'm running from main method. What am I doing wrong?

2
  • Sorry, I am not able to get your question. You are trying to insert the data with JUNIT test cases ?. If so it won't since you are using mock you have to return the result by yourself and need to check the business logic of that method alone. Commented Jun 27, 2019 at 15:06
  • Arun you are right, I renamed my JUnit class into smth else. But still not working, and I also added the @Before annotation that was missing Commented Jun 27, 2019 at 15:16

3 Answers 3

1

There should be a @Before annotation in the set up method, else Junit wont run the setUp() method before executing your testAdd() method.

Sign up to request clarification or add additional context in comments.

1 Comment

just added it, still giving false
1

Mock should not provide behaviour for you. You are need to do it yourself. For example:

questionDaoMock = Mockito.mock(QuestionDAO.class); 
Mockito.when(questionDaoMock.findAll()).thenReturn(Collections.emptyList());

Now when your code call method questionDaoMock.findAll()- Mockito return empty list for you.

I think You need to use real instance of QuestionDAO for this test.

2 Comments

it seems that the mock instance doesn't find the entries from database. If I use a real instance of QuestionDAO, does it mean I don't mock it anymore?
Mock is a stub of your real object or interface. The methods of mock object are fiction. You can say Mockito to do something when somebody call method but it not your real object. Your test await that questionDaoMock save question. But it still mock and method persist haven't got a behaviour.
0

The problem seems to be with mocking the dao class. Am I doing that right? It actually returns a null array after the findAll() method (which Im sure it works)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.