0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Junit

Last updated at Posted at 2022-06-28

Enclosed, Theories

@RunWith( Enclosed.class )
public class TestHogeDto {

    @RunWith( Theories.class )
    public static class TestConstructor {

        @Builder
        static class TestData {
            private final String hoge;
        }

        @Builder
        static class Fixture {
            private final TestData testData;
            private final String fuga;
            private final String hogehoge;
        }

        @DataPoints
        public static Fixture[] getFixture() {
            return new Fixture[] {
                    Fixture.builder()
                            .testData( TestData.builder()
                                    .hoge( "hoge" )
                                    .build() )
                            .fuga( "fuga" )
                            .hogehoge( "hogehoge" )
                            .build(),
                    // ■ 接道状況が""
                    Fixture.builder()
                            .testData( TestData.builder()
                                    .hoge( "" )
                                    .build() )
                            .fuga( "" )
                            .hogehoge( "" )
                            .build(),
            };
        }

        @Theory
        public void create_hogeDto( Fixture fixture ) {

            HogeDto result = new HogeDto( fixture.testData.hoge );

            assertEquals( fixture.fuga, result.getFuga() );
            assertEquals( fixture.hogehoge, result.getHogehoge() );
        }
    }
}

enclosedで共通の使いたい場合

staticだからクラス外にアクセスできないので継承を利用する


    /**
     * Abstract common settings class.
     */
    abstract public static class CommonSetting {

        static final String SESSION_ID = "session_id";
        static final String TRIAL_APP_ID = "tal-1";

        @Mock
        public AuthClient authClient;
        @InjectMocks
        public HogeService service;

        @Before
        public void setUp() {
            MockitoAnnotations.initMocks( this );
        }
    }

@AutoConfigureTestDatabase

実際のDBでテストしたい時に使用する

PowerMock

  • whenNewのバグ
    withAnyArgumentsを使ってwhenNewをする時に、コンストラクタの引数にnull値が来ると挙動がおかしくなる

mockstatic

            try ( MockedStatic<DigestUtils> mockedDigestUtils = mockStatic( DigestUtils.class );
                    MockedStatic<DateUtils> mockedDateUtils = mockStatic( DateUtils.class ) ) {
                mockedDigestUtils.when( DigestUtils::createHashId ).thenReturn( "ta1" );
                mockedDateUtils.when( DateUtils::now ).thenReturn(
                        ZonedDateTime.of( LocalDateTime.of( 2022, 1, 1, 0, 0, 0 ), DateUtils.DEFAULT_ZONE_ID ) );
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?