How do I write unit test for a setter method which does not have a getter method?

mockito test getters and setters
junit test getter/setter automatic
example programs of getter and setter in java
getter and setter method in java example
java getter @setter annotation
alternative to getters and setters java
junit test cases for getters and setters
setter and getter methods for array in java

I have a class which looks like this

public class Assignment {

   protected String AssName;

   public void SetAssSpec(String theSpec){

     this.AssSpec = theSpec;



Here is my Testing class

class AssignmentTest {

   void testSetAssSpec(){

       String assignmentName = "CSE101";
       Assignment cseAssigment = new Assignment();
       cseAssigment.SetAssSpec(String assignmentName);

       //Now what?



Since there is no getter method. Another way to access the set String would be cseAssigment.AssName but the problem is the AssName is protected so I can not access it. How would I test it? I am new to testing and Junit. So, please tell me if this even makes sense?

You can declare a local subclass which has a getter:

void testSetAssSpec(){
   class SubAssigment extends Assignment {
     String getter() { return AssName; }

   String assignmentName = "CSE101";
   SubAssigment cseAssigment = new SubAssigment();
   cseAssigment.SetAssSpec(String assignmentName);

   assertEquals(assignmentName, cseAssignment.getter());

But, as Kayaman points out, you probably don't really need to test that Java assignment works.

Unit testing when you have no getters and setters, I think the problems with this approach are obvious: • If a test can fail for several Don't write unit tests for usual getters and setters of a class, like this: Listing 2-5. really expect that something could go wrong with such straightforward methods? We don't have to verify that libraries or frameworks do work as expected. Since getters and setters are explicit code in your application, I think they should be tested. But no way am I going to write individual tests for every single getter and setter method. So the other day I wrote a test utility that uses reflection to test that values passed to the setter methods are the values returned by the corresponding getters.

The problem in this example is that it doesn't show how the value is used after it is being set. And that is the clue on how to test it.

If it isn't used, the setter is useless and should be removed as well.

It is protected, so I guess a subclass is using it. In that case, I would use the approach with the test subclass.

If it is used via reflection, I would also test it via reflection.

Clean C++: Sustainable Software Development Patterns and Best , Seam dependency bijection annotations can work directly on private data fields. Without getter/setter methods (or constructor methods), the test framework does not An example is the person field in the ManagerAction class; it is annotated with How can the unit test case in TestNG manipulate the ManagerAction. person  When you write unit test your test on class A should be executed independent from class B. So your unit tests for class A should not call / cover method of class B. If you want to identify obsolete getter/setter and other “dead” methods (which are not used by production code) use static code analysis for that.

Testing getter methods would be overkill, but if you ever want to check/test the value of private variables in an object with out getters you can use reflection

Field f = obj.getClass().getDeclaredField("AssName"); //NoSuchFieldException
String assignmentName = (String) f.get(obj); 

JBoss Seam: Simplicity and Power Beyond Java EE, Java Access Modifiers with Method Overriding If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive. data together into a single unit, for example, a capsule which is mixed of several medicines. Now we can use setter and getter methods to set and get the data in it. Thus, from semiconductor testing, for example, you would test gates by not just writing a 1 bit, you have to write 1 and 0 to make sure the gate is working, not stuck on high or low. The bullet points. Java Getter/Setters are a programmer’s agreement, easily broken. A getter/setter could be collaborating with external resources or services.

Java from Plinth to Paramount, Now we can use setter and getter methods to set and get the data in it. The Java Bean class is You can write the logic not to store the negative numbers in the setter methods. It is a way to So, it is better for unit testing. The standard IDE's  On the other hand, a getter method is the only way for the outside world reads the variable’s value: public int getNumber() { return this.number; } The following picture illustrates the situation: So far, setter and getter methods protect a variable’s value from unexpected changes by outside world - the caller code.

Java Programming: Basic Java programming, You may also like: Why Should I Write Getters and Setters? In Java, getter and setter are two conventional methods that are used As described in mistakes #2 and #3, it's not good to have setter and getter methods like this  Just because a private method returns the expected result, does not mean the system that eventually calls the private method uses the result correctly. Stub static references. One of the principles of a unit test is that it must have full control of the system under test.

Java Getter and Setter: Basics, Common Mistakes, and Best , After all, doesn't it seem excessive to have an entire set of unit tests Well, I'm here to say that it might actually be worth putting in the extra 5 minutes to write Typescript's syntax allows Getter methods to be called just like  Testing when methods are called, or stubbing their responses with a predetermined canned behavior, are some common scenarios we find ourselves in when it comes to unit testing our JavaScript code.

  • No, it doesn't make sense to test setters (or getters). Tests are for behaviour, not whether this.AssSpec = theSpec; works (that's guaranteed by Java).
  • You could create a local subclass with a getter...
  • And please read about java naming conventions - this rather looks like VB
  • Could you write a detailed answer? @AndyTurner
  • This might be an XY problem that is part of a bigger issue.