1

Please help me with an issue I have while using Spring Rest docs. I have made all required setting from Spring manual. I wrote Spring MVC Test. Here is the code. Strange symbols are in Russian.

@WebAppConfiguration
@ContextConfiguration(classes = {TestConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({"test"})
@Slf4j
public class ProductApiControllerTest {

    protected static final long TEST_PRODUCT_ID_1 = 11_071_076_993L;
    protected static final long TEST_PRODUCT_ID_2 = 21_071_076_994L;

    public static final Product TEST_PRODUCT_1 = Product.builder()
            .id(TEST_PRODUCT_ID_1)
            .productId(TEST_PRODUCT_ID_1)
            .name("WRX3000")
            .fullName("WRX3000")
            .regionPickupAvailable(Arrays.asList("a100"))
            .regionDeliveryAvailable(Arrays.asList("b200"))
            .categoryId(100500)
            .categoryName("Телевизоры")
            .categories(Arrays.asList(10L, 20L, 30L))
            .brandId(200300)
            .brandName("Samsung")
            .description("AMOLED HD Телевизор 4 поколения")
            .prices(Arrays.asList(Price.builder().baseStore("b200").priceId(777).priceValue(2990d).oldPriceValue(3490d).build()))
            .build();

    @Mock
    private SearchService searchServiceMock;

    @InjectMocks
    private ProductApiController controller;

    private MockMvc mockMvc;
    private RestDocumentationResultHandler document;


    @Rule
    public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
        this.document = document("{method-name}", preprocessResponse(prettyPrint()));
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .apply(documentationConfiguration(this.restDocumentation).snippets().withEncoding("UTF-8"))
                .alwaysDo(document)
                .build();
    }

    @Test
    public void testGetProduct() throws Exception {
        log.info("Test getProduct(..) from ProductApiController");
        when(searchServiceMock.findByProductId(TEST_PRODUCT_ID_1, "b200")).thenReturn(TEST_PRODUCT_1);
        this.document.snippets(responseFields(
                fieldWithPath("id").description("Идентификатор для служебных целей поисковой машины."),
                fieldWithPath("name").description("Название продукта"),
                fieldWithPath("fullName").description("Полное название продукта"),
                fieldWithPath("productId").description("Идентификатор (SKU) товара"),
                fieldWithPath("regionPickupAvailable").description("Показывает доступен ли продукт для самовывоза в данном регионе"),
                fieldWithPath("regionDeliveryAvailable").description("Показывает доступен ли продукт для доставки в данном регионе"),
                fieldWithPath("categoryId").description("Идентификатор категории, к которой принадлежит товар"),
                fieldWithPath("categoryName").description("Название категории, к которой принадлежит товар"),
                fieldWithPath("categories").description("Список идентификаторов категорий к которым принадлежит товар"),
                fieldWithPath("brandId").description("Идентификатор бренда товара"),
                fieldWithPath("brandName").description("Название бренда продукта"),
                fieldWithPath("description").description("Описание товара"),
                fieldWithPath("propertyAggregate").description("Какая-то фигня"),
                fieldWithPath("propepropertyAggregatertyMap").description("Дополнительные свойства товара"),
                fieldWithPath("price").description("Цена товара"),
                fieldWithPath("oldPrice").description("Предыдущая цена товара")
        ));
        mockMvc.perform(get(Constants.RestApiV1.ROOT_PATH + "/" + Constants.RestApiV1.GET_PRODUCTS + "/" + TEST_PRODUCT_ID_1 + "?baseStore=b200").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$.productId").value(TEST_PRODUCT_ID_1))
                .andExpect(jsonPath("$.name").value("WRX3000"))
                .andExpect(jsonPath("$.fullName").value("WRX3000"))
                .andExpect(jsonPath("$.regionPickupAvailable").value(false)) // a100 != b200
                .andExpect(jsonPath("$.regionDeliveryAvailable").value(true)) // b200 == b200
                .andExpect(jsonPath("$.categoryId").value(100500))
                .andExpect(jsonPath("$.brandName").value("Samsung"));
    }
}

The problem is when I start this test from IDE (Intellij IDEA) it works fine and produces all snippets. But when I run maven "package" task test fails with Results :

Tests in error: 
  testGetProduct(ru.eldorado.searchservice.web.controllers.rest.ProductApiControllerTest): com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 0xe5

I have checked the problem is in fields description. If I delete this part package goal executes fine.

Encoding of my project and all the output files is UTF-8. In maven I explicitly specified

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

Any help and ideas will be kindly appreciated.

2
  • But, is ProductApiControllerTest.java actually saved in UTF-8? In Eclipse, you can have different encoding for any file, and Eclipse knows and compiles ok, but external compile would fail (or produce bad string literals) with mixed encodings. I don't know about Intellij, but could be same issue. Commented Jan 15, 2016 at 16:40
  • Yes, it is saved in UTF-8. Intellij shows it in bottom right side of the screen and I also checked project setting. It's UTF-8 everythere. Commented Jan 15, 2016 at 18:28

2 Answers 2

1

The other my answer is just a workaround. The real problem was in standard Windows encoding for Russian version(Cp-1251). Somehow maven parameters hadn't solved this issue. But environment variable

JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8

fixed everything. The answer have been found here How to configure encoding in maven

Sign up to request clarification or add additional context in comments.

Comments

0

Answering to my own question. The problem was in Russian characters here

 public static final Product TEST_PRODUCT_1 = Product.builder()
            .id(TEST_PRODUCT_ID_1)
            .productId(TEST_PRODUCT_ID_1)
            .name("WRX3000")
            .fullName("WRX3000")
            .regionPickupAvailable(Arrays.asList("a100"))
            .regionDeliveryAvailable(Arrays.asList("b200"))
            .categoryId(100500)
            .categoryName("Телевизоры")
            .categories(Arrays.asList(10L, 20L, 30L))
            .brandId(200300)
            .brandName("Samsung")
            .description("AMOLED HD Телевизор 4 поколения")
            .prices(Arrays.asList(Price.builder().baseStore("b200").priceId(777).priceValue(2990d).oldPriceValue(3490d).build()))
            .build();

I've added method

private static String getStringInUtf8(String source) {
    return new String(source.getBytes(StandardCharsets.UTF_8));
}

and used it to assign values containing Russian characters in TEST_PRODUCT_1 declaration

public static final Product TEST_PRODUCT_1 = Product.builder()
        .id(TEST_PRODUCT_ID_1)
        .productId(TEST_PRODUCT_ID_1)
        .name("WRX3000")
        .fullName("WRX3000")
        .regionPickupAvailable(Arrays.asList("a100"))
        .regionDeliveryAvailable(Arrays.asList("b200"))
        .categoryId(100500)
        .categoryName(getStringInUtf8("Телевизоры"))
        .categories(Arrays.asList(10L, 20L, 30L))
        .brandId(200300)
        .brandName("Samsung")
        .description(getStringInUtf8("Super AMOLED HD Телевизор 5 поколения"))
        .prices(Arrays.asList(Price.builder().baseStore("b200").priceId(777).priceValue(2990d).oldPriceValue(3490d).build()))
        .build();

It solved the problem but the root of it haven't been found.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.