2 Replies Latest reply on Dec 9, 2015 5:15 AM by mahesh shet

    How to modify instance variables with Byteman

    mahesh shet Newbie

      Here is sample java code:

       

      public enum TestCode

      {

         INTERNAL_SERVER_ERROR(""),

         SOME_FAILURE("Unknown Error");

       

         private final String message;

         private final boolean success;

       

         private TestCode(String msg) {

         this.message = msg;

         this.success = false;

        }

       

         public String getMessage() {

              return this.message;

         }


         public boolean isSuccess() {

         return success;

        }

      }

       

      I want to set the variable success to true, before I call a test method testIsSuccess in test class. How do i set the rule for byteman to do that? Any help is appreciated.

        • 1. Re: How to modify instance variables with Byteman
          Andrew Dinn Master

          Hi Mahesh,

          I want to set the variable success to true, before I call a test method testIsSuccess in test class. How do i set the rule for byteman to do that? Any help is appreciated.

          Well, that's easy to do but the code you have shown doesn't contain a call to test.IsSuccess. Let's assume you have a class somewhere which makes the call

           

          class TestClient
          {
            public void validateTest(Test test) throws Exception
            {
              if (!test.isSuccess()) {
                throw new Exception("Error in Test " + test);
              }
            }
          }
          
          

           

          Here is a Byteman rule which will ensure that the exception does not get thrown

           

          RULE ensure success and earn $$$!
          CLASS TestClient
          METHOD validateTest
          AT INVOKE isSuccess
          IF TRUE
          $test.success = true
          ENDRULE
          

           

          The rule is injected into the client code just before method isSuccess is called. If you want to be more precise you could make it clearer by writing AT INVOKE Test.isSuccess or AT INVOKE Test.isSuccess(). The assignment in the rule action uses $test to refer to the first parameter of method validateTest by name. Youcan also refer to it by index using the syntax $1.

           

          regards,

           

           

          Andrew Dinn

          • 2. Re: How to modify instance variables with Byteman
            mahesh shet Newbie

            Hi Andrew Dinn,

             

            Thanks a lot for your replying soon.

             

            Here is my enum class:

            public enum TestCode {

                   INTERNAL_SERVER_ERROR("Internal Server Error"),

                   SOME_FAILURE("Unknown Error");

                   private final String message;

                   private final boolean success;

             

                   private TestCode(String msg) {

                          this.message = msg;

                          this.success = false;

                   }

             

                    public String getMessage() {

                          return this.message;

                    }

              

                    public boolean isSuccess() {

                          return success;

                    }

            }

             

             

            And here is my test class:

             

            @RunWith(BMUnitRunner.class)

            @BMUnitConfig(loadDirectory="target/test-classes", debug=true, verbose = true,bmunitVerbose = true)

            public class TestCodeTester {

                   @BMScript(value="transactionResponseCode.btm")

                   @Test
                   public void testisSuccess() throws Exception {

                         TestCode fixture = TestCode.INTERNAL_SERVER_ERROR;

                         boolean result = fixture.isSuccess();

                         // add additional test code here
                         assertEquals(false, result);

                   }

             

                  @Before
                   public void setUp() throws Exception {

                        // add additional set up code here
                   }

             

                   @After
                   public void tearDown() throws Exception {

                         // Add additional tear down code here
                   }

             

                  public static void main(String[] args) {

                       new org.junit.runner.JUnitCore().run(TestCodeTester.class);

                  }

            }