Class ChatBotTest

java.lang.Object
com.gophers.services.testCases.ChatBotTest

public class ChatBotTest extends Object
Unit tests for the `ChatBot` class, focusing on verifying methods are correctly implemented and returns expected results based on various input codes. Tests check method statics, expected return values, and handling of invalid input codes.
  • Constructor Details

    • ChatBotTest

      public ChatBotTest()
  • Method Details

    • setUp

      public void setUp() throws Exception
      Sets up the test environment by initializing the ChatBot class, instantiating default and overloaded ChatBot instances, and preparing method references and static field resets.
      Throws:
      Exception - if any initialization fails.
    • resetStaticFields

      public void resetStaticFields()
      Resets the static field `messageNumber` in the `ChatBot` class to 0. This ensures a clean state before each test is run.
    • instantiateChatBots

      public void instantiateChatBots() throws Exception
      Instantiates two ChatBot objects: one using the default constructor and the other using the overloaded constructor with an integer argument.
      Throws:
      Exception - if instantiation of the constructors fails.
    • getMethod

      public Method getMethod(String methodName, Class<?>... parameterTypes)
      Retrieves a method from the `ChatBot` class using reflection.
      Parameters:
      methodName - The name of the method to retrieve.
      parameterTypes - The parameter types for the method (if any).
      Returns:
      The method object, or null if not found.
    • testChatBotNameField

      public void testChatBotNameField() throws Exception
      Tests that the `chatBotName` field exists, is private, and is of type `String`.
      Throws:
      Exception - if the field cannot be accessed.
    • testNumResponsesGeneratedField

      public void testNumResponsesGeneratedField() throws NoSuchFieldException
      Tests that the `numResponsesGenerated` field exists, is private, and is of type `int`.
      Throws:
      NoSuchFieldException - if the field is not found.
    • testMessageLimitField_Exists

      public void testMessageLimitField_Exists() throws Exception
      Tests that the `messageLimit` field exists in the `ChatBot` class.
      Throws:
      Exception - if the field cannot be accessed.
    • testMessageLimitField_StaticPrivateInteger

      public void testMessageLimitField_StaticPrivateInteger() throws Exception
      Tests that the `messageLimit` field is private, static, and of type `int`.
      Throws:
      Exception - if the field cannot be accessed.
    • testMessageLimitField_EqualsTen

      public void testMessageLimitField_EqualsTen() throws Exception
      Tests that the `messageLimit` field has a value of 10.
      Throws:
      Exception - if the field cannot be accessed.
    • testMessageNumberField_Exists

      public void testMessageNumberField_Exists() throws Exception
      Tests that the `messageNumber` field exists, is private, and is static, with an initial value of 0.
      Throws:
      Exception - if the field cannot be accessed.
    • testMessageNumberField_PrivateStaticEqualsZero

      public void testMessageNumberField_PrivateStaticEqualsZero() throws Exception
      Tests that the `messageNumber` field is private, static, and has an initial value of 0.
      Throws:
      Exception - if the field cannot be accessed.
    • testDefaultConstructor_ObjectNotNull

      public void testDefaultConstructor_ObjectNotNull()
      Tests that the default constructor of the `ChatBot` class creates a non-null instance.
    • testDefaultConstructor_CorrectName

      public void testDefaultConstructor_CorrectName() throws Exception
      Tests that the default constructor assigns the correct default name, "ChatGPT-3.5", to the `ChatBot` instance.
      Throws:
      Exception - if the field cannot be accessed.
    • testDefaultConstructor_InitialResponseCount

      public void testDefaultConstructor_InitialResponseCount() throws Exception
      Tests that the default constructor initializes the `numResponsesGenerated` field to 0.
      Throws:
      Exception - if the field cannot be accessed.
    • testOverloadedConstructor_ObjectNotNull

      public void testOverloadedConstructor_ObjectNotNull()
      Tests that the overloaded constructor of the `ChatBot` class creates a non-null instance.
    • testOverloadedConstructor_ValidLLMCodes

      public void testOverloadedConstructor_ValidLLMCodes() throws Exception
      Tests that the overloaded constructor correctly assigns valid names based on the provided LLM code (e.g., "LLaMa", "Mistral7B", "Bard", "Solar").
      Throws:
      Exception - if the constructor or field cannot be accessed.
    • testOverloadedConstructor_InvalidLLMCode

      public void testOverloadedConstructor_InvalidLLMCode() throws Exception
      Tests that the overloaded constructor assigns the default name "ChatGPT-3.5" when an invalid LLM code is provided.
      Throws:
      Exception - if the constructor or field cannot be accessed.
    • testGetChatBotName

      public void testGetChatBotName() throws Exception
      Tests that the `getChatBotName` method returns the correct names for the default and bard ChatBot instances. The default bot name should be "ChatGPT-3.5" and the bard bot name should be "Bard".
      Throws:
      Exception - if the method invocation fails.
    • testNumResponsesGenerated

      public void testNumResponsesGenerated() throws Exception
      Tests that the `numResponsesGenerated` field is correctly initialized to 0 and increments when the bot is prompted with a message.
      Throws:
      Exception - if the method invocation fails.
    • testGetTotalNumResponsesGenerated_IsStatic

      public void testGetTotalNumResponsesGenerated_IsStatic() throws Exception
      Verifies that the `getTotalNumResponsesGenerated` method is static.
      Throws:
      Exception - if the method cannot be accessed.
    • testGetTotalNumResponsesGenerated_ReturnsTotalMessages

      public void testGetTotalNumResponsesGenerated_ReturnsTotalMessages() throws Exception
      Tests that the `getTotalNumResponsesGenerated` method correctly returns the total number of messages generated, after multiple responses.
      Throws:
      Exception - if the method invocation fails.
    • testGetTotalNumMessagesRemaining_IsStatic

      public void testGetTotalNumMessagesRemaining_IsStatic() throws Exception
      Verifies that the `getTotalNumMessagesRemaining` method is static.
      Throws:
      Exception - if the method cannot be accessed.
    • testGetTotalNumMessagesRemaining_ReturnsCorrectValue

      public void testGetTotalNumMessagesRemaining_ReturnsCorrectValue() throws Exception
      Tests that the `getTotalNumMessagesRemaining` method correctly calculates the remaining messages based on the message limit and responses generated.
      Throws:
      Exception - if the method invocation fails.
    • testGetTotalNumMessagesRemaining_ReachesZero

      public void testGetTotalNumMessagesRemaining_ReachesZero() throws Exception
      Tests that the `getTotalNumMessagesRemaining` method correctly reaches zero when the message limit is reached.
      Throws:
      Exception - if the method invocation fails.
    • testLimitReached_InitiallyFalse

      public void testLimitReached_InitiallyFalse() throws Exception
      Verifies that the `limitReached` method returns false initially (before the limit is reached).
      Throws:
      Exception - if the method invocation fails.
    • testLimitReached_TrueWhenLimitReached

      public void testLimitReached_TrueWhenLimitReached() throws Exception
      Tests that the `limitReached` method returns true when the message limit is reached.
      Throws:
      Exception - if the method invocation fails.
    • testLimitReached_IsStatic

      public void testLimitReached_IsStatic() throws Exception
      Verifies that the `limitReached` method is static.
      Throws:
      Exception - if the method invocation fails.
    • testGenerateResponse_isPrivate

      public void testGenerateResponse_isPrivate() throws Exception
      Tests that the `generateResponse` method is private.
      Throws:
      Exception - if the method cannot be accessed.
    • testGenerateResponse_ReturnsString

      public void testGenerateResponse_ReturnsString() throws Exception
      Tests that the `generateResponse` method returns a String.
      Throws:
      Exception - if the method invocation fails.
    • testGenerateResponse_IncrementsCounters

      public void testGenerateResponse_IncrementsCounters() throws Exception
      Tests that the `generateResponse` method increments the response counters (both `numResponsesGenerated` and `totalNumResponsesGenerated`) when invoked.
      Throws:
      Exception - if the method invocation fails.
    • testGenerateResponse_ReturnsCorrectStringFormat

      public void testGenerateResponse_ReturnsCorrectStringFormat() throws Exception
      Tests that the `generateResponse` method returns a response with the correct format, including the message number and generated text.
      Throws:
      Exception - if the method invocation fails.
    • testGenerateResponse_ContainsUniqueMessageNumber

      public void testGenerateResponse_ContainsUniqueMessageNumber() throws Exception
      Verifies that the `generateResponse` method generates unique message numbers for each response.
      Throws:
      Exception - if the method invocation fails.
    • testToString_BotNameHeaderExists

      public void testToString_BotNameHeaderExists() throws Exception
      Tests that the `toString` method includes the ChatBot's name as a header.
      Throws:
      Exception - if the method invocation fails.