All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nikolavp.approval.pathmappers.JunitPathMapper Maven / Gradle / Ivy

Go to download

Approval is a Java library which will make you look at your testing from a whole different angle

There is a newer version: 0.3
Show newest version
package com.nikolavp.approval.pathmappers;

/*
 * #%L
 * approval
 * %%
 * Copyright (C) 2014 Nikolavp
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import com.nikolavp.approval.FullPathMapper;
import com.nikolavp.approval.PathMapper;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * A path mapper that have to be declared as a {@link org.junit.Rule} and will use the standard junit mechanisms to put
 * your approval results in $package-name-with-slashes/$classname/$methodname.
 *
 * 

This class can be used as a {@link com.nikolavp.approval.PathMapper} in which case it will put your approval * results in that directory or you can use it as a {@link com.nikolavp.approval.FullPathMapper} in which case your * approval result for the single virifacion will be put in a file with that path. In the latter case you will * have to make sure that there aren't two approvals for a single test method.

*/ public class JunitPathMapper implements TestRule, PathMapper, FullPathMapper { private final Path parentPath; private Path currentTestPath; /** * A parent path in which you want to put your approvals if any. * * @param parentPath the parent path */ public JunitPathMapper(Path parentPath) { this.parentPath = parentPath; } @Override public Statement apply(final Statement base, final Description description) { return new Statement() { @Override public void evaluate() throws Throwable { currentTestPath = parentPath. resolve(description.getClassName().replace(".", File.separator)). resolve(description.getMethodName()); try { base.evaluate(); } finally { currentTestPath = null; } } }; } Path getCurrentTestPath() { return currentTestPath; } @Nonnull @Override public Path getApprovalPath(Object value) { return Paths.get(currentTestPath + ".approved"); } @Nonnull @Override public Path getPath(@Nullable Object value, Path approvalFilePath) { return currentTestPath.resolve(approvalFilePath); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy