How to unit test string value in an object class

assert compare two objects junit
unit test compare two objects c#
fluentassertions shouldbeequivalentto
c# unit test equals method
xunit compare two objects
unit test compare two objects python
how to compare two values in junit
compare list of objects in junit

I am trying to make a unit test for an object class that holds values in it with get/sets.

I understand how you would test a number calculation.

But how do you test get/sets where there is no methods to test like x+y?

This is the class I need to test:

public class Train
{
    //Variables to hold each Trains details. Using different data types for specific entrys when necessary.
    private string _trainID;
    private string _departure;
    private string _destination;
    private string _type;
    private string _interStop;
    private TimeSpan _departureTime;
    private string _departureDay;
    private bool _sleeperBerth;
    private bool _firstClass;

    //Get/Set for setting Train ID value.
    public string TrainID
    {
        get { return _trainID; }
        set { _trainID = value; }
    }

    //Get/Set for setting departure value.
    public string Departure
    {
        get { return _departure; }
        set { _departure = value; }
    }

    //Get/Set for setting destination value.
    public string Destination
    {
        get { return _destination; }
        set { _destination = value; }
    }

    //Get/Set for setting type value.
    public string Type
    {
        get { return _type; }
        set { _type = value; }
    }

    //Get/Set for setting Intermediate Stop value.
    public string IntermediateStop
    {
        get { return _interStop; }
        set { _interStop = value; }
    }

    //Get/Set for setting Departure Time value.
    public TimeSpan DepartureTime
    {
        get { return _departureTime; }
        set { _departureTime = value; }
    }

    //Get/Set for setting Departure Day value.
    public string DepartureDay
    {
        get { return _departureDay; }
        set { _departureDay = value; }
    }

    //Get/Set for setting  Sleeper Berth value.
    public bool SleeperBerth
    {
        get { return _sleeperBerth; }
        set { _sleeperBerth = value; }
    }

    //Get/Set for setting First Class value.
    public bool FirstClass
    {
                get { return _firstClass; }
        set { _firstClass = value; }
    }
}

I have also made up some kind of unit test that I think is right but it just fails. Can someone give me insight as to how I would do this?

This is the test I have made up (is it really as simple and easy as that?):

namespace TrainTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestTrainID()
        {
            Train train = new Train();

            string id = "1S45";

            train.TrainID = "1S45";

            Assert.AreEqual(id, train.TrainID);
        }
    }
}

You are wasting your time re-inventing the wheel.

Replace your properties with auto-implemented properties, and, suddenly you don't need unit tests for your getters and setters, because the compiler creates all the getter/setter code and ensures it's correct:

public class Train
{
    public string TrainID { get; set; }
    public string Departure { get; set; }
    ... etc. ...
}

(Since you mentioned that this is a "toy example" intended to get you started with unit tests: Yes, your proposed test is fine.)

How to unit test string value in an object class, xunit compare two objects junit assert string equals unit test compare two objects python. I am trying to make a unit test for an object class that holds values in it  On the Filemenu, select Add> New Project. Tip. You can also right-click on the solution in Solution Explorerand choose Add> New Project. In the New Projectdialog box, expand Installed, expand Visual C#, and then choose Test. From the list of templates, select MSTest Test Project (.NET Core).

Syntax semantics aside, unless there was an actual error in code not shown, as you have mentioned this is a toy example, or a typo with the values passed to the test which causes a caparison error, the provided test should pass as written

Most times, structuring the test flow can help avoid simple mistakes when arranging and exercising tests

[TestClass]
public class TrainUnitTest
{
    [TestMethod]
    public void Should_Get_TrainID() {
        //Arrange
        string expected = "1S45";
        Train subject = new Train();
        subject.TrainID = expected;

        //Act
        string actual = subject.TrainID;

        //Assert
        Assert.AreEqual(expected, actual);
    }
}

The likelihood of the above failing now narrows the problem down the subject under test being at fault.

Reference Unit test basics

How to Compare Object Instances in your Unit Tests Quickly and , When unit testing, you may need to compare attribute equality instead of the of objects without overriding the default equality operator of the class under test? StringValue })); var erpUtilities = new ERPUtilities(mockErpPackageWriter. You mention how to access a private member, but Unit Tests should only exercise the public interface of a class. Tests should never access private methods/variables/etc as they are then testing the implementation and not the interface. See Bad White Box Testing in my blog at Unit Tests - White Box Testing for further information. Andrew Phillips

You should test behavior and state changes caused by behavior. Your class has no behavior and no state changes from behavior. There is nothing to test.

A valid test would be when your class would have a constructor that sets the backing fields and then asserting that the properties expose the values from the backing fields.

A failing tests would then point out an error in your constructor.

So assuming your class has a constructor like (simplified for demonstration purposes only)

public class Train {        
    private string _trainID;

    public Train(string id) {
        _trainID = id;
    }

    //Get/Set for setting Train ID value.
    public string TrainID {
        get {
            return _trainID;
        }
        set {
            _trainID = value;
        }
    }
}

See the below example of a test that can be done to confirm the constructor behaves as expected.

public class TrainUnitTest
{
    [Fact]
    public void Should_Get_TrainID()
    {
        //arrange
        var expected = "1S45";
        var subject  = new Train("1S45");

        //act
        string actual = subject.TrainID;

        //assert
        Assert.Equal(expected, actual);
    }
}

Junit Assert & AssertEquals with Example, The assert methods are provided by the class org.junit. If you want to test equality of two objects, you have the following methods You can have assertion method with an additional String parameter as the first parameter. First, exclude the JUnit bundled copy of hamcrest-core, and include the useful hamcrest-library, it contains many useful methods to test the List data type. pom.xml

Asserting Equality in your C# unit tests - Paulo Gomes, Int32, a value type, so its comparison will actually check whether the objects have the same value. The property Name is of type System.String  Introduction. The below example will show you how to mock an Autowired @Value field in Spring with Junit Mockito. Generally we read some configuration values from properties file into Spring bean or component class using @Value annotated attributes but when we want to test such service or component class using Junit test class then it is required to pass values for those autowired fields.

Comparing Two Objects Using Assert.AreEqual(), In this testing tutorial, we learn how to change the way two objects are compared in an assert in unit testing by changing the behavior of the ''expect'' value. If you​'ve been using unit tests for some time you might have come to a default behavior of Equals is to compare references (in case of classes) the  The property Id is of type System.Int32, a value type, so its comparison will actually check whether the objects have the same value. The property Name is of type System.String, which although is

4. Writing Unit Tests - Unit Test Frameworks [Book], Writing Unit Tests The previous chapters present a simple unit test framework and try { String value = (String)methods[i].invoke( book, null ); assertEquals( "​test", value ); Mock objects are a way of dealing elegantly with this type of situation. What is Junit Assert? Assert is a method useful in determining Pass or Fail status of a test case, The assert methods are provided by the class org.junit.Assert which extends java.lang.Object class. There are various types of assertions like Boolean, Null, Identical etc.

Comments
  • In a "real world" application there is no need for any of this - you can just use auto-implemented properties without unit testing. Your professor is likely using this as a teaching exercise to get you used to unit testing and also probably as an opportunity to teach you what the compiler does with auto-implemented properties. If by the next class he/she hasn't explained what auto-implemented properties are and what the compiler does with them, you should ask. Once you and your class understand them, there is no need for you to keep writing 100 lines of code for what could be 10 lines.
  • Fails how? what is the failure message?
  • @ESuth Any update on the status of this issue?
  • @Kris: I disagree: There is no need to unit test an automated property's setter or getter. The Roslyn developers took care of unit-testing that.
  • That is not what I am saying. I am saying there is no need to test a class that has no behavior and/or state changes. You are saying the requirement of having to write unit tests magically disappears when using auto-implemented properties, which is no true.
  • @Kris: You are effectively advocating that language features need to be unit tested, which is a patently absurd claim. Code should not second-guess the framework (or language) on which it is built.
  • @Esuth If your professor gives you a requirement to test your getter/setter, regardless of using auto implemented properties or not, the code you did write and post is exactly what you need. It is that simple.
  • @Flater The OP posts a class containing properties with backing fields. IF there was a constructor setting the backing fields, then yes, it is correct to test if the public properties expose the values set on the backing fields.
  • Yes @Nkosi but all examples failed to point out the constructor part!
  • That is not a problem. I updated your example to include the constructor so the OP has a better idea of what you were explaining.
  • Cheers, I appreciate that!