0

Converted a java class into kotlin in Android app, the jacoco coverage starts to show 0 coverage on a compiler generated function, which is not access able. Other ones seem fine in the report.

enter image description here

How to make reference type for string enum in kotlin and jacoco coverage testable

java code:

public final class Message {

    private Message() { }

    public static class MessageAction {

        public static final String OPEN = "open";
        public static final String VIEW_ALL = "view_all";

        @Retention(RetentionPolicy.SOURCE)
        @StringDef({OPEN, VIEW_ALL})
        public @interface Action { }

        public String mAction;

        public MessageAction(@Action String action) {
            this.mAction = action;
        }

        public String getMessageAction() {
            return this.mAction;
        }
    }
}

in kotlin;

import androidx.annotation.StringDef

class Message private constructor() {
    class MessageAction(@param:Action var messageAction: String) {
        @kotlin.annotation.Retention(AnnotationRetention.SOURCE)
        @StringDef(OPEN, VIEW_ALL)
        annotation class Action

        companion object {
            const val OPEN = "open"
            const val VIEW_ALL = "view_all"
        }
    }
}

this is sample of how it is used in java code:

public static void doSomeThing(@Nullable String message, @Message.MessageAction.Action String action) {
   ...
}

and the test:

@Test
public void test_messageAction() {
        String testAction = "open";
        Message.MessageAction action = new Message.MessageAction(Message.MessageAction.OPEN);
        assertEquals(testAction, action.getMessageAction());
        }

the jacoco test coverage result shows 0 covergate on the function setMessageAction(@NotNull String var1) which is in the decompiled java code.
And it is not visible from the code's autocomplete hint.

the kotlin decompiled java code:

public final class Message {
    private Message() {
    }

    @Metadata( ...... )
    public static final class MessageAction {
        @NotNull
        private String messageAction;
        @NotNull
        public static final String OPEN = "open";
        @NotNull
        public static final String VIEW_ALL = "view_all";

        public static final Message.MessageAction.Companion Companion = new Message.MessageAction.Companion((DefaultConstructorMarker) null);

        @NotNull
        public final String getMessageAction() {
            return this.messageAction;
        }

        public final void setMessageAction(@NotNull String var1) {  //<=== coverage result shows it is not called
            Intrinsics.checkNotNullParameter(var1, "<set-?>");
            this.messageAction = var1;
        }

        public MessageAction(@NotNull String messageAction) {
            Intrinsics.checkNotNullParameter(messageAction, "messageAction");
            super();
            this.messageAction = messageAction;
        }

        @Retention(AnnotationRetention.SOURCE)
        @java.lang.annotation.Retention(RetentionPolicy.SOURCE)
        @Metadata( ...... )
        public @interface Action {
        }

        @Metadata( ...... )
        public static final class Companion {
            private Companion() {
            }

            // $FF: synthetic method
            public Companion(DefaultConstructorMarker $constructor_marker) {
                this();
            }
        }
    }
}
lannyf
  • 9,865
  • 12
  • 70
  • 152

1 Answers1

0

adding @JvmField resolves it

class MessageAction(@param:Action messageAction: String) {
        @kotlin.annotation.Retention(AnnotationRetention.SOURCE)
        @StringDef(OPEN, VIEW_ALL, CLEAR, TOUCH3D, PLAY, PAUSE, DISMISSED)
        annotation class Action

        @JvmField
        var messageAction: String = messageAction

        companion object {
            const val OPEN = "open"
            const val VIEW_ALL = "view_all"
           
        }
    }
lannyf
  • 9,865
  • 12
  • 70
  • 152