JackParker2
JackParker2

Reputation: 167

Can I do a jUnit Test in the Same Class as My Main Code?

I am new to jUnit and I am finding testing the small project I'm working on difficult. All the examples for jUnit testing seem to involve math, and what I wrote is a simple application that takes information via Scanner and creates various objects and stores them in an ArrayList. One of my thoughts was to test if the ArrayList was empty, but I obviously cannot access it from a separate class, but the standard seems to be to separate the test from the code. So I am not sure what to do?

Upvotes: 2

Views: 1889

Answers (2)

GhostCat
GhostCat

Reputation: 140427

The typical well established practices are:

  • Unit test and production code goes into different files living in different projects but using the same package names.
  • Unit tests should not rely on internal state of production code. You don't want to write a test that needs to know about a field within the class under test. Because that means that your test can break when you change the production code to solve the problem differently.

The real answer here: you should share pieces of your code with us, to receive really helpful feedback.

Upvotes: 1

Ray Tayek
Ray Tayek

Reputation: 10003

You could provide a protected accessor for the array so that it can be used by the unit tests (which should be in the same package).

Upvotes: 2

Related Questions