0

I have the following public void method:

public void setValidFrom (java.util.Date validFrom) {
        _validFrom = validFrom;
    }

I try to run a JUnit test for which I need to set a Date using the method upon:

@Test
public void testSomething
    try {
    
        Object.setValidFrom(validFrom); //Error: validFrom cannot be resolved to a variable
    }

How can I set a date here for validFrom in this test case?

I tried to define a variable in the test case itself but I do not think that is the correct approach. This was my approach:

java.util.Date validFrom = new java.util.Date();

And my JUnit got a NullPointerException.

Tobitor
  • 1,388
  • 1
  • 23
  • 58
  • 1
    Well what value *do* you expect to pass to the method? (And have you really called your class `Object`? I'd strongly encourage you to use a name which doesn't conflict with anything in `java.lang`.) – Jon Skeet Nov 14 '20 at 11:36
  • Thank you for your answer! No, I did not call it `Object`, sorry for the confusion. I thought, I could set a date here with method `setValidFrom`, e.g. (2020, 1, 31). – Tobitor Nov 14 '20 at 12:03
  • Well where do you expect that value (2020, 1, 31) to come from? You need to pass a value to that method - the compiler isn't going to just make up a value for you. You need to specify it somewhere. – Jon Skeet Nov 14 '20 at 13:18
  • But that would mean I have to modify the method `setValidFrom`, correct? And as I understood it this method should not be modified... – Tobitor Nov 14 '20 at 13:34
  • 1
    No, you're *calling* the method. The method has a parameter, so you have to specify an argument for that parameter. What value do you want the parameter to have? Pass that value. – Jon Skeet Nov 14 '20 at 14:27
  • Yes, okay. But how can I know in this case, what is a valid parameter? I thought I could specify it by using this kind of code: `java.util.Date newestValidFrom = new java.util.Date(2020, 6, 6);`and `newestObject.setValidFrom(newestValidFrom);`. But it does not seem to be right... – Tobitor Nov 14 '20 at 17:31
  • "It does not seem to be right" doesn't really give us any information. If you're getting a NullPointerException, then probably `newestObject` is null. Note that that constructor is deprecated, and I'd suggest avoiding using `java.util.Date` anyway. (I doubt that you really want to construct a date in July 3920, do you?) – Jon Skeet Nov 14 '20 at 18:23
  • No, I don't. That's right. But why is it July in 3920? What information would you need to answer my question? – Tobitor Nov 15 '20 at 18:34
  • 1
    "But why is it July in 3920?" - read the documentation for the constructor you're calling. "What information would you need to answer my question?" Well a [mcve] would be a good start, along with exactly what you mean by "it does not seem to be right". (If you go to a doctor, you wouldn't expect them to be able to diagnose you just by saying "I feel unwell" but without more information, would you?) – Jon Skeet Nov 16 '20 at 07:23

1 Answers1

0

I think your would be like that:--

public Class Example{
          // private Date validFrom;
           
       public void setValidFrom (java.util.Date validFrom) {
                    this.validFrom = validFrom;
                }
        }
   

junit for above code would be :-----

@Test
public void setValidFromTest(){
Example example = new Example();
example.setValidFrom (new Date())
}

if there is getter in your pojo then u can use assertEquals method and compare the result.

priyranjan
  • 674
  • 6
  • 15